Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

384 řádky
8.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 functions
  25. *
  26. * This file contains line and polyline drawing functions, with both thin
  27. * and thick styles.
  28. */
  29. #include "config.h"
  30. #ifdef HAVE_INTTYPES_H
  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. struct line
  39. {
  40. int x1, y1;
  41. int x2, y2;
  42. char c;
  43. void (*draw) (struct line*);
  44. };
  45. static void clip_line(struct line*);
  46. static uint8_t clip_bits(int, int);
  47. static void draw_solid_line(struct line*);
  48. static void draw_thin_line(struct line*);
  49. /**
  50. * \brief Draw a line on the screen using the given character.
  51. *
  52. * \param x1 X coordinate of the first point.
  53. * \param y1 Y coordinate of the first point.
  54. * \param x2 X coordinate of the second point.
  55. * \param y2 Y coordinate of the second point.
  56. * \param c Character to draw the line with.
  57. * \return void
  58. */
  59. void caca_draw_line(int x1, int y1, int x2, int y2, char c)
  60. {
  61. struct line s;
  62. s.x1 = x1;
  63. s.y1 = y1;
  64. s.x2 = x2;
  65. s.y2 = y2;
  66. s.c = c;
  67. s.draw = draw_solid_line;
  68. clip_line(&s);
  69. }
  70. void caca_draw_polyline(const int x[], const int y[], int n, char c)
  71. {
  72. int i;
  73. struct line s;
  74. s.c = c;
  75. s.draw = draw_solid_line;
  76. for(i = 0; i < n; i++)
  77. {
  78. s.x1 = x[i];
  79. s.y1 = y[i];
  80. s.x2 = x[i+1];
  81. s.y2 = y[i+1];
  82. clip_line(&s);
  83. }
  84. }
  85. /**
  86. * \brief Draw a thin line on the screen, using ASCII art.
  87. *
  88. * \param x1 X coordinate of the first point.
  89. * \param y1 Y coordinate of the first point.
  90. * \param x2 X coordinate of the second point.
  91. * \param y2 Y coordinate of the second point.
  92. * \return void
  93. */
  94. void caca_draw_thin_line(int x1, int y1, int x2, int y2)
  95. {
  96. struct line s;
  97. s.x1 = x1;
  98. s.y1 = y1;
  99. s.x2 = x2;
  100. s.y2 = y2;
  101. s.draw = draw_thin_line;
  102. clip_line(&s);
  103. }
  104. void caca_draw_thin_polyline(const int x[], const int y[], int n)
  105. {
  106. int i;
  107. struct line s;
  108. s.draw = draw_thin_line;
  109. for(i = 0; i < n; i++)
  110. {
  111. s.x1 = x[i];
  112. s.y1 = y[i];
  113. s.x2 = x[i+1];
  114. s.y2 = y[i+1];
  115. clip_line(&s);
  116. }
  117. }
  118. /*
  119. * XXX: The following functions are local.
  120. */
  121. /**
  122. * \brief Generic Cohen-Sutherland line clipping function.
  123. *
  124. * \param s a line structure
  125. * \return void
  126. */
  127. static void clip_line(struct line* s)
  128. {
  129. uint8_t bits1, bits2;
  130. bits1 = clip_bits(s->x1, s->y1);
  131. bits2 = clip_bits(s->x2, s->y2);
  132. if(bits1 & bits2)
  133. return;
  134. if(bits1 == 0)
  135. {
  136. if(bits2 == 0)
  137. s->draw(s);
  138. else
  139. {
  140. int tmp;
  141. tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
  142. tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
  143. clip_line(s);
  144. }
  145. return;
  146. }
  147. if(bits1 & (1<<0))
  148. {
  149. s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
  150. s->x1 = 0;
  151. }
  152. else if(bits1 & (1<<1))
  153. {
  154. int xmax = caca_get_width() - 1;
  155. s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
  156. s->x1 = xmax;
  157. }
  158. else if(bits1 & (1<<2))
  159. {
  160. s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
  161. s->y1 = 0;
  162. }
  163. else if(bits1 & (1<<3))
  164. {
  165. int ymax = caca_get_height() - 1;
  166. s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
  167. s->y1 = ymax;
  168. }
  169. clip_line(s);
  170. }
  171. /**
  172. * \brief Helper function for clip_line().
  173. *
  174. * \param x X coordinate of the point.
  175. * \param y Y coordinate of the point.
  176. * \return b The clipping bits for the given point.
  177. */
  178. static uint8_t clip_bits(int x, int y)
  179. {
  180. uint8_t b = 0;
  181. if(x < 0)
  182. b |= (1<<0);
  183. else if(x >= (int)caca_get_width())
  184. b |= (1<<1);
  185. if(y < 0)
  186. b |= (1<<2);
  187. else if(y >= (int)caca_get_height())
  188. b |= (1<<3);
  189. return b;
  190. }
  191. /**
  192. * \brief Solid line drawing function, using Bresenham's mid-point line
  193. * scan-conversion algorithm.
  194. *
  195. * \param s a line structure
  196. * \return void
  197. */
  198. static void draw_solid_line(struct line* s)
  199. {
  200. int x1, y1, x2, y2;
  201. int dx, dy;
  202. int xinc, yinc;
  203. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  204. dx = abs(x2 - x1);
  205. dy = abs(y2 - y1);
  206. xinc = (x1 > x2) ? -1 : 1;
  207. yinc = (y1 > y2) ? -1 : 1;
  208. if(dx >= dy)
  209. {
  210. int dpr = dy << 1;
  211. int dpru = dpr - (dx << 1);
  212. int delta = dpr - dx;
  213. for(; dx>=0; dx--)
  214. {
  215. caca_putchar(x1, y1, s->c);
  216. if(delta > 0)
  217. {
  218. x1 += xinc;
  219. y1 += yinc;
  220. delta += dpru;
  221. }
  222. else
  223. {
  224. x1 += xinc;
  225. delta += dpr;
  226. }
  227. }
  228. }
  229. else
  230. {
  231. int dpr = dx << 1;
  232. int dpru = dpr - (dy << 1);
  233. int delta = dpr - dy;
  234. for(; dy >= 0; dy--)
  235. {
  236. caca_putchar(x1, y1, s->c);
  237. if(delta > 0)
  238. {
  239. x1 += xinc;
  240. y1 += yinc;
  241. delta += dpru;
  242. }
  243. else
  244. {
  245. y1 += yinc;
  246. delta += dpr;
  247. }
  248. }
  249. }
  250. }
  251. /**
  252. * \brief Thin line drawing function, using Bresenham's mid-point line
  253. * scan-conversion algorithm and ASCII art graphics.
  254. *
  255. * \param s a line structure
  256. * \return void
  257. */
  258. static void draw_thin_line(struct line* s)
  259. {
  260. char *charmapx, *charmapy;
  261. int x1, y1, x2, y2;
  262. int dx, dy;
  263. int yinc;
  264. if(s->x2 >= s->x1)
  265. {
  266. if(s->y1 > s->y2)
  267. charmapx = ",'";
  268. else
  269. charmapx = "`.";
  270. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  271. }
  272. else
  273. {
  274. if(s->y1 > s->y2)
  275. charmapx = "`.";
  276. else
  277. charmapx = ",'";
  278. x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
  279. }
  280. dx = abs(x2 - x1);
  281. dy = abs(y2 - y1);
  282. if(y1 > y2)
  283. {
  284. charmapy = ",'";
  285. yinc = -1;
  286. }
  287. else
  288. {
  289. yinc = 1;
  290. charmapy = "`.";
  291. }
  292. if(dx >= dy)
  293. {
  294. int dpr = dy << 1;
  295. int dpru = dpr - (dx << 1);
  296. int delta = dpr - dx;
  297. int prev = 0;
  298. for(; dx>=0; dx--)
  299. {
  300. if(delta > 0)
  301. {
  302. caca_putchar(x1, y1, charmapy[1]);
  303. x1++;
  304. y1 += yinc;
  305. delta += dpru;
  306. prev = 1;
  307. }
  308. else
  309. {
  310. if(prev)
  311. caca_putchar(x1, y1, charmapy[0]);
  312. else
  313. caca_putchar(x1, y1, '-');
  314. x1++;
  315. delta += dpr;
  316. prev = 0;
  317. }
  318. }
  319. }
  320. else
  321. {
  322. int dpr = dx << 1;
  323. int dpru = dpr - (dy << 1);
  324. int delta = dpr - dy;
  325. for(; dy >= 0; dy--)
  326. {
  327. if(delta > 0)
  328. {
  329. caca_putchar(x1, y1, charmapx[0]);
  330. caca_putchar(x1 + 1, y1, charmapx[1]);
  331. x1++;
  332. y1 += yinc;
  333. delta += dpru;
  334. }
  335. else
  336. {
  337. caca_putchar(x1, y1, '|');
  338. y1 += yinc;
  339. delta += dpr;
  340. }
  341. }
  342. }
  343. }