Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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