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

Last change on this file since 568 was 568, checked in by Sam Hocevar, 14 years ago
  • Added glue code to compile libcaca without a libc and build applications as multiboot kernels.
  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
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 line.c
13 *  \version \$Id: line.c 568 2006-03-09 12:47:37Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Line drawing
16 *
17 *  This file contains line and polyline drawing functions, with both thin
18 *  and thick styles.
19 */
20
21#include "config.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    char c;
36    void (*draw) (cucul_t *, struct line*);
37};
38#endif
39
40static void clip_line(cucul_t*, struct line*);
41static uint8_t clip_bits(cucul_t*, int, int);
42static void draw_solid_line(cucul_t*, struct line*);
43static void draw_thin_line(cucul_t*, struct line*);
44
45/**
46 * \brief Draw a line on the screen using the given character.
47 *
48 * \param x1 X coordinate of the first point.
49 * \param y1 Y coordinate of the first point.
50 * \param x2 X coordinate of the second point.
51 * \param y2 Y coordinate of the second point.
52 * \param c Character to draw the line with.
53 * \return void
54 */
55void cucul_draw_line(cucul_t *qq, int x1, int y1, int x2, int y2, char c)
56{
57    struct line s;
58    s.x1 = x1;
59    s.y1 = y1;
60    s.x2 = x2;
61    s.y2 = y2;
62    s.c = c;
63    s.draw = draw_solid_line;
64    clip_line(qq, &s);
65}
66
67/**
68 * \brief Draw a polyline on the screen using the given character and
69 *       coordinate arrays. The first and last points are not connected,
70 *       so in order to draw a polygon you need to specify the starting
71 *       point at the end of the list as well.
72 *
73 * \param x Array of X coordinates. Must have \p n + 1 elements.
74 * \param y Array of Y coordinates. Must have \p n + 1 elements.
75 * \param n Number of lines to draw.
76 * \param c Character to draw the lines with.
77 * \return void
78 */
79void cucul_draw_polyline(cucul_t *qq, int const x[], int const y[], int n, char c)
80{
81    int i;
82    struct line s;
83    s.c = c;
84    s.draw = draw_solid_line;
85
86    for(i = 0; i < n; i++)
87    {
88        s.x1 = x[i];
89        s.y1 = y[i];
90        s.x2 = x[i+1];
91        s.y2 = y[i+1];
92        clip_line(qq, &s);
93    }
94}
95
96/**
97 * \brief Draw a thin line on the screen, using ASCII art.
98 *
99 * \param x1 X coordinate of the first point.
100 * \param y1 Y coordinate of the first point.
101 * \param x2 X coordinate of the second point.
102 * \param y2 Y coordinate of the second point.
103 * \return void
104 */
105void cucul_draw_thin_line(cucul_t *qq, int x1, int y1, int x2, int y2)
106{
107    struct line s;
108    s.x1 = x1;
109    s.y1 = y1;
110    s.x2 = x2;
111    s.y2 = y2;
112    s.draw = draw_thin_line;
113    clip_line(qq, &s);
114}
115
116/**
117 * \brief Draw a thin polyline on the screen using the given coordinate
118 *       arrays and with ASCII art. The first and last points are not
119 *       connected, so in order to draw a polygon you need to specify the
120 *       starting point at the end of the list as well.
121 *
122 * \param x Array of X coordinates. Must have \p n + 1 elements.
123 * \param y Array of Y coordinates. Must have \p n + 1 elements.
124 * \param n Number of lines to draw.
125 * \return void
126 */
127void cucul_draw_thin_polyline(cucul_t *qq, int const x[], int const y[], int n)
128{
129    int i;
130    struct line s;
131    s.draw = draw_thin_line;
132
133    for(i = 0; i < n; i++)
134    {
135        s.x1 = x[i];
136        s.y1 = y[i];
137        s.x2 = x[i+1];
138        s.y2 = y[i+1];
139        clip_line(qq, &s);
140    }
141}
142
143/*
144 * XXX: The following functions are local.
145 */
146
147/**
148 * \brief Generic Cohen-Sutherland line clipping function.
149 *
150 * \param s a line structure
151 * \return void
152 */
153static void clip_line(cucul_t *qq, struct line* s)
154{
155    uint8_t bits1, bits2;
156
157    bits1 = clip_bits(qq, s->x1, s->y1);
158    bits2 = clip_bits(qq, s->x2, s->y2);
159
160    if(bits1 & bits2)
161        return;
162
163    if(bits1 == 0)
164    {
165        if(bits2 == 0)
166            s->draw(qq, s);
167        else
168        {
169            int tmp;
170            tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
171            tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
172            clip_line(qq, s);
173        }
174
175        return;
176    }
177
178    if(bits1 & (1<<0))
179    {
180        s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
181        s->x1 = 0;
182    }
183    else if(bits1 & (1<<1))
184    {
185        int xmax = qq->width - 1;
186        s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
187        s->x1 = xmax;
188    }
189    else if(bits1 & (1<<2))
190    {
191        s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
192        s->y1 = 0;
193    }
194    else if(bits1 & (1<<3))
195    {
196        int ymax = qq->height - 1;
197        s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
198        s->y1 = ymax;
199    }
200
201    clip_line(qq, s);
202}
203
204/**
205 * \brief Helper function for clip_line().
206 *
207 * \param x X coordinate of the point.
208 * \param y Y coordinate of the point.
209 * \return The clipping bits for the given point.
210 */
211static uint8_t clip_bits(cucul_t *qq, int x, int y)
212{
213    uint8_t b = 0;
214
215    if(x < 0)
216        b |= (1<<0);
217    else if(x >= (int)qq->width)
218        b |= (1<<1);
219
220    if(y < 0)
221        b |= (1<<2);
222    else if(y >= (int)qq->height)
223        b |= (1<<3);
224
225    return b;
226}
227
228/**
229 * \brief Solid line drawing function, using Bresenham's mid-point line
230 *       scan-conversion algorithm.
231 *
232 * \param s a line structure
233 * \return void
234 */
235static void draw_solid_line(cucul_t *qq, struct line* s)
236{
237    int x1, y1, x2, y2;
238    int dx, dy;
239    int xinc, yinc;
240
241    x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
242
243    dx = abs(x2 - x1);
244    dy = abs(y2 - y1);
245
246    xinc = (x1 > x2) ? -1 : 1;
247    yinc = (y1 > y2) ? -1 : 1;
248
249    if(dx >= dy)
250    {
251        int dpr = dy << 1;
252        int dpru = dpr - (dx << 1);
253        int delta = dpr - dx;
254
255        for(; dx>=0; dx--)
256        {
257            cucul_putchar(qq, x1, y1, s->c);
258            if(delta > 0)
259            {
260                x1 += xinc;
261                y1 += yinc;
262                delta += dpru;
263            }
264            else
265            {
266                x1 += xinc;
267                delta += dpr;
268            }
269        }
270    }
271    else
272    {
273        int dpr = dx << 1;
274        int dpru = dpr - (dy << 1);
275        int delta = dpr - dy;
276
277        for(; dy >= 0; dy--)
278        {
279            cucul_putchar(qq, x1, y1, s->c);
280            if(delta > 0)
281            {
282                x1 += xinc;
283                y1 += yinc;
284                delta += dpru;
285            }
286            else
287            {
288                y1 += yinc;
289                delta += dpr;
290            }
291        }
292    }
293}
294
295/**
296 * \brief Thin line drawing function, using Bresenham's mid-point line
297 *        scan-conversion algorithm and ASCII art graphics.
298 *
299 * \param s a line structure
300 * \return void
301 */
302static void draw_thin_line(cucul_t *qq, struct line* s)
303{
304    char *charmapx, *charmapy;
305    int x1, y1, x2, y2;
306    int dx, dy;
307    int yinc;
308
309    if(s->x2 >= s->x1)
310    {
311        if(s->y1 > s->y2)
312            charmapx = ",'";
313        else
314            charmapx = "`.";
315        x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
316    }
317    else
318    {
319        if(s->y1 > s->y2)
320            charmapx = "`.";
321        else
322            charmapx = ",'";
323        x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
324    }
325
326    dx = abs(x2 - x1);
327    dy = abs(y2 - y1);
328
329    if(y1 > y2)
330    {
331        charmapy = ",'";
332        yinc = -1;
333    }
334    else
335    {
336        yinc = 1;
337        charmapy = "`.";
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_putchar(qq, x1, y1, charmapy[1]);
352                x1++;
353                y1 += yinc;
354                delta += dpru;
355                prev = 1;
356            }
357            else
358            {
359                if(prev)
360                    cucul_putchar(qq, x1, y1, charmapy[0]);
361                else
362                    cucul_putchar(qq, 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_putchar(qq, x1, y1, charmapx[0]);
380                cucul_putchar(qq, x1 + 1, y1, charmapx[1]);
381                x1++;
382                y1 += yinc;
383                delta += dpru;
384            }
385            else
386            {
387                cucul_putchar(qq, x1, y1, '|');
388                y1 += yinc;
389                delta += dpr;
390            }
391        }
392    }
393}
394
Note: See TracBrowser for help on using the repository browser.