1 | /* |
---|
2 | * libee ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: conic.c 159 2003-11-12 21:18:50Z sam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or modify |
---|
9 | * it under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; either version 2 of the License, or |
---|
11 | * (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "config.h" |
---|
24 | |
---|
25 | #ifdef HAVE_INTTYPES_H |
---|
26 | # include <inttypes.h> |
---|
27 | #else |
---|
28 | typedef unsigned char uint8_t; |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <stdlib.h> |
---|
32 | |
---|
33 | #include "ee.h" |
---|
34 | #include "ee_internals.h" |
---|
35 | |
---|
36 | static void ellipsepoints(int, int, int, int, char); |
---|
37 | |
---|
38 | void ee_draw_circle(int x, int y, int r, char c) |
---|
39 | { |
---|
40 | int test, dx, dy; |
---|
41 | |
---|
42 | /* Optimized Bresenham. Kick ass. */ |
---|
43 | for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++) |
---|
44 | { |
---|
45 | ellipsepoints(x, y, dx, dy, c); |
---|
46 | ellipsepoints(x, y, dy, dx, c); |
---|
47 | |
---|
48 | test += test > 0 ? dx - dy-- : dx; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | void ee_fill_ellipse(int xo, int yo, int a, int b, char c) |
---|
53 | { |
---|
54 | int d2; |
---|
55 | int x = 0; |
---|
56 | int y = b; |
---|
57 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
58 | |
---|
59 | while( a*a*y - a*a/2 > b*b*(x+1)) |
---|
60 | { |
---|
61 | if(d1 < 0) |
---|
62 | { |
---|
63 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
64 | } |
---|
65 | else |
---|
66 | { |
---|
67 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
68 | ee_draw_line(xo - x, yo - y, xo + x, yo - y, c); |
---|
69 | ee_draw_line(xo - x, yo + y, xo + x, yo + y, c); |
---|
70 | y--; |
---|
71 | } |
---|
72 | x++; |
---|
73 | } |
---|
74 | |
---|
75 | ee_draw_line(xo - x, yo - y, xo + x, yo - y, c); |
---|
76 | ee_draw_line(xo - x, yo + y, xo + x, yo + y, c); |
---|
77 | |
---|
78 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
79 | while(y > 0) |
---|
80 | { |
---|
81 | if(d2 < 0) |
---|
82 | { |
---|
83 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
84 | x++; |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | d2 += a*a*(-2*y+3); |
---|
89 | } |
---|
90 | |
---|
91 | y--; |
---|
92 | ee_draw_line(xo - x, yo - y, xo + x, yo - y, c); |
---|
93 | ee_draw_line(xo - x, yo + y, xo + x, yo + y, c); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | void ee_draw_ellipse(int xo, int yo, int a, int b, char c) |
---|
98 | { |
---|
99 | int d2; |
---|
100 | int x = 0; |
---|
101 | int y = b; |
---|
102 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
103 | |
---|
104 | ellipsepoints(xo, yo, x, y, c); |
---|
105 | |
---|
106 | while( a*a*y - a*a/2 > b*b*(x+1)) |
---|
107 | { |
---|
108 | if(d1 < 0) |
---|
109 | { |
---|
110 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
115 | y--; |
---|
116 | } |
---|
117 | x++; |
---|
118 | ellipsepoints(xo, yo, x, y, c); |
---|
119 | } |
---|
120 | |
---|
121 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
122 | while(y > 0) |
---|
123 | { |
---|
124 | if(d2 < 0) |
---|
125 | { |
---|
126 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
127 | x++; |
---|
128 | } |
---|
129 | else |
---|
130 | { |
---|
131 | d2 += a*a*(-2*y+3); |
---|
132 | } |
---|
133 | |
---|
134 | y--; |
---|
135 | ellipsepoints(xo, yo, x, y, c); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | void ee_draw_thin_ellipse(int xo, int yo, int a, int b) |
---|
140 | { |
---|
141 | /* FIXME: this is not correct */ |
---|
142 | int d2; |
---|
143 | int x = 0; |
---|
144 | int y = b; |
---|
145 | int d1 = b*b - (a*a*b) + (a*a/4); |
---|
146 | |
---|
147 | ellipsepoints(xo, yo, x, y, '-'); |
---|
148 | |
---|
149 | while( a*a*y - a*a/2 > b*b*(x+1)) |
---|
150 | { |
---|
151 | if(d1 < 0) |
---|
152 | { |
---|
153 | d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ |
---|
154 | } |
---|
155 | else |
---|
156 | { |
---|
157 | d1 += b*b*(2*x*1) + a*a*(-2*y+2); |
---|
158 | y--; |
---|
159 | } |
---|
160 | x++; |
---|
161 | ellipsepoints(xo, yo, x, y, '-'); |
---|
162 | } |
---|
163 | |
---|
164 | d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; |
---|
165 | while(y > 0) |
---|
166 | { |
---|
167 | if(d2 < 0) |
---|
168 | { |
---|
169 | d2 += b*b*(2*x+2) + a*a*(-2*y+3); |
---|
170 | x++; |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | d2 += a*a*(-2*y+3); |
---|
175 | } |
---|
176 | |
---|
177 | y--; |
---|
178 | ellipsepoints(xo, yo, x, y, '|'); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | static void ellipsepoints(int xo, int yo, int x, int y, char c) |
---|
183 | { |
---|
184 | uint8_t b = 0; |
---|
185 | |
---|
186 | if(xo + x >= 0 && xo + x < ee_get_width()) |
---|
187 | b |= 0x1; |
---|
188 | if(xo - x >= 0 && xo - x < ee_get_width()) |
---|
189 | b |= 0x2; |
---|
190 | if(yo + y >= 0 && yo + y < ee_get_height()) |
---|
191 | b |= 0x4; |
---|
192 | if(yo - y >= 0 && yo - y < ee_get_height()) |
---|
193 | b |= 0x8; |
---|
194 | |
---|
195 | if((b & (0x1|0x4)) == (0x1|0x4)) |
---|
196 | ee_putchar(xo + x, yo + y, c); |
---|
197 | |
---|
198 | if((b & (0x2|0x4)) == (0x2|0x4)) |
---|
199 | ee_putchar(xo - x, yo + y, c); |
---|
200 | |
---|
201 | if((b & (0x1|0x8)) == (0x1|0x8)) |
---|
202 | ee_putchar(xo + x, yo - y, c); |
---|
203 | |
---|
204 | if((b & (0x2|0x8)) == (0x2|0x8)) |
---|
205 | ee_putchar(xo - x, yo - y, c); |
---|
206 | } |
---|
207 | |
---|