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.
 
 
 
 
 
 

409 line
9.1 KiB

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