You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 18 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
преди 21 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 str UTF-8 string representing the character that should be used
  34. * to draw the circle outline.
  35. * \return This function always returns 0.
  36. */
  37. int cucul_draw_circle(cucul_canvas_t *cv, int x, int y, int r, char const *str)
  38. {
  39. int test, dx, dy;
  40. uint32_t ch = cucul_utf8_to_utf32(str, NULL);
  41. /* Optimized Bresenham. Kick ass. */
  42. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  43. {
  44. ellipsepoints(cv, x, y, dx, dy, ch);
  45. ellipsepoints(cv, x, y, dy, dx, ch);
  46. test += test > 0 ? dx - dy-- : dx;
  47. }
  48. return 0;
  49. }
  50. /** \brief Fill an ellipse on the canvas using the given character.
  51. *
  52. * This function never fails.
  53. *
  54. * \param cv The handle to the libcucul canvas.
  55. * \param xo Center X coordinate.
  56. * \param yo Center Y coordinate.
  57. * \param a Ellipse X radius.
  58. * \param b Ellipse Y radius.
  59. * \param str UTF-8 string representing the character that should be used
  60. * to fill the ellipse.
  61. * \return This function always returns 0.
  62. */
  63. int cucul_fill_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
  64. char const *str)
  65. {
  66. int d2;
  67. int x = 0;
  68. int y = b;
  69. int d1 = b*b - (a*a*b) + (a*a/4);
  70. while(a*a*y - a*a/2 > b*b*(x+1))
  71. {
  72. if(d1 < 0)
  73. {
  74. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  75. }
  76. else
  77. {
  78. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  79. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
  80. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
  81. y--;
  82. }
  83. x++;
  84. }
  85. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
  86. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
  87. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  88. while(y > 0)
  89. {
  90. if(d2 < 0)
  91. {
  92. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  93. x++;
  94. }
  95. else
  96. {
  97. d2 += a*a*(-2*y+3);
  98. }
  99. y--;
  100. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
  101. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
  102. }
  103. return 0;
  104. }
  105. /** \brief Draw an ellipse on the canvas using the given character.
  106. *
  107. * This function never fails.
  108. *
  109. * \param cv The handle to the libcucul canvas.
  110. * \param xo Center X coordinate.
  111. * \param yo Center Y coordinate.
  112. * \param a Ellipse X radius.
  113. * \param b Ellipse Y radius.
  114. * \param str UTF-8 string representing the character that should be used
  115. * to draw the ellipse outline.
  116. * \return This function always returns 0.
  117. */
  118. int cucul_draw_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
  119. char const *str)
  120. {
  121. int d2;
  122. int x = 0;
  123. int y = b;
  124. int d1 = b*b - (a*a*b) + (a*a/4);
  125. uint32_t ch = cucul_utf8_to_utf32(str, NULL);
  126. ellipsepoints(cv, xo, yo, x, y, ch);
  127. while(a*a*y - a*a/2 > b*b*(x+1))
  128. {
  129. if(d1 < 0)
  130. {
  131. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  132. }
  133. else
  134. {
  135. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  136. y--;
  137. }
  138. x++;
  139. ellipsepoints(cv, xo, yo, x, y, ch);
  140. }
  141. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  142. while(y > 0)
  143. {
  144. if(d2 < 0)
  145. {
  146. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  147. x++;
  148. }
  149. else
  150. {
  151. d2 += a*a*(-2*y+3);
  152. }
  153. y--;
  154. ellipsepoints(cv, xo, yo, x, y, ch);
  155. }
  156. return 0;
  157. }
  158. /** \brief Draw a thin ellipse on the canvas.
  159. *
  160. * This function never fails.
  161. *
  162. * \param cv The handle to the libcucul canvas.
  163. * \param xo Center X coordinate.
  164. * \param yo Center Y coordinate.
  165. * \param a Ellipse X radius.
  166. * \param b Ellipse Y radius.
  167. * \return This function always returns 0.
  168. */
  169. int cucul_draw_thin_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b)
  170. {
  171. /* FIXME: this is not correct */
  172. int d2;
  173. int x = 0;
  174. int y = b;
  175. int d1 = b*b - (a*a*b) + (a*a/4);
  176. ellipsepoints(cv, xo, yo, x, y, '-');
  177. while(a*a*y - a*a/2 > b*b*(x+1))
  178. {
  179. if(d1 < 0)
  180. {
  181. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  182. }
  183. else
  184. {
  185. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  186. y--;
  187. }
  188. x++;
  189. ellipsepoints(cv, xo, yo, x, y, '-');
  190. }
  191. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  192. while(y > 0)
  193. {
  194. if(d2 < 0)
  195. {
  196. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  197. x++;
  198. }
  199. else
  200. {
  201. d2 += a*a*(-2*y+3);
  202. }
  203. y--;
  204. ellipsepoints(cv, xo, yo, x, y, '|');
  205. }
  206. return 0;
  207. }
  208. static void ellipsepoints(cucul_canvas_t *cv, int xo, int yo, int x, int y,
  209. uint32_t ch)
  210. {
  211. uint8_t b = 0;
  212. if(xo + x >= 0 && xo + x < (int)cv->width)
  213. b |= 0x1;
  214. if(xo - x >= 0 && xo - x < (int)cv->width)
  215. b |= 0x2;
  216. if(yo + y >= 0 && yo + y < (int)cv->height)
  217. b |= 0x4;
  218. if(yo - y >= 0 && yo - y < (int)cv->height)
  219. b |= 0x8;
  220. if((b & (0x1|0x4)) == (0x1|0x4))
  221. cucul_putchar(cv, xo + x, yo + y, ch);
  222. if((b & (0x2|0x4)) == (0x2|0x4))
  223. cucul_putchar(cv, xo - x, yo + y, ch);
  224. if((b & (0x1|0x8)) == (0x1|0x8))
  225. cucul_putchar(cv, xo + x, yo - y, ch);
  226. if((b & (0x2|0x8)) == (0x2|0x8))
  227. cucul_putchar(cv, xo - x, yo - y, ch);
  228. }