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: conic.c 2299 2008-04-19 12:42:50Z sam $ |
---|
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 | /* |
---|
16 | * This file contains ellipse and circle drawing functions, both filled |
---|
17 | * and outline. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #if !defined(__KERNEL__) |
---|
23 | # include <stdlib.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #include "cucul.h" |
---|
27 | #include "cucul_internals.h" |
---|
28 | |
---|
29 | static void ellipsepoints(cucul_canvas_t *, int, int, int, int, uint32_t, int); |
---|
30 | |
---|
31 | /** \brief Draw a circle on the canvas using the given character. |
---|
32 | * |
---|
33 | * This function never fails. |
---|
34 | * |
---|
35 | * \param cv The handle to the libcucul canvas. |
---|
36 | * \param x Center X coordinate. |
---|
37 | * \param y Center Y coordinate. |
---|
38 | * \param r Circle radius. |
---|
39 | * \param ch UTF-32 character to be used to draw the circle outline. |
---|
40 | * \return This function always returns 0. |
---|
41 | */ |
---|
42 | int cucul_draw_circle(cucul_canvas_t *cv, int x, int y, int r, |
---|
43 | unsigned long int ch) |
---|
44 | { |
---|
45 | int test, dx, dy; |
---|
46 | |
---|
47 | /* Optimized Bresenham. Kick ass. */ |
---|
48 | for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++) |
---|
49 | { |
---|
50 | ellipsepoints(cv, x, y, dx, dy, ch, 1); |
---|
51 | ellipsepoints(cv, x, y, dy, dx, ch, 1); |
---|
52 | |
---|
53 | test += test > 0 ? dx - dy-- : dx; |
---|
54 | } |
---|
55 | |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | /** \brief Fill an ellipse on the canvas using the given character. |
---|
60 | * |
---|
61 | * This function never fails. |
---|
62 | * |
---|
63 | * \param cv The handle to the libcucul canvas. |
---|
64 | * \param xo Center X coordinate. |
---|
65 | * \param yo Center Y coordinate. |
---|
66 | * \param a Ellipse X radius. |
---|
67 | * \param b Ellipse Y radius. |
---|
68 | * \param ch UTF-32 character to be used to fill the ellipse. |
---|
69 | * \return This function always returns 0. |
---|
70 | */ |
---|
71 | int cucul_fill_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b, |
---|
72 | unsigned long int ch) |
---|
73 | { |
---|
74 | int d2; |
---|
75 | int x = 0; |
---|
76 | int y = b; |
---|
77 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
78 | |
---|
79 | while(a*a*y - a*a/2 > b*b*(x+1)) |
---|
80 | { |
---|
81 | if(d1 < 0) |
---|
82 | { |
---|
83 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
84 | } |
---|
85 | else |
---|
86 | { |
---|
87 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
88 | cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch); |
---|
89 | cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch); |
---|
90 | y--; |
---|
91 | } |
---|
92 | x++; |
---|
93 | } |
---|
94 | |
---|
95 | cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch); |
---|
96 | cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch); |
---|
97 | |
---|
98 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
99 | while(y > 0) |
---|
100 | { |
---|
101 | if(d2 < 0) |
---|
102 | { |
---|
103 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
104 | x++; |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | d2 += a*a*(-2*y+3); |
---|
109 | } |
---|
110 | |
---|
111 | y--; |
---|
112 | cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch); |
---|
113 | cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch); |
---|
114 | } |
---|
115 | |
---|
116 | return 0; |
---|
117 | } |
---|
118 | |
---|
119 | /** \brief Draw an ellipse on the canvas using the given character. |
---|
120 | * |
---|
121 | * This function never fails. |
---|
122 | * |
---|
123 | * \param cv The handle to the libcucul canvas. |
---|
124 | * \param xo Center X coordinate. |
---|
125 | * \param yo Center Y coordinate. |
---|
126 | * \param a Ellipse X radius. |
---|
127 | * \param b Ellipse Y radius. |
---|
128 | * \param ch UTF-32 character to be used to draw the ellipse outline. |
---|
129 | * \return This function always returns 0. |
---|
130 | */ |
---|
131 | int cucul_draw_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b, |
---|
132 | unsigned long int ch) |
---|
133 | { |
---|
134 | int d2; |
---|
135 | int x = 0; |
---|
136 | int y = b; |
---|
137 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
138 | |
---|
139 | ellipsepoints(cv, xo, yo, x, y, ch, 0); |
---|
140 | |
---|
141 | while(a*a*y - a*a/2 > b*b*(x+1)) |
---|
142 | { |
---|
143 | if(d1 < 0) |
---|
144 | { |
---|
145 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
150 | y--; |
---|
151 | } |
---|
152 | x++; |
---|
153 | ellipsepoints(cv, xo, yo, x, y, ch, 0); |
---|
154 | } |
---|
155 | |
---|
156 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
157 | while(y > 0) |
---|
158 | { |
---|
159 | if(d2 < 0) |
---|
160 | { |
---|
161 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
162 | x++; |
---|
163 | } |
---|
164 | else |
---|
165 | { |
---|
166 | d2 += a*a*(-2*y+3); |
---|
167 | } |
---|
168 | |
---|
169 | y--; |
---|
170 | ellipsepoints(cv, xo, yo, x, y, ch, 0); |
---|
171 | } |
---|
172 | |
---|
173 | return 0; |
---|
174 | } |
---|
175 | |
---|
176 | /** \brief Draw a thin ellipse on the canvas. |
---|
177 | * |
---|
178 | * This function never fails. |
---|
179 | * |
---|
180 | * \param cv The handle to the libcucul canvas. |
---|
181 | * \param xo Center X coordinate. |
---|
182 | * \param yo Center Y coordinate. |
---|
183 | * \param a Ellipse X radius. |
---|
184 | * \param b Ellipse Y radius. |
---|
185 | * \return This function always returns 0. |
---|
186 | */ |
---|
187 | int cucul_draw_thin_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b) |
---|
188 | { |
---|
189 | /* FIXME: this is not correct */ |
---|
190 | int d2; |
---|
191 | int x = 0; |
---|
192 | int y = b; |
---|
193 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
194 | |
---|
195 | ellipsepoints(cv, xo, yo, x, y, '-', 1); |
---|
196 | |
---|
197 | while(a*a*y - a*a/2 > b*b*(x+1)) |
---|
198 | { |
---|
199 | if(d1 < 0) |
---|
200 | { |
---|
201 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
202 | ellipsepoints(cv, xo, yo, x + 1, y, '0', 1); |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
207 | y--; |
---|
208 | ellipsepoints(cv, xo, yo, x + 1, y, '1', 1); |
---|
209 | } |
---|
210 | x++; |
---|
211 | |
---|
212 | |
---|
213 | } |
---|
214 | |
---|
215 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
216 | while(y > 0) |
---|
217 | { |
---|
218 | if(d2 < 0) |
---|
219 | { |
---|
220 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
221 | x++; |
---|
222 | ellipsepoints(cv, xo, yo, x , y - 1, '2', 1); |
---|
223 | } |
---|
224 | else |
---|
225 | { |
---|
226 | d2 += a*a*(-2*y+3); |
---|
227 | ellipsepoints(cv, xo, yo, x , y - 1, '3', 1); |
---|
228 | } |
---|
229 | |
---|
230 | y--; |
---|
231 | |
---|
232 | |
---|
233 | } |
---|
234 | |
---|
235 | return 0; |
---|
236 | } |
---|
237 | |
---|
238 | static void ellipsepoints(cucul_canvas_t *cv, int xo, int yo, int x, int y, |
---|
239 | uint32_t ch, int thin) |
---|
240 | { |
---|
241 | uint8_t b = 0; |
---|
242 | |
---|
243 | if(xo + x >= 0 && xo + x < (int)cv->width) |
---|
244 | b |= 0x1; |
---|
245 | if(xo - x >= 0 && xo - x < (int)cv->width) |
---|
246 | b |= 0x2; |
---|
247 | if(yo + y >= 0 && yo + y < (int)cv->height) |
---|
248 | b |= 0x4; |
---|
249 | if(yo - y >= 0 && yo - y < (int)cv->height) |
---|
250 | b |= 0x8; |
---|
251 | |
---|
252 | if((b & (0x1|0x4)) == (0x1|0x4)) { |
---|
253 | uint32_t c = ch; |
---|
254 | |
---|
255 | if(thin) { |
---|
256 | switch(c) { |
---|
257 | case '0': |
---|
258 | c = '-'; |
---|
259 | break; |
---|
260 | case '1': |
---|
261 | c = ','; |
---|
262 | break; |
---|
263 | case '2': |
---|
264 | c = '/'; |
---|
265 | break; |
---|
266 | case '3': |
---|
267 | c = '|'; |
---|
268 | break; |
---|
269 | } |
---|
270 | |
---|
271 | } |
---|
272 | cucul_put_char(cv, xo + x, yo + y, c); |
---|
273 | } |
---|
274 | if((b & (0x2|0x4)) == (0x2|0x4)) { |
---|
275 | uint32_t c = ch; |
---|
276 | |
---|
277 | if(thin) { |
---|
278 | switch(c) { |
---|
279 | case '0': |
---|
280 | c = '-'; |
---|
281 | break; |
---|
282 | case '1': |
---|
283 | c = '.'; |
---|
284 | break; |
---|
285 | case '2': |
---|
286 | c = '\\'; |
---|
287 | break; |
---|
288 | case '3': |
---|
289 | c = '|'; |
---|
290 | break; |
---|
291 | } |
---|
292 | |
---|
293 | } |
---|
294 | cucul_put_char(cv, xo - x, yo + y, c); |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | if((b & (0x1|0x8)) == (0x1|0x8)) { |
---|
299 | uint32_t c = ch; |
---|
300 | |
---|
301 | if(thin) { |
---|
302 | switch(c) { |
---|
303 | case '0': |
---|
304 | c = '-'; |
---|
305 | break; |
---|
306 | case '1': |
---|
307 | c = '`'; |
---|
308 | break; |
---|
309 | case '2': |
---|
310 | c = '\\'; |
---|
311 | break; |
---|
312 | case '3': |
---|
313 | c = '|'; |
---|
314 | break; |
---|
315 | } |
---|
316 | |
---|
317 | } |
---|
318 | cucul_put_char(cv, xo + x, yo - y, c); |
---|
319 | } |
---|
320 | |
---|
321 | if((b & (0x2|0x8)) == (0x2|0x8)) { |
---|
322 | uint32_t c = ch; |
---|
323 | |
---|
324 | if(thin) { |
---|
325 | switch(c) { |
---|
326 | case '0': |
---|
327 | c = '-'; |
---|
328 | break; |
---|
329 | case '1': |
---|
330 | c = '\''; |
---|
331 | break; |
---|
332 | case '2': |
---|
333 | c = '/'; |
---|
334 | break; |
---|
335 | case '3': |
---|
336 | c = '|'; |
---|
337 | break; |
---|
338 | } |
---|
339 | |
---|
340 | } |
---|
341 | cucul_put_char(cv, xo - x, yo - y, c); |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | } |
---|
346 | |
---|