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.
 
 
 
 
 
 

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