No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

conic.c 6.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains ellipse and circle drawing functions, both filled
  15. * and outline.
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. static void ellipsepoints(cucul_canvas_t *, int, int, int, int, uint32_t);
  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 libcucul 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 cucul_draw_circle(cucul_canvas_t *cv, int x, int y, int r,
  37. unsigned long int ch)
  38. {
  39. int test, dx, dy;
  40. /* Optimized Bresenham. Kick ass. */
  41. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  42. {
  43. ellipsepoints(cv, x, y, dx, dy, ch);
  44. ellipsepoints(cv, x, y, dy, dx, ch);
  45. test += test > 0 ? dx - dy-- : dx;
  46. }
  47. return 0;
  48. }
  49. /** \brief Fill an ellipse on the canvas using the given character.
  50. *
  51. * This function never fails.
  52. *
  53. * \param cv The handle to the libcucul canvas.
  54. * \param xo Center X coordinate.
  55. * \param yo Center Y coordinate.
  56. * \param a Ellipse X radius.
  57. * \param b Ellipse Y radius.
  58. * \param ch UTF-32 character to be used to fill the ellipse.
  59. * \return This function always returns 0.
  60. */
  61. int cucul_fill_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
  62. unsigned long int ch)
  63. {
  64. int d2;
  65. int x = 0;
  66. int y = b;
  67. int d1 = b*b - (a*a*b) + (a*a/4);
  68. while(a*a*y - a*a/2 > b*b*(x+1))
  69. {
  70. if(d1 < 0)
  71. {
  72. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  73. }
  74. else
  75. {
  76. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  77. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  78. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  79. y--;
  80. }
  81. x++;
  82. }
  83. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  84. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  85. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  86. while(y > 0)
  87. {
  88. if(d2 < 0)
  89. {
  90. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  91. x++;
  92. }
  93. else
  94. {
  95. d2 += a*a*(-2*y+3);
  96. }
  97. y--;
  98. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  99. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  100. }
  101. return 0;
  102. }
  103. /** \brief Draw an ellipse on the canvas using the given character.
  104. *
  105. * This function never fails.
  106. *
  107. * \param cv The handle to the libcucul canvas.
  108. * \param xo Center X coordinate.
  109. * \param yo Center Y coordinate.
  110. * \param a Ellipse X radius.
  111. * \param b Ellipse Y radius.
  112. * \param ch UTF-32 character to be used to draw the ellipse outline.
  113. * \return This function always returns 0.
  114. */
  115. int cucul_draw_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
  116. unsigned long int ch)
  117. {
  118. int d2;
  119. int x = 0;
  120. int y = b;
  121. int d1 = b*b - (a*a*b) + (a*a/4);
  122. ellipsepoints(cv, xo, yo, x, y, ch);
  123. while(a*a*y - a*a/2 > b*b*(x+1))
  124. {
  125. if(d1 < 0)
  126. {
  127. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  128. }
  129. else
  130. {
  131. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  132. y--;
  133. }
  134. x++;
  135. ellipsepoints(cv, xo, yo, x, y, ch);
  136. }
  137. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  138. while(y > 0)
  139. {
  140. if(d2 < 0)
  141. {
  142. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  143. x++;
  144. }
  145. else
  146. {
  147. d2 += a*a*(-2*y+3);
  148. }
  149. y--;
  150. ellipsepoints(cv, xo, yo, x, y, ch);
  151. }
  152. return 0;
  153. }
  154. /** \brief Draw a thin ellipse on the canvas.
  155. *
  156. * This function never fails.
  157. *
  158. * \param cv The handle to the libcucul canvas.
  159. * \param xo Center X coordinate.
  160. * \param yo Center Y coordinate.
  161. * \param a Ellipse X radius.
  162. * \param b Ellipse Y radius.
  163. * \return This function always returns 0.
  164. */
  165. int cucul_draw_thin_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b)
  166. {
  167. /* FIXME: this is not correct */
  168. int d2;
  169. int x = 0;
  170. int y = b;
  171. int d1 = b*b - (a*a*b) + (a*a/4);
  172. ellipsepoints(cv, xo, yo, x, y, '-');
  173. while(a*a*y - a*a/2 > b*b*(x+1))
  174. {
  175. if(d1 < 0)
  176. {
  177. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  178. }
  179. else
  180. {
  181. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  182. y--;
  183. }
  184. x++;
  185. ellipsepoints(cv, xo, yo, x, y, '-');
  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. }
  195. else
  196. {
  197. d2 += a*a*(-2*y+3);
  198. }
  199. y--;
  200. ellipsepoints(cv, xo, yo, x, y, '|');
  201. }
  202. return 0;
  203. }
  204. static void ellipsepoints(cucul_canvas_t *cv, int xo, int yo, int x, int y,
  205. uint32_t ch)
  206. {
  207. uint8_t b = 0;
  208. if(xo + x >= 0 && xo + x < (int)cv->width)
  209. b |= 0x1;
  210. if(xo - x >= 0 && xo - x < (int)cv->width)
  211. b |= 0x2;
  212. if(yo + y >= 0 && yo + y < (int)cv->height)
  213. b |= 0x4;
  214. if(yo - y >= 0 && yo - y < (int)cv->height)
  215. b |= 0x8;
  216. if((b & (0x1|0x4)) == (0x1|0x4))
  217. cucul_put_char(cv, xo + x, yo + y, ch);
  218. if((b & (0x2|0x4)) == (0x2|0x4))
  219. cucul_put_char(cv, xo - x, yo + y, ch);
  220. if((b & (0x1|0x8)) == (0x1|0x8))
  221. cucul_put_char(cv, xo + x, yo - y, ch);
  222. if((b & (0x2|0x8)) == (0x2|0x8))
  223. cucul_put_char(cv, xo - x, yo - y, ch);
  224. }