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