1 | /* |
---|
2 | * libcaca ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 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 GNU Lesser General Public |
---|
8 | * License as published by the Free Software Foundation; either |
---|
9 | * version 2 of the License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This library is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * Lesser General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU Lesser General Public |
---|
17 | * License along with this library; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
19 | * 02111-1307 USA |
---|
20 | */ |
---|
21 | |
---|
22 | /** \file line.c |
---|
23 | * \version \$Id: line.c 322 2004-01-07 12:45:02Z sam $ |
---|
24 | * \author Sam Hocevar <sam@zoy.org> |
---|
25 | * \brief Line drawing |
---|
26 | * |
---|
27 | * This file contains line and polyline drawing functions, with both thin |
---|
28 | * and thick styles. |
---|
29 | */ |
---|
30 | |
---|
31 | #include "config.h" |
---|
32 | |
---|
33 | #if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME) |
---|
34 | # include <inttypes.h> |
---|
35 | #else |
---|
36 | typedef unsigned char uint8_t; |
---|
37 | #endif |
---|
38 | |
---|
39 | #include <stdlib.h> |
---|
40 | |
---|
41 | #include "caca.h" |
---|
42 | #include "caca_internals.h" |
---|
43 | |
---|
44 | #if !defined(_DOXYGEN_SKIP_ME) |
---|
45 | struct line |
---|
46 | { |
---|
47 | int x1, y1; |
---|
48 | int x2, y2; |
---|
49 | char c; |
---|
50 | void (*draw) (struct line*); |
---|
51 | }; |
---|
52 | #endif |
---|
53 | |
---|
54 | static void clip_line(struct line*); |
---|
55 | static uint8_t clip_bits(int, int); |
---|
56 | static void draw_solid_line(struct line*); |
---|
57 | static void draw_thin_line(struct line*); |
---|
58 | |
---|
59 | /** |
---|
60 | * \brief Draw a line on the screen using the given character. |
---|
61 | * |
---|
62 | * \param x1 X coordinate of the first point. |
---|
63 | * \param y1 Y coordinate of the first point. |
---|
64 | * \param x2 X coordinate of the second point. |
---|
65 | * \param y2 Y coordinate of the second point. |
---|
66 | * \param c Character to draw the line with. |
---|
67 | * \return void |
---|
68 | */ |
---|
69 | void caca_draw_line(int x1, int y1, int x2, int y2, char c) |
---|
70 | { |
---|
71 | struct line s; |
---|
72 | s.x1 = x1; |
---|
73 | s.y1 = y1; |
---|
74 | s.x2 = x2; |
---|
75 | s.y2 = y2; |
---|
76 | s.c = c; |
---|
77 | s.draw = draw_solid_line; |
---|
78 | clip_line(&s); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * \brief Draw a polyline on the screen using the given character and |
---|
83 | * coordinate arrays. The first and last points are not connected, |
---|
84 | * so in order to draw a polygon you need to specify the starting |
---|
85 | * point at the end of the list as well. |
---|
86 | * |
---|
87 | * \param x Array of X coordinates. Must have \p n + 1 elements. |
---|
88 | * \param y Array of Y coordinates. Must have \p n + 1 elements. |
---|
89 | * \param n Number of lines to draw. |
---|
90 | * \param c Character to draw the lines with. |
---|
91 | * \return void |
---|
92 | */ |
---|
93 | void caca_draw_polyline(int const x[], int const y[], int n, char c) |
---|
94 | { |
---|
95 | int i; |
---|
96 | struct line s; |
---|
97 | s.c = c; |
---|
98 | s.draw = draw_solid_line; |
---|
99 | |
---|
100 | for(i = 0; i < n; i++) |
---|
101 | { |
---|
102 | s.x1 = x[i]; |
---|
103 | s.y1 = y[i]; |
---|
104 | s.x2 = x[i+1]; |
---|
105 | s.y2 = y[i+1]; |
---|
106 | clip_line(&s); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * \brief Draw a thin line on the screen, using ASCII art. |
---|
112 | * |
---|
113 | * \param x1 X coordinate of the first point. |
---|
114 | * \param y1 Y coordinate of the first point. |
---|
115 | * \param x2 X coordinate of the second point. |
---|
116 | * \param y2 Y coordinate of the second point. |
---|
117 | * \return void |
---|
118 | */ |
---|
119 | void caca_draw_thin_line(int x1, int y1, int x2, int y2) |
---|
120 | { |
---|
121 | struct line s; |
---|
122 | s.x1 = x1; |
---|
123 | s.y1 = y1; |
---|
124 | s.x2 = x2; |
---|
125 | s.y2 = y2; |
---|
126 | s.draw = draw_thin_line; |
---|
127 | clip_line(&s); |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * \brief Draw a thin polyline on the screen using the given coordinate |
---|
132 | * arrays and with ASCII art. The first and last points are not |
---|
133 | * connected, so in order to draw a polygon you need to specify the |
---|
134 | * starting point at the end of the list as well. |
---|
135 | * |
---|
136 | * \param x Array of X coordinates. Must have \p n + 1 elements. |
---|
137 | * \param y Array of Y coordinates. Must have \p n + 1 elements. |
---|
138 | * \param n Number of lines to draw. |
---|
139 | * \return void |
---|
140 | */ |
---|
141 | void caca_draw_thin_polyline(int const x[], int const y[], int n) |
---|
142 | { |
---|
143 | int i; |
---|
144 | struct line s; |
---|
145 | s.draw = draw_thin_line; |
---|
146 | |
---|
147 | for(i = 0; i < n; i++) |
---|
148 | { |
---|
149 | s.x1 = x[i]; |
---|
150 | s.y1 = y[i]; |
---|
151 | s.x2 = x[i+1]; |
---|
152 | s.y2 = y[i+1]; |
---|
153 | clip_line(&s); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | /* |
---|
158 | * XXX: The following functions are local. |
---|
159 | */ |
---|
160 | |
---|
161 | /** |
---|
162 | * \brief Generic Cohen-Sutherland line clipping function. |
---|
163 | * |
---|
164 | * \param s a line structure |
---|
165 | * \return void |
---|
166 | */ |
---|
167 | static void clip_line(struct line* s) |
---|
168 | { |
---|
169 | uint8_t bits1, bits2; |
---|
170 | |
---|
171 | bits1 = clip_bits(s->x1, s->y1); |
---|
172 | bits2 = clip_bits(s->x2, s->y2); |
---|
173 | |
---|
174 | if(bits1 & bits2) |
---|
175 | return; |
---|
176 | |
---|
177 | if(bits1 == 0) |
---|
178 | { |
---|
179 | if(bits2 == 0) |
---|
180 | s->draw(s); |
---|
181 | else |
---|
182 | { |
---|
183 | int tmp; |
---|
184 | tmp = s->x1; s->x1 = s->x2; s->x2 = tmp; |
---|
185 | tmp = s->y1; s->y1 = s->y2; s->y2 = tmp; |
---|
186 | clip_line(s); |
---|
187 | } |
---|
188 | |
---|
189 | return; |
---|
190 | } |
---|
191 | |
---|
192 | if(bits1 & (1<<0)) |
---|
193 | { |
---|
194 | s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1); |
---|
195 | s->x1 = 0; |
---|
196 | } |
---|
197 | else if(bits1 & (1<<1)) |
---|
198 | { |
---|
199 | int xmax = _caca_width - 1; |
---|
200 | s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1); |
---|
201 | s->x1 = xmax; |
---|
202 | } |
---|
203 | else if(bits1 & (1<<2)) |
---|
204 | { |
---|
205 | s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1); |
---|
206 | s->y1 = 0; |
---|
207 | } |
---|
208 | else if(bits1 & (1<<3)) |
---|
209 | { |
---|
210 | int ymax = _caca_height - 1; |
---|
211 | s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1); |
---|
212 | s->y1 = ymax; |
---|
213 | } |
---|
214 | |
---|
215 | clip_line(s); |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * \brief Helper function for clip_line(). |
---|
220 | * |
---|
221 | * \param x X coordinate of the point. |
---|
222 | * \param y Y coordinate of the point. |
---|
223 | * \return The clipping bits for the given point. |
---|
224 | */ |
---|
225 | static uint8_t clip_bits(int x, int y) |
---|
226 | { |
---|
227 | uint8_t b = 0; |
---|
228 | |
---|
229 | if(x < 0) |
---|
230 | b |= (1<<0); |
---|
231 | else if(x >= (int)_caca_width) |
---|
232 | b |= (1<<1); |
---|
233 | |
---|
234 | if(y < 0) |
---|
235 | b |= (1<<2); |
---|
236 | else if(y >= (int)_caca_height) |
---|
237 | b |= (1<<3); |
---|
238 | |
---|
239 | return b; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * \brief Solid line drawing function, using Bresenham's mid-point line |
---|
244 | * scan-conversion algorithm. |
---|
245 | * |
---|
246 | * \param s a line structure |
---|
247 | * \return void |
---|
248 | */ |
---|
249 | static void draw_solid_line(struct line* s) |
---|
250 | { |
---|
251 | int x1, y1, x2, y2; |
---|
252 | int dx, dy; |
---|
253 | int xinc, yinc; |
---|
254 | |
---|
255 | x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2; |
---|
256 | |
---|
257 | dx = abs(x2 - x1); |
---|
258 | dy = abs(y2 - y1); |
---|
259 | |
---|
260 | xinc = (x1 > x2) ? -1 : 1; |
---|
261 | yinc = (y1 > y2) ? -1 : 1; |
---|
262 | |
---|
263 | if(dx >= dy) |
---|
264 | { |
---|
265 | int dpr = dy << 1; |
---|
266 | int dpru = dpr - (dx << 1); |
---|
267 | int delta = dpr - dx; |
---|
268 | |
---|
269 | for(; dx>=0; dx--) |
---|
270 | { |
---|
271 | caca_putchar(x1, y1, s->c); |
---|
272 | if(delta > 0) |
---|
273 | { |
---|
274 | x1 += xinc; |
---|
275 | y1 += yinc; |
---|
276 | delta += dpru; |
---|
277 | } |
---|
278 | else |
---|
279 | { |
---|
280 | x1 += xinc; |
---|
281 | delta += dpr; |
---|
282 | } |
---|
283 | } |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | int dpr = dx << 1; |
---|
288 | int dpru = dpr - (dy << 1); |
---|
289 | int delta = dpr - dy; |
---|
290 | |
---|
291 | for(; dy >= 0; dy--) |
---|
292 | { |
---|
293 | caca_putchar(x1, y1, s->c); |
---|
294 | if(delta > 0) |
---|
295 | { |
---|
296 | x1 += xinc; |
---|
297 | y1 += yinc; |
---|
298 | delta += dpru; |
---|
299 | } |
---|
300 | else |
---|
301 | { |
---|
302 | y1 += yinc; |
---|
303 | delta += dpr; |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | /** |
---|
310 | * \brief Thin line drawing function, using Bresenham's mid-point line |
---|
311 | * scan-conversion algorithm and ASCII art graphics. |
---|
312 | * |
---|
313 | * \param s a line structure |
---|
314 | * \return void |
---|
315 | */ |
---|
316 | static void draw_thin_line(struct line* s) |
---|
317 | { |
---|
318 | char *charmapx, *charmapy; |
---|
319 | int x1, y1, x2, y2; |
---|
320 | int dx, dy; |
---|
321 | int yinc; |
---|
322 | |
---|
323 | if(s->x2 >= s->x1) |
---|
324 | { |
---|
325 | if(s->y1 > s->y2) |
---|
326 | charmapx = ",'"; |
---|
327 | else |
---|
328 | charmapx = "`."; |
---|
329 | x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2; |
---|
330 | } |
---|
331 | else |
---|
332 | { |
---|
333 | if(s->y1 > s->y2) |
---|
334 | charmapx = "`."; |
---|
335 | else |
---|
336 | charmapx = ",'"; |
---|
337 | x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2; |
---|
338 | } |
---|
339 | |
---|
340 | dx = abs(x2 - x1); |
---|
341 | dy = abs(y2 - y1); |
---|
342 | |
---|
343 | if(y1 > y2) |
---|
344 | { |
---|
345 | charmapy = ",'"; |
---|
346 | yinc = -1; |
---|
347 | } |
---|
348 | else |
---|
349 | { |
---|
350 | yinc = 1; |
---|
351 | charmapy = "`."; |
---|
352 | } |
---|
353 | |
---|
354 | if(dx >= dy) |
---|
355 | { |
---|
356 | int dpr = dy << 1; |
---|
357 | int dpru = dpr - (dx << 1); |
---|
358 | int delta = dpr - dx; |
---|
359 | int prev = 0; |
---|
360 | |
---|
361 | for(; dx>=0; dx--) |
---|
362 | { |
---|
363 | if(delta > 0) |
---|
364 | { |
---|
365 | caca_putchar(x1, y1, charmapy[1]); |
---|
366 | x1++; |
---|
367 | y1 += yinc; |
---|
368 | delta += dpru; |
---|
369 | prev = 1; |
---|
370 | } |
---|
371 | else |
---|
372 | { |
---|
373 | if(prev) |
---|
374 | caca_putchar(x1, y1, charmapy[0]); |
---|
375 | else |
---|
376 | caca_putchar(x1, y1, '-'); |
---|
377 | x1++; |
---|
378 | delta += dpr; |
---|
379 | prev = 0; |
---|
380 | } |
---|
381 | } |
---|
382 | } |
---|
383 | else |
---|
384 | { |
---|
385 | int dpr = dx << 1; |
---|
386 | int dpru = dpr - (dy << 1); |
---|
387 | int delta = dpr - dy; |
---|
388 | |
---|
389 | for(; dy >= 0; dy--) |
---|
390 | { |
---|
391 | if(delta > 0) |
---|
392 | { |
---|
393 | caca_putchar(x1, y1, charmapx[0]); |
---|
394 | caca_putchar(x1 + 1, y1, charmapx[1]); |
---|
395 | x1++; |
---|
396 | y1 += yinc; |
---|
397 | delta += dpru; |
---|
398 | } |
---|
399 | else |
---|
400 | { |
---|
401 | caca_putchar(x1, y1, '|'); |
---|
402 | y1 += yinc; |
---|
403 | delta += dpr; |
---|
404 | } |
---|
405 | } |
---|
406 | } |
---|
407 | } |
---|
408 | |
---|