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 810 2006-04-18 12:59:07Z sam $ |
---|
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 | /* |
---|
15 | * This file contains ellipse and circle drawing functions, both filled |
---|
16 | * and outline. |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #if !defined(__KERNEL__) |
---|
22 | # include <stdlib.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | #include "cucul.h" |
---|
26 | #include "cucul_internals.h" |
---|
27 | |
---|
28 | static void ellipsepoints(cucul_canvas_t *, int, int, int, int, uint32_t); |
---|
29 | |
---|
30 | /** \brief Draw a circle on the canvas using the given character. |
---|
31 | * |
---|
32 | * \param c The handle to the libcucul canvas. |
---|
33 | * \param x Center X coordinate. |
---|
34 | * \param y Center Y coordinate. |
---|
35 | * \param r Circle radius. |
---|
36 | * \param str UTF-8 string representing the character that should be used |
---|
37 | * to draw the circle outline. |
---|
38 | * \return void |
---|
39 | */ |
---|
40 | void cucul_draw_circle(cucul_canvas_t *c, int x, int y, int r, char const *str) |
---|
41 | { |
---|
42 | int test, dx, dy; |
---|
43 | uint32_t ch = _cucul_utf8_to_utf32(str); |
---|
44 | |
---|
45 | /* Optimized Bresenham. Kick ass. */ |
---|
46 | for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++) |
---|
47 | { |
---|
48 | ellipsepoints(c, x, y, dx, dy, ch); |
---|
49 | ellipsepoints(c, x, y, dy, dx, ch); |
---|
50 | |
---|
51 | test += test > 0 ? dx - dy-- : dx; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | /** \brief Fill an ellipse on the canvas using the given character. |
---|
56 | * |
---|
57 | * \param c The handle to the libcucul canvas. |
---|
58 | * \param xo Center X coordinate. |
---|
59 | * \param yo Center Y coordinate. |
---|
60 | * \param a Ellipse X radius. |
---|
61 | * \param b Ellipse Y radius. |
---|
62 | * \param str UTF-8 string representing the character that should be used |
---|
63 | * to fill the ellipse. |
---|
64 | * \return void |
---|
65 | */ |
---|
66 | void cucul_fill_ellipse(cucul_canvas_t *c, int xo, int yo, int a, int b, |
---|
67 | char const *str) |
---|
68 | { |
---|
69 | int d2; |
---|
70 | int x = 0; |
---|
71 | int y = b; |
---|
72 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
73 | |
---|
74 | while(a*a*y - a*a/2 > b*b*(x+1)) |
---|
75 | { |
---|
76 | if(d1 < 0) |
---|
77 | { |
---|
78 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
83 | cucul_draw_line(c, xo - x, yo - y, xo + x, yo - y, str); |
---|
84 | cucul_draw_line(c, xo - x, yo + y, xo + x, yo + y, str); |
---|
85 | y--; |
---|
86 | } |
---|
87 | x++; |
---|
88 | } |
---|
89 | |
---|
90 | cucul_draw_line(c, xo - x, yo - y, xo + x, yo - y, str); |
---|
91 | cucul_draw_line(c, xo - x, yo + y, xo + x, yo + y, str); |
---|
92 | |
---|
93 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
94 | while(y > 0) |
---|
95 | { |
---|
96 | if(d2 < 0) |
---|
97 | { |
---|
98 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
99 | x++; |
---|
100 | } |
---|
101 | else |
---|
102 | { |
---|
103 | d2 += a*a*(-2*y+3); |
---|
104 | } |
---|
105 | |
---|
106 | y--; |
---|
107 | cucul_draw_line(c, xo - x, yo - y, xo + x, yo - y, str); |
---|
108 | cucul_draw_line(c, xo - x, yo + y, xo + x, yo + y, str); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | /** \brief Draw an ellipse on the canvas using the given character. |
---|
113 | * |
---|
114 | * \param c The handle to the libcucul canvas. |
---|
115 | * \param xo Center X coordinate. |
---|
116 | * \param yo Center Y coordinate. |
---|
117 | * \param a Ellipse X radius. |
---|
118 | * \param b Ellipse Y radius. |
---|
119 | * \param str UTF-8 string representing the character that should be used |
---|
120 | * to draw the ellipse outline. |
---|
121 | * \return void |
---|
122 | */ |
---|
123 | void cucul_draw_ellipse(cucul_canvas_t *c, int xo, int yo, int a, int b, |
---|
124 | char const *str) |
---|
125 | { |
---|
126 | int d2; |
---|
127 | int x = 0; |
---|
128 | int y = b; |
---|
129 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
130 | uint32_t ch = _cucul_utf8_to_utf32(str); |
---|
131 | |
---|
132 | ellipsepoints(c, xo, yo, x, y, ch); |
---|
133 | |
---|
134 | while(a*a*y - a*a/2 > b*b*(x+1)) |
---|
135 | { |
---|
136 | if(d1 < 0) |
---|
137 | { |
---|
138 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
139 | } |
---|
140 | else |
---|
141 | { |
---|
142 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
143 | y--; |
---|
144 | } |
---|
145 | x++; |
---|
146 | ellipsepoints(c, xo, yo, x, y, ch); |
---|
147 | } |
---|
148 | |
---|
149 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
150 | while(y > 0) |
---|
151 | { |
---|
152 | if(d2 < 0) |
---|
153 | { |
---|
154 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
155 | x++; |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | d2 += a*a*(-2*y+3); |
---|
160 | } |
---|
161 | |
---|
162 | y--; |
---|
163 | ellipsepoints(c, xo, yo, x, y, ch); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /** \brief Draw a thin ellipse on the canvas. |
---|
168 | * |
---|
169 | * \param c The handle to the libcucul canvas. |
---|
170 | * \param xo Center X coordinate. |
---|
171 | * \param yo Center Y coordinate. |
---|
172 | * \param a Ellipse X radius. |
---|
173 | * \param b Ellipse Y radius. |
---|
174 | * \return void |
---|
175 | */ |
---|
176 | void cucul_draw_thin_ellipse(cucul_canvas_t *c, int xo, int yo, int a, int b) |
---|
177 | { |
---|
178 | /* FIXME: this is not correct */ |
---|
179 | int d2; |
---|
180 | int x = 0; |
---|
181 | int y = b; |
---|
182 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
183 | |
---|
184 | ellipsepoints(c, xo, yo, x, y, '-'); |
---|
185 | |
---|
186 | while(a*a*y - a*a/2 > b*b*(x+1)) |
---|
187 | { |
---|
188 | if(d1 < 0) |
---|
189 | { |
---|
190 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
195 | y--; |
---|
196 | } |
---|
197 | x++; |
---|
198 | ellipsepoints(c, xo, yo, x, y, '-'); |
---|
199 | } |
---|
200 | |
---|
201 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
202 | while(y > 0) |
---|
203 | { |
---|
204 | if(d2 < 0) |
---|
205 | { |
---|
206 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
207 | x++; |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | d2 += a*a*(-2*y+3); |
---|
212 | } |
---|
213 | |
---|
214 | y--; |
---|
215 | ellipsepoints(c, xo, yo, x, y, '|'); |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | static void ellipsepoints(cucul_canvas_t *c, int xo, int yo, int x, int y, |
---|
220 | uint32_t ch) |
---|
221 | { |
---|
222 | uint8_t b = 0; |
---|
223 | |
---|
224 | if(xo + x >= 0 && xo + x < (int)c->width) |
---|
225 | b |= 0x1; |
---|
226 | if(xo - x >= 0 && xo - x < (int)c->width) |
---|
227 | b |= 0x2; |
---|
228 | if(yo + y >= 0 && yo + y < (int)c->height) |
---|
229 | b |= 0x4; |
---|
230 | if(yo - y >= 0 && yo - y < (int)c->height) |
---|
231 | b |= 0x8; |
---|
232 | |
---|
233 | if((b & (0x1|0x4)) == (0x1|0x4)) |
---|
234 | _cucul_putchar32(c, xo + x, yo + y, ch); |
---|
235 | |
---|
236 | if((b & (0x2|0x4)) == (0x2|0x4)) |
---|
237 | _cucul_putchar32(c, xo - x, yo + y, ch); |
---|
238 | |
---|
239 | if((b & (0x1|0x8)) == (0x1|0x8)) |
---|
240 | _cucul_putchar32(c, xo + x, yo - y, ch); |
---|
241 | |
---|
242 | if((b & (0x2|0x8)) == (0x2|0x8)) |
---|
243 | _cucul_putchar32(c, xo - x, yo - y, ch); |
---|
244 | } |
---|
245 | |
---|