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.
 
 
 
 
 
 

347 rivejä
8.0 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. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains ellipse and circle drawing functions, both filled
  16. * and outline.
  17. */
  18. #include "config.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, int);
  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, 1);
  44. ellipsepoints(cv, x, y, dy, dx, ch, 1);
  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, 0);
  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, 0);
  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, 0);
  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, '-', 1);
  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. ellipsepoints(cv, xo, yo, x + 1, y, '0', 1);
  179. }
  180. else
  181. {
  182. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  183. y--;
  184. ellipsepoints(cv, xo, yo, x + 1, y, '1', 1);
  185. }
  186. x++;
  187. }
  188. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  189. while(y > 0)
  190. {
  191. if(d2 < 0)
  192. {
  193. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  194. x++;
  195. ellipsepoints(cv, xo, yo, x , y - 1, '2', 1);
  196. }
  197. else
  198. {
  199. d2 += a*a*(-2*y+3);
  200. ellipsepoints(cv, xo, yo, x , y - 1, '3', 1);
  201. }
  202. y--;
  203. }
  204. return 0;
  205. }
  206. static void ellipsepoints(cucul_canvas_t *cv, int xo, int yo, int x, int y,
  207. uint32_t ch, int thin)
  208. {
  209. uint8_t b = 0;
  210. if(xo + x >= 0 && xo + x < (int)cv->width)
  211. b |= 0x1;
  212. if(xo - x >= 0 && xo - x < (int)cv->width)
  213. b |= 0x2;
  214. if(yo + y >= 0 && yo + y < (int)cv->height)
  215. b |= 0x4;
  216. if(yo - y >= 0 && yo - y < (int)cv->height)
  217. b |= 0x8;
  218. if((b & (0x1|0x4)) == (0x1|0x4)) {
  219. uint32_t c = ch;
  220. if(thin) {
  221. switch(c) {
  222. case '0':
  223. c = '-';
  224. break;
  225. case '1':
  226. c = ',';
  227. break;
  228. case '2':
  229. c = '/';
  230. break;
  231. case '3':
  232. c = '|';
  233. break;
  234. }
  235. }
  236. cucul_put_char(cv, xo + x, yo + y, c);
  237. }
  238. if((b & (0x2|0x4)) == (0x2|0x4)) {
  239. uint32_t c = ch;
  240. if(thin) {
  241. switch(c) {
  242. case '0':
  243. c = '-';
  244. break;
  245. case '1':
  246. c = '.';
  247. break;
  248. case '2':
  249. c = '\\';
  250. break;
  251. case '3':
  252. c = '|';
  253. break;
  254. }
  255. }
  256. cucul_put_char(cv, xo - x, yo + y, c);
  257. }
  258. if((b & (0x1|0x8)) == (0x1|0x8)) {
  259. uint32_t c = ch;
  260. if(thin) {
  261. switch(c) {
  262. case '0':
  263. c = '-';
  264. break;
  265. case '1':
  266. c = '`';
  267. break;
  268. case '2':
  269. c = '\\';
  270. break;
  271. case '3':
  272. c = '|';
  273. break;
  274. }
  275. }
  276. cucul_put_char(cv, xo + x, yo - y, c);
  277. }
  278. if((b & (0x2|0x8)) == (0x2|0x8)) {
  279. uint32_t c = ch;
  280. if(thin) {
  281. switch(c) {
  282. case '0':
  283. c = '-';
  284. break;
  285. case '1':
  286. c = '\'';
  287. break;
  288. case '2':
  289. c = '/';
  290. break;
  291. case '3':
  292. c = '|';
  293. break;
  294. }
  295. }
  296. cucul_put_char(cv, xo - x, yo - y, c);
  297. }
  298. }