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 | * This library is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
8 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
10 | */ |
---|
11 | |
---|
12 | /** \file conic.c |
---|
13 | * \version \$Id: conic.c 678 2006-03-23 14:07:32Z sam $ |
---|
14 | * \author Sam Hocevar <sam@zoy.org> |
---|
15 | * \brief Ellipse and circle drawing |
---|
16 | * |
---|
17 | * This file contains ellipse and circle drawing functions, both filled |
---|
18 | * and outline. |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.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_t *, int, int, int, int, uint32_t); |
---|
31 | |
---|
32 | /** |
---|
33 | * \brief Draw a circle on the screen using the given character. |
---|
34 | * |
---|
35 | * \param x Center X coordinate. |
---|
36 | * \param y Center Y coordinate. |
---|
37 | * \param r Circle radius. |
---|
38 | * \param c Character to draw the circle outline with. |
---|
39 | * \return void |
---|
40 | */ |
---|
41 | void cucul_draw_circle(cucul_t *qq, int x, int y, int r, char const *str) |
---|
42 | { |
---|
43 | int test, dx, dy; |
---|
44 | uint32_t c = _cucul_utf8_to_utf32(str); |
---|
45 | |
---|
46 | /* Optimized Bresenham. Kick ass. */ |
---|
47 | for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++) |
---|
48 | { |
---|
49 | ellipsepoints(qq, x, y, dx, dy, c); |
---|
50 | ellipsepoints(qq, x, y, dy, dx, c); |
---|
51 | |
---|
52 | test += test > 0 ? dx - dy-- : dx; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * \brief Fill an ellipse on the screen using the given character. |
---|
58 | * |
---|
59 | * \param xo Center X coordinate. |
---|
60 | * \param yo Center Y coordinate. |
---|
61 | * \param a Ellipse X radius. |
---|
62 | * \param b Ellipse Y radius. |
---|
63 | * \param c Character to fill the ellipse with. |
---|
64 | * \return void |
---|
65 | */ |
---|
66 | void cucul_fill_ellipse(cucul_t *qq, 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(qq, xo - x, yo - y, xo + x, yo - y, str); |
---|
84 | cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, str); |
---|
85 | y--; |
---|
86 | } |
---|
87 | x++; |
---|
88 | } |
---|
89 | |
---|
90 | cucul_draw_line(qq, xo - x, yo - y, xo + x, yo - y, str); |
---|
91 | cucul_draw_line(qq, 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(qq, xo - x, yo - y, xo + x, yo - y, str); |
---|
108 | cucul_draw_line(qq, xo - x, yo + y, xo + x, yo + y, str); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * \brief Draw an ellipse on the screen using the given character. |
---|
114 | * |
---|
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 c Character to draw the ellipse outline with. |
---|
120 | * \return void |
---|
121 | */ |
---|
122 | void cucul_draw_ellipse(cucul_t *qq, int xo, int yo, int a, int b, |
---|
123 | char const *str) |
---|
124 | { |
---|
125 | int d2; |
---|
126 | int x = 0; |
---|
127 | int y = b; |
---|
128 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
129 | uint32_t c = _cucul_utf8_to_utf32(str); |
---|
130 | |
---|
131 | ellipsepoints(qq, xo, yo, x, y, c); |
---|
132 | |
---|
133 | while(a*a*y - a*a/2 > b*b*(x+1)) |
---|
134 | { |
---|
135 | if(d1 < 0) |
---|
136 | { |
---|
137 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
142 | y--; |
---|
143 | } |
---|
144 | x++; |
---|
145 | ellipsepoints(qq, xo, yo, x, y, c); |
---|
146 | } |
---|
147 | |
---|
148 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
149 | while(y > 0) |
---|
150 | { |
---|
151 | if(d2 < 0) |
---|
152 | { |
---|
153 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
154 | x++; |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | d2 += a*a*(-2*y+3); |
---|
159 | } |
---|
160 | |
---|
161 | y--; |
---|
162 | ellipsepoints(qq, xo, yo, x, y, c); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * \brief Draw a thin ellipse on the screen. |
---|
168 | * |
---|
169 | * \param xo Center X coordinate. |
---|
170 | * \param yo Center Y coordinate. |
---|
171 | * \param a Ellipse X radius. |
---|
172 | * \param b Ellipse Y radius. |
---|
173 | * \return void |
---|
174 | */ |
---|
175 | void cucul_draw_thin_ellipse(cucul_t *qq, int xo, int yo, int a, int b) |
---|
176 | { |
---|
177 | /* FIXME: this is not correct */ |
---|
178 | int d2; |
---|
179 | int x = 0; |
---|
180 | int y = b; |
---|
181 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
182 | |
---|
183 | ellipsepoints(qq, xo, yo, x, y, '-'); |
---|
184 | |
---|
185 | while(a*a*y - a*a/2 > b*b*(x+1)) |
---|
186 | { |
---|
187 | if(d1 < 0) |
---|
188 | { |
---|
189 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
194 | y--; |
---|
195 | } |
---|
196 | x++; |
---|
197 | ellipsepoints(qq, xo, yo, x, y, '-'); |
---|
198 | } |
---|
199 | |
---|
200 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
201 | while(y > 0) |
---|
202 | { |
---|
203 | if(d2 < 0) |
---|
204 | { |
---|
205 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
206 | x++; |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | d2 += a*a*(-2*y+3); |
---|
211 | } |
---|
212 | |
---|
213 | y--; |
---|
214 | ellipsepoints(qq, xo, yo, x, y, '|'); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | static void ellipsepoints(cucul_t *qq, int xo, int yo, int x, int y, uint32_t c) |
---|
219 | { |
---|
220 | uint8_t b = 0; |
---|
221 | |
---|
222 | if(xo + x >= 0 && xo + x < (int)qq->width) |
---|
223 | b |= 0x1; |
---|
224 | if(xo - x >= 0 && xo - x < (int)qq->width) |
---|
225 | b |= 0x2; |
---|
226 | if(yo + y >= 0 && yo + y < (int)qq->height) |
---|
227 | b |= 0x4; |
---|
228 | if(yo - y >= 0 && yo - y < (int)qq->height) |
---|
229 | b |= 0x8; |
---|
230 | |
---|
231 | if((b & (0x1|0x4)) == (0x1|0x4)) |
---|
232 | _cucul_putchar32(qq, xo + x, yo + y, c); |
---|
233 | |
---|
234 | if((b & (0x2|0x4)) == (0x2|0x4)) |
---|
235 | _cucul_putchar32(qq, xo - x, yo + y, c); |
---|
236 | |
---|
237 | if((b & (0x1|0x8)) == (0x1|0x8)) |
---|
238 | _cucul_putchar32(qq, xo + x, yo - y, c); |
---|
239 | |
---|
240 | if((b & (0x2|0x8)) == (0x2|0x8)) |
---|
241 | _cucul_putchar32(qq, xo - x, yo - y, c); |
---|
242 | } |
---|
243 | |
---|