source: ttyvaders/trunk/libee/line.c @ 102

Last change on this file since 102 was 102, checked in by Sam Hocevar, 17 years ago
  • libee/line.c: genericized line drawing for future use.
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
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 102 2003-11-09 18:10:44Z 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#include <inttypes.h>
26#include <stdlib.h>
27
28#include "ee.h"
29
30struct line
31{
32    int x1, y1;
33    int x2, y2;
34    char c;
35    void (*draw) (struct line*);
36};
37
38static void clip_line(struct line*);
39static uint8_t clip_bits(int, int);
40static void draw_solid_line(struct line*);
41
42void ee_draw_line(int x1, int y1, int x2, int y2, char c)
43{
44    struct line s;
45    s.x1 = x1;
46    s.y1 = y1;
47    s.x2 = x2;
48    s.y2 = y2;
49    s.c = c;
50    s.draw = draw_solid_line;
51    clip_line(&s);
52}
53
54static void clip_line(struct line* s)
55{
56    uint8_t bits1, bits2;
57
58    bits1 = clip_bits(s->x1, s->y1);
59    bits2 = clip_bits(s->x2, s->y2);
60
61    if(bits1 & bits2)
62        return;
63
64    if(bits1 == 0)
65    {
66        if(bits2 == 0)
67            s->draw(s);
68        else
69        {
70            int tmp;
71            tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
72            tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
73            clip_line(s);
74        }
75
76        return;
77    }
78
79    if(bits1 & (1<<0))
80    {
81        s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
82        s->x1 = 0;
83    }
84    else if( bits1 & (1<<1) )
85    {
86        int xmax = ee_get_width() - 1;
87        s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
88        s->x1 = xmax;
89    }
90    else if( bits1 & (1<<2) )
91    {
92        s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
93        s->y1 = 0;
94    }
95    else if( bits1 & (1<<3) )
96    {
97        int ymax = ee_get_height() - 1;
98        s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
99        s->y1 = ymax;
100    }
101
102    clip_line(s);
103}
104
105static uint8_t clip_bits(int x, int y)
106{
107    uint8_t b = 0;
108
109    if(x < 0)
110        b |= (1<<0);
111    else if(x >= ee_get_width())
112        b |= (1<<1);
113
114    if(y < 0)
115        b |= (1<<2);
116    else if(y >= ee_get_height())
117        b |= (1<<3);
118
119    return b;
120}
121
122static void draw_solid_line(struct line* s)
123{
124    int x1 = s->x1;
125    int y1 = s->y1;
126    int x2 = s->x2;
127    int y2 = s->y2;
128
129    int dx = abs(x2-x1);
130    int dy = abs(y2-y1);
131
132    int xinc, yinc;
133
134    xinc = (x1 > x2) ? -1 : 1;
135    yinc = (y1 > y2) ? -1 : 1;
136
137    if(dx >= dy)
138    {
139        int dpr = dy << 1;
140        int dpru = dpr - (dx << 1);
141        int delta = dpr - dx;
142
143        for(; dx>=0; dx--)
144        {
145            ee_goto(x1, y1);
146            ee_putchar(s->c);
147            if(delta > 0)
148            {
149                x1 += xinc;
150                y1 += yinc;
151                delta += dpru;
152            }
153            else
154            {
155                x1 += xinc;
156                delta += dpr;
157            }
158        }
159    }
160    else
161    {
162        int dpr = dx << 1;
163        int dpru = dpr - (dy << 1);
164        int delta = dpr - dy;
165
166        for(; dy >= 0; dy--)
167        {
168            ee_goto(x1, y1);
169            ee_putchar(s->c);
170            if(delta > 0)
171            {
172                x1 += xinc;
173                y1 += yinc;
174                delta += dpru;
175            }
176            else
177            {
178                y1 += yinc;
179                delta += dpr;
180            }
181        }
182    }
183}
184
Note: See TracBrowser for help on using the repository browser.