Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

357 linhas
8.3 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains ellipse and circle drawing functions, both filled
  16. * and outline.
  17. */
  18. #include "config.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. #endif
  22. #include "caca.h"
  23. #include "caca_internals.h"
  24. static void ellipsepoints(caca_canvas_t *, int, int, int, int, uint32_t, int);
  25. /** \brief Draw a circle on the canvas using the given character.
  26. *
  27. * This function never fails.
  28. *
  29. * \param cv The handle to the libcaca canvas.
  30. * \param x Center X coordinate.
  31. * \param y Center Y coordinate.
  32. * \param r Circle radius.
  33. * \param ch UTF-32 character to be used to draw the circle outline.
  34. * \return This function always returns 0.
  35. */
  36. int caca_draw_circle(caca_canvas_t *cv, int x, int y, int r, uint32_t ch)
  37. {
  38. int test, dx, dy;
  39. /* Optimized Bresenham. Kick ass. */
  40. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  41. {
  42. ellipsepoints(cv, x, y, dx, dy, ch, 1);
  43. ellipsepoints(cv, x, y, dy, dx, ch, 1);
  44. test += test > 0 ? dx - dy-- : dx;
  45. }
  46. return 0;
  47. }
  48. /** \brief Fill an ellipse on the canvas using the given character.
  49. *
  50. * This function never fails.
  51. *
  52. * \param cv The handle to the libcaca canvas.
  53. * \param xo Center X coordinate.
  54. * \param yo Center Y coordinate.
  55. * \param a Ellipse X radius.
  56. * \param b Ellipse Y radius.
  57. * \param ch UTF-32 character to be used to fill the ellipse.
  58. * \return This function always returns 0.
  59. */
  60. int caca_fill_ellipse(caca_canvas_t *cv, int xo, int yo, int a, int b,
  61. uint32_t ch)
  62. {
  63. int d2;
  64. int x = 0;
  65. int y = b;
  66. int d1 = b*b - (a*a*b) + (a*a/4);
  67. while(a*a*y - a*a/2 > b*b*(x+1))
  68. {
  69. if(d1 < 0)
  70. {
  71. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  72. }
  73. else
  74. {
  75. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  76. caca_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  77. caca_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  78. y--;
  79. }
  80. x++;
  81. }
  82. caca_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  83. caca_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  84. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  85. while(y > 0)
  86. {
  87. if(d2 < 0)
  88. {
  89. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  90. x++;
  91. }
  92. else
  93. {
  94. d2 += a*a*(-2*y+3);
  95. }
  96. y--;
  97. caca_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  98. caca_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  99. }
  100. return 0;
  101. }
  102. /** \brief Draw an ellipse on the canvas using the given character.
  103. *
  104. * This function never fails.
  105. *
  106. * \param cv The handle to the libcaca canvas.
  107. * \param xo Center X coordinate.
  108. * \param yo Center Y coordinate.
  109. * \param a Ellipse X radius.
  110. * \param b Ellipse Y radius.
  111. * \param ch UTF-32 character to be used to draw the ellipse outline.
  112. * \return This function always returns 0.
  113. */
  114. int caca_draw_ellipse(caca_canvas_t *cv, int xo, int yo, int a, int b,
  115. uint32_t ch)
  116. {
  117. int d2;
  118. int x = 0;
  119. int y = b;
  120. int d1 = b*b - (a*a*b) + (a*a/4);
  121. ellipsepoints(cv, xo, yo, x, y, ch, 0);
  122. while(a*a*y - a*a/2 > b*b*(x+1))
  123. {
  124. if(d1 < 0)
  125. {
  126. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  127. }
  128. else
  129. {
  130. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  131. y--;
  132. }
  133. x++;
  134. ellipsepoints(cv, xo, yo, x, y, ch, 0);
  135. }
  136. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  137. while(y > 0)
  138. {
  139. if(d2 < 0)
  140. {
  141. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  142. x++;
  143. }
  144. else
  145. {
  146. d2 += a*a*(-2*y+3);
  147. }
  148. y--;
  149. ellipsepoints(cv, xo, yo, x, y, ch, 0);
  150. }
  151. return 0;
  152. }
  153. /** \brief Draw a thin ellipse on the canvas.
  154. *
  155. * This function never fails.
  156. *
  157. * \param cv The handle to the libcaca canvas.
  158. * \param xo Center X coordinate.
  159. * \param yo Center Y coordinate.
  160. * \param a Ellipse X radius.
  161. * \param b Ellipse Y radius.
  162. * \return This function always returns 0.
  163. */
  164. int caca_draw_thin_ellipse(caca_canvas_t *cv, int xo, int yo, int a, int b)
  165. {
  166. /* FIXME: this is not correct */
  167. int d2;
  168. int x = 0;
  169. int y = b;
  170. int d1 = b*b - (a*a*b) + (a*a/4);
  171. ellipsepoints(cv, xo, yo, x, y, '-', 1);
  172. while(a*a*y - a*a/2 > b*b*(x+1))
  173. {
  174. if(d1 < 0)
  175. {
  176. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  177. ellipsepoints(cv, xo, yo, x + 1, y, '0', 1);
  178. }
  179. else
  180. {
  181. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  182. y--;
  183. ellipsepoints(cv, xo, yo, x + 1, y, '1', 1);
  184. }
  185. x++;
  186. }
  187. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  188. while(y > 0)
  189. {
  190. if(d2 < 0)
  191. {
  192. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  193. x++;
  194. ellipsepoints(cv, xo, yo, x , y - 1, '2', 1);
  195. }
  196. else
  197. {
  198. d2 += a*a*(-2*y+3);
  199. ellipsepoints(cv, xo, yo, x , y - 1, '3', 1);
  200. }
  201. y--;
  202. }
  203. return 0;
  204. }
  205. static void ellipsepoints(caca_canvas_t *cv, int xo, int yo, int x, int y,
  206. uint32_t ch, int thin)
  207. {
  208. uint8_t b = 0;
  209. if(xo + x >= 0 && xo + x < (int)cv->width)
  210. b |= 0x1;
  211. if(xo - x >= 0 && xo - x < (int)cv->width)
  212. b |= 0x2;
  213. if(yo + y >= 0 && yo + y < (int)cv->height)
  214. b |= 0x4;
  215. if(yo - y >= 0 && yo - y < (int)cv->height)
  216. b |= 0x8;
  217. if((b & (0x1|0x4)) == (0x1|0x4)) {
  218. uint32_t c = ch;
  219. if(thin) {
  220. switch(c) {
  221. case '0':
  222. c = '-';
  223. break;
  224. case '1':
  225. c = ',';
  226. break;
  227. case '2':
  228. c = '/';
  229. break;
  230. case '3':
  231. c = '|';
  232. break;
  233. }
  234. }
  235. caca_put_char(cv, xo + x, yo + y, c);
  236. }
  237. if((b & (0x2|0x4)) == (0x2|0x4)) {
  238. uint32_t c = ch;
  239. if(thin) {
  240. switch(c) {
  241. case '0':
  242. c = '-';
  243. break;
  244. case '1':
  245. c = '.';
  246. break;
  247. case '2':
  248. c = '\\';
  249. break;
  250. case '3':
  251. c = '|';
  252. break;
  253. }
  254. }
  255. caca_put_char(cv, xo - x, yo + y, c);
  256. }
  257. if((b & (0x1|0x8)) == (0x1|0x8)) {
  258. uint32_t c = ch;
  259. if(thin) {
  260. switch(c) {
  261. case '0':
  262. c = '-';
  263. break;
  264. case '1':
  265. c = '`';
  266. break;
  267. case '2':
  268. c = '\\';
  269. break;
  270. case '3':
  271. c = '|';
  272. break;
  273. }
  274. }
  275. caca_put_char(cv, xo + x, yo - y, c);
  276. }
  277. if((b & (0x2|0x8)) == (0x2|0x8)) {
  278. uint32_t c = ch;
  279. if(thin) {
  280. switch(c) {
  281. case '0':
  282. c = '-';
  283. break;
  284. case '1':
  285. c = '\'';
  286. break;
  287. case '2':
  288. c = '/';
  289. break;
  290. case '3':
  291. c = '|';
  292. break;
  293. }
  294. }
  295. caca_put_char(cv, xo - x, yo - y, c);
  296. }
  297. }
  298. /*
  299. * XXX: The following functions are aliases.
  300. */
  301. int cucul_draw_circle(cucul_canvas_t *, int, int, int, uint32_t)
  302. CACA_ALIAS(caca_draw_circle);
  303. int cucul_draw_ellipse(cucul_canvas_t *, int, int, int, int, uint32_t)
  304. CACA_ALIAS(caca_draw_ellipse);
  305. int cucul_draw_thin_ellipse(cucul_canvas_t *, int, int, int, int)
  306. CACA_ALIAS(caca_draw_thin_ellipse);
  307. int cucul_fill_ellipse(cucul_canvas_t *, int, int, int, int, uint32_t)
  308. CACA_ALIAS(caca_fill_ellipse);