You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

376 lines
7.8 KiB

  1. /*
  2. * libee ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "config.h"
  23. #ifdef HAVE_INTTYPES_H
  24. # include <inttypes.h>
  25. #else
  26. typedef unsigned char uint8_t;
  27. #endif
  28. #include <stdlib.h>
  29. #include "ee.h"
  30. #include "ee_internals.h"
  31. struct line
  32. {
  33. int x1, y1;
  34. int x2, y2;
  35. char c;
  36. void (*draw) (struct line*);
  37. };
  38. static void clip_line(struct line*);
  39. static uint8_t clip_bits(int, int);
  40. static void draw_solid_line(struct line*);
  41. static void draw_thin_line(struct line*);
  42. /**
  43. * \brief Draw a line on the screen using the given character.
  44. *
  45. * \param x1 X coordinate of the first point.
  46. * \param y1 Y coordinate of the first point.
  47. * \param x2 X coordinate of the second point.
  48. * \param y2 Y coordinate of the second point.
  49. * \param c Character to draw the line with.
  50. * \return nothing
  51. */
  52. void ee_draw_line(int x1, int y1, int x2, int y2, char c)
  53. {
  54. struct line s;
  55. s.x1 = x1;
  56. s.y1 = y1;
  57. s.x2 = x2;
  58. s.y2 = y2;
  59. s.c = c;
  60. s.draw = draw_solid_line;
  61. clip_line(&s);
  62. }
  63. void ee_draw_polyline(int x[], int y[], int n, char c)
  64. {
  65. int i;
  66. struct line s;
  67. s.c = c;
  68. s.draw = draw_solid_line;
  69. for(i = 0; i < n; i++)
  70. {
  71. s.x1 = x[i];
  72. s.y1 = y[i];
  73. s.x2 = x[i+1];
  74. s.y2 = y[i+1];
  75. clip_line(&s);
  76. }
  77. }
  78. /**
  79. * \brief Draw a thin line on the screen, using ASCII art.
  80. *
  81. * \param x1 X coordinate of the first point.
  82. * \param y1 Y coordinate of the first point.
  83. * \param x2 X coordinate of the second point.
  84. * \param y2 Y coordinate of the second point.
  85. * \return nothing
  86. */
  87. void ee_draw_thin_line(int x1, int y1, int x2, int y2)
  88. {
  89. struct line s;
  90. s.x1 = x1;
  91. s.y1 = y1;
  92. s.x2 = x2;
  93. s.y2 = y2;
  94. s.draw = draw_thin_line;
  95. clip_line(&s);
  96. }
  97. void ee_draw_thin_polyline(int x[], int y[], int n)
  98. {
  99. int i;
  100. struct line s;
  101. s.draw = draw_thin_line;
  102. for(i = 0; i < n; i++)
  103. {
  104. s.x1 = x[i];
  105. s.y1 = y[i];
  106. s.x2 = x[i+1];
  107. s.y2 = y[i+1];
  108. clip_line(&s);
  109. }
  110. }
  111. /*
  112. * XXX: The following functions are local.
  113. */
  114. /**
  115. * \brief Generic Cohen-Sutherland line clipping function.
  116. *
  117. * \param s a line structure
  118. * \return nothing
  119. */
  120. static void clip_line(struct line* s)
  121. {
  122. uint8_t bits1, bits2;
  123. bits1 = clip_bits(s->x1, s->y1);
  124. bits2 = clip_bits(s->x2, s->y2);
  125. if(bits1 & bits2)
  126. return;
  127. if(bits1 == 0)
  128. {
  129. if(bits2 == 0)
  130. s->draw(s);
  131. else
  132. {
  133. int tmp;
  134. tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
  135. tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
  136. clip_line(s);
  137. }
  138. return;
  139. }
  140. if(bits1 & (1<<0))
  141. {
  142. s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
  143. s->x1 = 0;
  144. }
  145. else if( bits1 & (1<<1) )
  146. {
  147. int xmax = ee_get_width() - 1;
  148. s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
  149. s->x1 = xmax;
  150. }
  151. else if( bits1 & (1<<2) )
  152. {
  153. s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
  154. s->y1 = 0;
  155. }
  156. else if( bits1 & (1<<3) )
  157. {
  158. int ymax = ee_get_height() - 1;
  159. s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
  160. s->y1 = ymax;
  161. }
  162. clip_line(s);
  163. }
  164. /**
  165. * \brief Helper function for clip_line().
  166. *
  167. * \param x X coordinate of the point.
  168. * \param y Y coordinate of the point.
  169. * \return b The clipping bits for the given point.
  170. */
  171. static uint8_t clip_bits(int x, int y)
  172. {
  173. uint8_t b = 0;
  174. if(x < 0)
  175. b |= (1<<0);
  176. else if(x >= ee_get_width())
  177. b |= (1<<1);
  178. if(y < 0)
  179. b |= (1<<2);
  180. else if(y >= ee_get_height())
  181. b |= (1<<3);
  182. return b;
  183. }
  184. /**
  185. * \brief Solid line drawing function, using Bresenham's mid-point line
  186. * scan-conversion algorithm.
  187. *
  188. * \param s a line structure
  189. * \return nothing
  190. */
  191. static void draw_solid_line(struct line* s)
  192. {
  193. int x1, y1, x2, y2;
  194. int dx, dy;
  195. int xinc, yinc;
  196. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  197. dx = abs(x2 - x1);
  198. dy = abs(y2 - y1);
  199. xinc = (x1 > x2) ? -1 : 1;
  200. yinc = (y1 > y2) ? -1 : 1;
  201. if(dx >= dy)
  202. {
  203. int dpr = dy << 1;
  204. int dpru = dpr - (dx << 1);
  205. int delta = dpr - dx;
  206. for(; dx>=0; dx--)
  207. {
  208. ee_putchar(x1, y1, s->c);
  209. if(delta > 0)
  210. {
  211. x1 += xinc;
  212. y1 += yinc;
  213. delta += dpru;
  214. }
  215. else
  216. {
  217. x1 += xinc;
  218. delta += dpr;
  219. }
  220. }
  221. }
  222. else
  223. {
  224. int dpr = dx << 1;
  225. int dpru = dpr - (dy << 1);
  226. int delta = dpr - dy;
  227. for(; dy >= 0; dy--)
  228. {
  229. ee_putchar(x1, y1, s->c);
  230. if(delta > 0)
  231. {
  232. x1 += xinc;
  233. y1 += yinc;
  234. delta += dpru;
  235. }
  236. else
  237. {
  238. y1 += yinc;
  239. delta += dpr;
  240. }
  241. }
  242. }
  243. }
  244. /**
  245. * \brief Thin line drawing function, using Bresenham's mid-point line
  246. * scan-conversion algorithm and ASCII art graphics.
  247. *
  248. * \param s a line structure
  249. * \return nothing
  250. */
  251. static void draw_thin_line(struct line* s)
  252. {
  253. char *charmapx, *charmapy;
  254. int x1, y1, x2, y2;
  255. int dx, dy;
  256. int yinc;
  257. if(s->x2 >= s->x1)
  258. {
  259. if(s->y1 > s->y2)
  260. charmapx = ",'";
  261. else
  262. charmapx = "`.";
  263. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  264. }
  265. else
  266. {
  267. if(s->y1 > s->y2)
  268. charmapx = "`.";
  269. else
  270. charmapx = ",'";
  271. x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
  272. }
  273. dx = abs(x2 - x1);
  274. dy = abs(y2 - y1);
  275. if(y1 > y2)
  276. {
  277. charmapy = ",'";
  278. yinc = -1;
  279. }
  280. else
  281. {
  282. yinc = 1;
  283. charmapy = "`.";
  284. }
  285. if(dx >= dy)
  286. {
  287. int dpr = dy << 1;
  288. int dpru = dpr - (dx << 1);
  289. int delta = dpr - dx;
  290. int prev = 0;
  291. for(; dx>=0; dx--)
  292. {
  293. if(delta > 0)
  294. {
  295. ee_putchar(x1, y1, charmapy[1]);
  296. x1++;
  297. y1 += yinc;
  298. delta += dpru;
  299. prev = 1;
  300. }
  301. else
  302. {
  303. if(prev)
  304. ee_putchar(x1, y1, charmapy[0]);
  305. else
  306. ee_putchar(x1, y1, '-');
  307. x1++;
  308. delta += dpr;
  309. prev = 0;
  310. }
  311. }
  312. }
  313. else
  314. {
  315. int dpr = dx << 1;
  316. int dpru = dpr - (dy << 1);
  317. int delta = dpr - dy;
  318. for(; dy >= 0; dy--)
  319. {
  320. if(delta > 0)
  321. {
  322. ee_putchar(x1, y1, charmapx[0]);
  323. ee_putchar(x1 + 1, y1, charmapx[1]);
  324. x1++;
  325. y1 += yinc;
  326. delta += dpru;
  327. }
  328. else
  329. {
  330. ee_putchar(x1, y1, '|');
  331. y1 += yinc;
  332. delta += dpr;
  333. }
  334. }
  335. }
  336. }