Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

242 wiersze
5.6 KiB

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