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

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