1 | /* |
---|
2 | * libee ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: line.c 147 2003-11-10 23:38: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 USE_SLANG |
---|
26 | # include <slang.h> |
---|
27 | #elif USE_NCURSES |
---|
28 | # include <curses.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <inttypes.h> |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | #include "ee.h" |
---|
35 | |
---|
36 | struct line |
---|
37 | { |
---|
38 | int x1, y1; |
---|
39 | int x2, y2; |
---|
40 | char c; |
---|
41 | void (*draw) (struct line*); |
---|
42 | }; |
---|
43 | |
---|
44 | static void clip_line(struct line*); |
---|
45 | static uint8_t clip_bits(int, int); |
---|
46 | static void draw_solid_line(struct line*); |
---|
47 | static void draw_thin_line(struct line*); |
---|
48 | |
---|
49 | /** |
---|
50 | * \brief Draw a line on the screen using the given character. |
---|
51 | * |
---|
52 | * \param x1 X coordinate of the first point. |
---|
53 | * \param y1 Y coordinate of the first point. |
---|
54 | * \param x2 X coordinate of the second point. |
---|
55 | * \param y2 Y coordinate of the second point. |
---|
56 | * \param c Character to draw the line with. |
---|
57 | * \return nothing |
---|
58 | */ |
---|
59 | void ee_draw_line(int x1, int y1, int x2, int y2, char c) |
---|
60 | { |
---|
61 | struct line s; |
---|
62 | s.x1 = x1; |
---|
63 | s.y1 = y1; |
---|
64 | s.x2 = x2; |
---|
65 | s.y2 = y2; |
---|
66 | s.c = c; |
---|
67 | s.draw = draw_solid_line; |
---|
68 | clip_line(&s); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * \brief Draw a thin line on the screen, using ASCII art. |
---|
73 | * |
---|
74 | * \param x1 X coordinate of the first point. |
---|
75 | * \param y1 Y coordinate of the first point. |
---|
76 | * \param x2 X coordinate of the second point. |
---|
77 | * \param y2 Y coordinate of the second point. |
---|
78 | * \return nothing |
---|
79 | */ |
---|
80 | void ee_draw_thin_line(int x1, int y1, int x2, int y2) |
---|
81 | { |
---|
82 | struct line s; |
---|
83 | s.x1 = x1; |
---|
84 | s.y1 = y1; |
---|
85 | s.x2 = x2; |
---|
86 | s.y2 = y2; |
---|
87 | s.draw = draw_thin_line; |
---|
88 | clip_line(&s); |
---|
89 | } |
---|
90 | |
---|
91 | /* |
---|
92 | * XXX: The following functions are local. |
---|
93 | */ |
---|
94 | |
---|
95 | /** |
---|
96 | * \brief Generic Cohen-Sutherland line clipping function. |
---|
97 | * |
---|
98 | * \param s a line structure |
---|
99 | * \return nothing |
---|
100 | */ |
---|
101 | static void clip_line(struct line* s) |
---|
102 | { |
---|
103 | uint8_t bits1, bits2; |
---|
104 | |
---|
105 | bits1 = clip_bits(s->x1, s->y1); |
---|
106 | bits2 = clip_bits(s->x2, s->y2); |
---|
107 | |
---|
108 | if(bits1 & bits2) |
---|
109 | return; |
---|
110 | |
---|
111 | if(bits1 == 0) |
---|
112 | { |
---|
113 | if(bits2 == 0) |
---|
114 | s->draw(s); |
---|
115 | else |
---|
116 | { |
---|
117 | int tmp; |
---|
118 | tmp = s->x1; s->x1 = s->x2; s->x2 = tmp; |
---|
119 | tmp = s->y1; s->y1 = s->y2; s->y2 = tmp; |
---|
120 | clip_line(s); |
---|
121 | } |
---|
122 | |
---|
123 | return; |
---|
124 | } |
---|
125 | |
---|
126 | if(bits1 & (1<<0)) |
---|
127 | { |
---|
128 | s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1); |
---|
129 | s->x1 = 0; |
---|
130 | } |
---|
131 | else if( bits1 & (1<<1) ) |
---|
132 | { |
---|
133 | int xmax = ee_get_width() - 1; |
---|
134 | s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1); |
---|
135 | s->x1 = xmax; |
---|
136 | } |
---|
137 | else if( bits1 & (1<<2) ) |
---|
138 | { |
---|
139 | s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1); |
---|
140 | s->y1 = 0; |
---|
141 | } |
---|
142 | else if( bits1 & (1<<3) ) |
---|
143 | { |
---|
144 | int ymax = ee_get_height() - 1; |
---|
145 | s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1); |
---|
146 | s->y1 = ymax; |
---|
147 | } |
---|
148 | |
---|
149 | clip_line(s); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * \brief Helper function for clip_line(). |
---|
154 | * |
---|
155 | * \param x X coordinate of the point. |
---|
156 | * \param y Y coordinate of the point. |
---|
157 | * \return b The clipping bits for the given point. |
---|
158 | */ |
---|
159 | static uint8_t clip_bits(int x, int y) |
---|
160 | { |
---|
161 | uint8_t b = 0; |
---|
162 | |
---|
163 | if(x < 0) |
---|
164 | b |= (1<<0); |
---|
165 | else if(x >= ee_get_width()) |
---|
166 | b |= (1<<1); |
---|
167 | |
---|
168 | if(y < 0) |
---|
169 | b |= (1<<2); |
---|
170 | else if(y >= ee_get_height()) |
---|
171 | b |= (1<<3); |
---|
172 | |
---|
173 | return b; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * \brief Solid line drawing function, using Bresenham's mid-point line |
---|
178 | * scan-conversion algorithm. |
---|
179 | * |
---|
180 | * \param s a line structure |
---|
181 | * \return nothing |
---|
182 | */ |
---|
183 | static void draw_solid_line(struct line* s) |
---|
184 | { |
---|
185 | int x1, y1, x2, y2; |
---|
186 | int dx, dy; |
---|
187 | int xinc, yinc; |
---|
188 | |
---|
189 | x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2; |
---|
190 | |
---|
191 | dx = abs(x2 - x1); |
---|
192 | dy = abs(y2 - y1); |
---|
193 | |
---|
194 | xinc = (x1 > x2) ? -1 : 1; |
---|
195 | yinc = (y1 > y2) ? -1 : 1; |
---|
196 | |
---|
197 | if(dx >= dy) |
---|
198 | { |
---|
199 | int dpr = dy << 1; |
---|
200 | int dpru = dpr - (dx << 1); |
---|
201 | int delta = dpr - dx; |
---|
202 | |
---|
203 | for(; dx>=0; dx--) |
---|
204 | { |
---|
205 | ee_putchar(x1, y1, s->c); |
---|
206 | if(delta > 0) |
---|
207 | { |
---|
208 | x1 += xinc; |
---|
209 | y1 += yinc; |
---|
210 | delta += dpru; |
---|
211 | } |
---|
212 | else |
---|
213 | { |
---|
214 | x1 += xinc; |
---|
215 | delta += dpr; |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | else |
---|
220 | { |
---|
221 | int dpr = dx << 1; |
---|
222 | int dpru = dpr - (dy << 1); |
---|
223 | int delta = dpr - dy; |
---|
224 | |
---|
225 | for(; dy >= 0; dy--) |
---|
226 | { |
---|
227 | ee_putchar(x1, y1, s->c); |
---|
228 | if(delta > 0) |
---|
229 | { |
---|
230 | x1 += xinc; |
---|
231 | y1 += yinc; |
---|
232 | delta += dpru; |
---|
233 | } |
---|
234 | else |
---|
235 | { |
---|
236 | y1 += yinc; |
---|
237 | delta += dpr; |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * \brief Thin line drawing function, using Bresenham's mid-point line |
---|
245 | * scan-conversion algorithm and ASCII art graphics. |
---|
246 | * |
---|
247 | * \param s a line structure |
---|
248 | * \return nothing |
---|
249 | */ |
---|
250 | static void draw_thin_line(struct line* s) |
---|
251 | { |
---|
252 | char *charmapx, *charmapy; |
---|
253 | int x1, y1, x2, y2; |
---|
254 | int dx, dy; |
---|
255 | int yinc; |
---|
256 | |
---|
257 | if(s->x2 >= s->x1) |
---|
258 | { |
---|
259 | if(s->y1 > s->y2) |
---|
260 | charmapx = ",'"; |
---|
261 | else |
---|
262 | charmapx = "`."; |
---|
263 | x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2; |
---|
264 | } |
---|
265 | else |
---|
266 | { |
---|
267 | if(s->y1 > s->y2) |
---|
268 | charmapx = "`."; |
---|
269 | else |
---|
270 | charmapx = ",'"; |
---|
271 | x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2; |
---|
272 | } |
---|
273 | |
---|
274 | dx = abs(x2 - x1); |
---|
275 | dy = abs(y2 - y1); |
---|
276 | |
---|
277 | if(y1 > y2) |
---|
278 | { |
---|
279 | charmapy = ",'"; |
---|
280 | yinc = -1; |
---|
281 | } |
---|
282 | else |
---|
283 | { |
---|
284 | yinc = 1; |
---|
285 | charmapy = "`."; |
---|
286 | } |
---|
287 | |
---|
288 | if(dx >= dy) |
---|
289 | { |
---|
290 | int dpr = dy << 1; |
---|
291 | int dpru = dpr - (dx << 1); |
---|
292 | int delta = dpr - dx; |
---|
293 | int prev = 0; |
---|
294 | |
---|
295 | for(; dx>=0; dx--) |
---|
296 | { |
---|
297 | if(delta > 0) |
---|
298 | { |
---|
299 | ee_putchar(x1, y1, charmapy[1]); |
---|
300 | x1++; |
---|
301 | y1 += yinc; |
---|
302 | delta += dpru; |
---|
303 | prev = 1; |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | if(prev) |
---|
308 | ee_putchar(x1, y1, charmapy[0]); |
---|
309 | else |
---|
310 | ee_putchar(x1, y1, '-'); |
---|
311 | x1++; |
---|
312 | delta += dpr; |
---|
313 | prev = 0; |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | else |
---|
318 | { |
---|
319 | int dpr = dx << 1; |
---|
320 | int dpru = dpr - (dy << 1); |
---|
321 | int delta = dpr - dy; |
---|
322 | |
---|
323 | for(; dy >= 0; dy--) |
---|
324 | { |
---|
325 | if(delta > 0) |
---|
326 | { |
---|
327 | ee_putchar(x1, y1, charmapx[0]); |
---|
328 | ee_putchar(x1 + 1, y1, charmapx[1]); |
---|
329 | x1++; |
---|
330 | y1 += yinc; |
---|
331 | delta += dpru; |
---|
332 | } |
---|
333 | else |
---|
334 | { |
---|
335 | ee_putchar(x1, y1, '|'); |
---|
336 | y1 += yinc; |
---|
337 | delta += dpr; |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|