source: libcaca/trunk/cucul/line.c @ 1947

Last change on this file since 1947 was 1947, checked in by Pascal Terjan, 12 years ago

Fix a typo in the doc

  • Property svn:keywords set to Id
File size: 9.3 KB
Line 
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 *  $Id: line.c 1947 2007-11-13 12:41:33Z pterjan $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains line and polyline drawing functions, with both thin
17 *  and thick styles.
18 */
19
20#include "config.h"
21#include "common.h"
22
23#if !defined(__KERNEL__)
24#   include <stdlib.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30#if !defined(_DOXYGEN_SKIP_ME)
31struct line
32{
33    int x1, y1;
34    int x2, y2;
35    uint32_t ch;
36    void (*draw) (cucul_canvas_t *, struct line*);
37};
38#endif
39
40static void clip_line(cucul_canvas_t*, struct line*);
41static uint8_t clip_bits(cucul_canvas_t*, int, int);
42static void draw_solid_line(cucul_canvas_t*, struct line*);
43static void draw_thin_line(cucul_canvas_t*, struct line*);
44
45/** \brief Draw a line on the canvas using the given character.
46 *
47 *  This function never fails.
48 *
49 *  \param cv The handle to the libcucul canvas.
50 *  \param x1 X coordinate of the first point.
51 *  \param y1 Y coordinate of the first point.
52 *  \param x2 X coordinate of the second point.
53 *  \param y2 Y coordinate of the second point.
54 *  \param ch UTF-32 character to be used to draw the line.
55 *  \return This function always returns 0.
56 */
57int cucul_draw_line(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
58                    unsigned long int ch)
59{
60    struct line s;
61    s.x1 = x1;
62    s.y1 = y1;
63    s.x2 = x2;
64    s.y2 = y2;
65    s.ch = ch;
66    s.draw = draw_solid_line;
67    clip_line(cv, &s);
68
69    return 0;
70}
71
72/** \brief Draw a polyline.
73 *
74 *  Draw a polyline on the canvas using the given character and coordinate
75 *  arrays. The first and last points are not connected, hence in order to
76 *  draw a polygon you need to specify the starting point at the end of the
77 *  list as well.
78 *
79 *  This function never fails.
80 *
81 *  \param cv The handle to the libcucul canvas.
82 *  \param x Array of X coordinates. Must have \p n + 1 elements.
83 *  \param y Array of Y coordinates. Must have \p n + 1 elements.
84 *  \param n Number of lines to draw.
85 *  \param ch UTF-32 character to be used to draw the lines.
86 *  \return This function always returns 0.
87 */
88int cucul_draw_polyline(cucul_canvas_t *cv, int const x[], int const y[],
89                        int n, unsigned long int ch)
90{
91    int i;
92    struct line s;
93    s.ch = ch;
94    s.draw = draw_solid_line;
95
96    for(i = 0; i < n; i++)
97    {
98        s.x1 = x[i];
99        s.y1 = y[i];
100        s.x2 = x[i+1];
101        s.y2 = y[i+1];
102        clip_line(cv, &s);
103    }
104
105    return 0;
106}
107
108/** \brief Draw a thin line on the canvas, using ASCII art.
109 *
110 *  This function never fails.
111 *
112 *  \param cv The handle to the libcucul canvas.
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 This function always returns 0.
118 */
119int cucul_draw_thin_line(cucul_canvas_t *cv, 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(cv, &s);
128
129    return 0;
130}
131
132/** \brief Draw an ASCII art thin polyline.
133 *
134 *  Draw a thin polyline on the canvas using the given coordinate arrays and
135 *  with ASCII art. The first and last points are not connected, so in order
136 *  to draw a polygon you need to specify the starting point at the end of
137 *  the list as well.
138 *
139 *  This function never fails.
140 *
141 *  \param cv The handle to the libcucul canvas.
142 *  \param x Array of X coordinates. Must have \p n + 1 elements.
143 *  \param y Array of Y coordinates. Must have \p n + 1 elements.
144 *  \param n Number of lines to draw.
145 *  \return This function always returns 0.
146 */
147int cucul_draw_thin_polyline(cucul_canvas_t *cv, int const x[], int const y[],
148                             int n)
149{
150    int i;
151    struct line s;
152    s.draw = draw_thin_line;
153
154    for(i = 0; i < n; i++)
155    {
156        s.x1 = x[i];
157        s.y1 = y[i];
158        s.x2 = x[i+1];
159        s.y2 = y[i+1];
160        clip_line(cv, &s);
161    }
162
163    return 0;
164}
165
166/*
167 * XXX: The following functions are local.
168 */
169
170/* Generic Cohen-Sutherland line clipping function. */
171static void clip_line(cucul_canvas_t *cv, struct line* s)
172{
173    uint8_t bits1, bits2;
174
175    bits1 = clip_bits(cv, s->x1, s->y1);
176    bits2 = clip_bits(cv, s->x2, s->y2);
177
178    if(bits1 & bits2)
179        return;
180
181    if(bits1 == 0)
182    {
183        if(bits2 == 0)
184            s->draw(cv, s);
185        else
186        {
187            int tmp;
188            tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
189            tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
190            clip_line(cv, s);
191        }
192
193        return;
194    }
195
196    if(bits1 & (1<<0))
197    {
198        s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
199        s->x1 = 0;
200    }
201    else if(bits1 & (1<<1))
202    {
203        int xmax = cv->width - 1;
204        s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
205        s->x1 = xmax;
206    }
207    else if(bits1 & (1<<2))
208    {
209        s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
210        s->y1 = 0;
211    }
212    else if(bits1 & (1<<3))
213    {
214        int ymax = cv->height - 1;
215        s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
216        s->y1 = ymax;
217    }
218
219    clip_line(cv, s);
220}
221
222/* Helper function for clip_line(). */
223static uint8_t clip_bits(cucul_canvas_t *cv, int x, int y)
224{
225    uint8_t b = 0;
226
227    if(x < 0)
228        b |= (1<<0);
229    else if(x >= (int)cv->width)
230        b |= (1<<1);
231
232    if(y < 0)
233        b |= (1<<2);
234    else if(y >= (int)cv->height)
235        b |= (1<<3);
236
237    return b;
238}
239
240/* Solid line drawing function, using Bresenham's mid-point line
241 * scan-conversion algorithm. */
242static void draw_solid_line(cucul_canvas_t *cv, struct line* s)
243{
244    int x1, y1, x2, y2;
245    int dx, dy;
246    int xinc, yinc;
247
248    x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
249
250    dx = abs(x2 - x1);
251    dy = abs(y2 - y1);
252
253    xinc = (x1 > x2) ? -1 : 1;
254    yinc = (y1 > y2) ? -1 : 1;
255
256    if(dx >= dy)
257    {
258        int dpr = dy << 1;
259        int dpru = dpr - (dx << 1);
260        int delta = dpr - dx;
261
262        for(; dx>=0; dx--)
263        {
264            cucul_put_char(cv, x1, y1, s->ch);
265            if(delta > 0)
266            {
267                x1 += xinc;
268                y1 += yinc;
269                delta += dpru;
270            }
271            else
272            {
273                x1 += xinc;
274                delta += dpr;
275            }
276        }
277    }
278    else
279    {
280        int dpr = dx << 1;
281        int dpru = dpr - (dy << 1);
282        int delta = dpr - dy;
283
284        for(; dy >= 0; dy--)
285        {
286            cucul_put_char(cv, x1, y1, s->ch);
287            if(delta > 0)
288            {
289                x1 += xinc;
290                y1 += yinc;
291                delta += dpru;
292            }
293            else
294            {
295                y1 += yinc;
296                delta += dpr;
297            }
298        }
299    }
300}
301
302/* Thin line drawing function, using Bresenham's mid-point line
303 * scan-conversion algorithm and ASCII art graphics. */
304static void draw_thin_line(cucul_canvas_t *cv, struct line* s)
305{
306    uint32_t charmapx[2], charmapy[2];
307    int x1, y1, x2, y2;
308    int dx, dy;
309    int yinc;
310
311    if(s->x2 >= s->x1)
312    {
313        charmapx[0] = (s->y1 > s->y2) ? ',' : '`';
314        charmapx[1] = (s->y1 > s->y2) ? '\'' : '.';
315        x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
316    }
317    else
318    {
319        charmapx[0] = (s->y1 > s->y2) ? '`' : '.';
320        charmapx[1] = (s->y1 > s->y2) ? ',' : '\'';
321        x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
322    }
323
324    dx = abs(x2 - x1);
325    dy = abs(y2 - y1);
326
327    if(y1 > y2)
328    {
329        charmapy[0] = ',';
330        charmapy[1] = '\'';
331        yinc = -1;
332    }
333    else
334    {
335        yinc = 1;
336        charmapy[0] = '`';
337        charmapy[1] = '.';
338    }
339
340    if(dx >= dy)
341    {
342        int dpr = dy << 1;
343        int dpru = dpr - (dx << 1);
344        int delta = dpr - dx;
345        int prev = 0;
346
347        for(; dx>=0; dx--)
348        {
349            if(delta > 0)
350            {
351                cucul_put_char(cv, x1, y1, charmapy[1]);
352                x1++;
353                y1 += yinc;
354                delta += dpru;
355                prev = 1;
356            }
357            else
358            {
359                if(prev)
360                    cucul_put_char(cv, x1, y1, charmapy[0]);
361                else
362                    cucul_put_char(cv, x1, y1, '-');
363                x1++;
364                delta += dpr;
365                prev = 0;
366            }
367        }
368    }
369    else
370    {
371        int dpr = dx << 1;
372        int dpru = dpr - (dy << 1);
373        int delta = dpr - dy;
374
375        for(; dy >= 0; dy--)
376        {
377            if(delta > 0)
378            {
379                cucul_put_char(cv, x1, y1, charmapx[0]);
380                cucul_put_char(cv, x1 + 1, y1, charmapx[1]);
381                x1++;
382                y1 += yinc;
383                delta += dpru;
384            }
385            else
386            {
387                cucul_put_char(cv, x1, y1, '|');
388                y1 += yinc;
389                delta += dpr;
390            }
391        }
392    }
393}
394
Note: See TracBrowser for help on using the repository browser.