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