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.
 
 
 
 
 
 

244 linhas
5.6 KiB

  1. /*
  2. * libcucul Unicode canvas library
  3. * Copyright (c) 2002-2006 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 Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file conic.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Ellipse and circle drawing
  15. *
  16. * This file contains ellipse and circle drawing functions, both filled
  17. * and outline.
  18. */
  19. #include "config.h"
  20. #if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
  21. # include <inttypes.h>
  22. #else
  23. typedef unsigned char uint8_t;
  24. #endif
  25. #include <stdlib.h>
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. static void ellipsepoints(cucul_t *, int, int, int, int, char);
  29. /**
  30. * \brief Draw a circle on the screen using the given character.
  31. *
  32. * \param x Center X coordinate.
  33. * \param y Center Y coordinate.
  34. * \param r Circle radius.
  35. * \param c Character to draw the circle outline with.
  36. * \return void
  37. */
  38. void cucul_draw_circle(cucul_t *qq, int x, int y, int r, char c)
  39. {
  40. int test, dx, dy;
  41. /* Optimized Bresenham. Kick ass. */
  42. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  43. {
  44. ellipsepoints(qq, x, y, dx, dy, c);
  45. ellipsepoints(qq, x, y, dy, dx, c);
  46. test += test > 0 ? dx - dy-- : dx;
  47. }
  48. }
  49. /**
  50. * \brief Fill an ellipse on the screen using the given character.
  51. *
  52. * \param xo Center X coordinate.
  53. * \param yo Center Y coordinate.
  54. * \param a Ellipse X radius.
  55. * \param b Ellipse Y radius.
  56. * \param c Character to fill the ellipse with.
  57. * \return void
  58. */
  59. void cucul_fill_ellipse(cucul_t *qq, int xo, int yo, int a, int b, char c)
  60. {
  61. int d2;
  62. int x = 0;
  63. int y = b;
  64. int d1 = b*b - (a*a*b) + (a*a/4);
  65. while(a*a*y - a*a/2 > b*b*(x+1))
  66. {
  67. if(d1 < 0)
  68. {
  69. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  70. }
  71. else
  72. {
  73. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  74. cucul_draw_line(qq, xo - x, yo - y, xo + x, yo - y, c);
  75. cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, c);
  76. y--;
  77. }
  78. x++;
  79. }
  80. cucul_draw_line(qq, xo - x, yo - y, xo + x, yo - y, c);
  81. cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, c);
  82. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  83. while(y > 0)
  84. {
  85. if(d2 < 0)
  86. {
  87. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  88. x++;
  89. }
  90. else
  91. {
  92. d2 += a*a*(-2*y+3);
  93. }
  94. y--;
  95. cucul_draw_line(qq, xo - x, yo - y, xo + x, yo - y, c);
  96. cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, c);
  97. }
  98. }
  99. /**
  100. * \brief Draw an ellipse on the screen using the given character.
  101. *
  102. * \param xo Center X coordinate.
  103. * \param yo Center Y coordinate.
  104. * \param a Ellipse X radius.
  105. * \param b Ellipse Y radius.
  106. * \param c Character to draw the ellipse outline with.
  107. * \return void
  108. */
  109. void cucul_draw_ellipse(cucul_t *qq, int xo, int yo, int a, int b, char c)
  110. {
  111. int d2;
  112. int x = 0;
  113. int y = b;
  114. int d1 = b*b - (a*a*b) + (a*a/4);
  115. ellipsepoints(qq, xo, yo, x, y, c);
  116. while(a*a*y - a*a/2 > b*b*(x+1))
  117. {
  118. if(d1 < 0)
  119. {
  120. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  121. }
  122. else
  123. {
  124. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  125. y--;
  126. }
  127. x++;
  128. ellipsepoints(qq, xo, yo, x, y, c);
  129. }
  130. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  131. while(y > 0)
  132. {
  133. if(d2 < 0)
  134. {
  135. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  136. x++;
  137. }
  138. else
  139. {
  140. d2 += a*a*(-2*y+3);
  141. }
  142. y--;
  143. ellipsepoints(qq, xo, yo, x, y, c);
  144. }
  145. }
  146. /**
  147. * \brief Draw a thin ellipse on the screen.
  148. *
  149. * \param xo Center X coordinate.
  150. * \param yo Center Y coordinate.
  151. * \param a Ellipse X radius.
  152. * \param b Ellipse Y radius.
  153. * \return void
  154. */
  155. void cucul_draw_thin_ellipse(cucul_t *qq, int xo, int yo, int a, int b)
  156. {
  157. /* FIXME: this is not correct */
  158. int d2;
  159. int x = 0;
  160. int y = b;
  161. int d1 = b*b - (a*a*b) + (a*a/4);
  162. ellipsepoints(qq, xo, yo, x, y, '-');
  163. while(a*a*y - a*a/2 > b*b*(x+1))
  164. {
  165. if(d1 < 0)
  166. {
  167. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  168. }
  169. else
  170. {
  171. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  172. y--;
  173. }
  174. x++;
  175. ellipsepoints(qq, xo, yo, x, y, '-');
  176. }
  177. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  178. while(y > 0)
  179. {
  180. if(d2 < 0)
  181. {
  182. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  183. x++;
  184. }
  185. else
  186. {
  187. d2 += a*a*(-2*y+3);
  188. }
  189. y--;
  190. ellipsepoints(qq, xo, yo, x, y, '|');
  191. }
  192. }
  193. static void ellipsepoints(cucul_t *qq, int xo, int yo, int x, int y, char c)
  194. {
  195. uint8_t b = 0;
  196. if(xo + x >= 0 && xo + x < (int)qq->width)
  197. b |= 0x1;
  198. if(xo - x >= 0 && xo - x < (int)qq->width)
  199. b |= 0x2;
  200. if(yo + y >= 0 && yo + y < (int)qq->height)
  201. b |= 0x4;
  202. if(yo - y >= 0 && yo - y < (int)qq->height)
  203. b |= 0x8;
  204. if((b & (0x1|0x4)) == (0x1|0x4))
  205. cucul_putchar(qq, xo + x, yo + y, c);
  206. if((b & (0x2|0x4)) == (0x2|0x4))
  207. cucul_putchar(qq, xo - x, yo + y, c);
  208. if((b & (0x1|0x8)) == (0x1|0x8))
  209. cucul_putchar(qq, xo + x, yo - y, c);
  210. if((b & (0x2|0x8)) == (0x2|0x8))
  211. cucul_putchar(qq, xo - x, yo - y, c);
  212. }