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 97 2003-11-09 17:21:43Z 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 | |
---|
30 | static uint8_t clip_bits(int, int); |
---|
31 | static void draw_solid_line(int, int, int, int, char); |
---|
32 | |
---|
33 | void ee_draw_line(int x1, int y1, int x2, int y2, char c) |
---|
34 | { |
---|
35 | uint8_t bits1, bits2; |
---|
36 | |
---|
37 | bits1 = clip_bits(x1, y1); |
---|
38 | bits2 = clip_bits(x2, y2); |
---|
39 | |
---|
40 | if(bits1 & bits2) |
---|
41 | return; |
---|
42 | |
---|
43 | if(bits1 == 0) |
---|
44 | { |
---|
45 | if(bits2 == 0) |
---|
46 | draw_solid_line(x1, y1, x2, y2, c); |
---|
47 | else |
---|
48 | ee_draw_line(x2, y2, x1, y1, c); |
---|
49 | |
---|
50 | return; |
---|
51 | } |
---|
52 | |
---|
53 | if(bits1 & (1<<0)) |
---|
54 | { |
---|
55 | y1 = y2 - (x2-0) * (y2-y1) / (x2-x1); |
---|
56 | x1 = 0; |
---|
57 | } |
---|
58 | else if( bits1 & (1<<1) ) |
---|
59 | { |
---|
60 | int xmax = ee_get_width() - 1; |
---|
61 | y1 = y2 - (x2-xmax) * (y2-y1) / (x2-x1); |
---|
62 | x1 = xmax; |
---|
63 | } |
---|
64 | else if( bits1 & (1<<2) ) |
---|
65 | { |
---|
66 | x1 = x2 - (y2-0) * (x2-x1) / (y2-y1); |
---|
67 | y1 = 0; |
---|
68 | } |
---|
69 | else if( bits1 & (1<<3) ) |
---|
70 | { |
---|
71 | int ymax = ee_get_height() - 1; |
---|
72 | x1 = x2 - (y2-ymax) * (x2-x1) / (y2-y1); |
---|
73 | y1 = ymax; |
---|
74 | } |
---|
75 | |
---|
76 | ee_draw_line(x1, y1, x2, y2, c); |
---|
77 | } |
---|
78 | |
---|
79 | static void draw_solid_line(int x1, int y1, int x2, int y2, char c) |
---|
80 | { |
---|
81 | int dx = abs(x2-x1); |
---|
82 | int dy = abs(y2-y1); |
---|
83 | |
---|
84 | int xinc, yinc; |
---|
85 | |
---|
86 | xinc = (x1 > x2) ? -1 : 1; |
---|
87 | yinc = (y1 > y2) ? -1 : 1; |
---|
88 | |
---|
89 | if(dx >= dy) |
---|
90 | { |
---|
91 | int dpr = dy << 1; |
---|
92 | int dpru = dpr - (dx << 1); |
---|
93 | int delta = dpr - dx; |
---|
94 | |
---|
95 | for(; dx>=0; dx--) |
---|
96 | { |
---|
97 | ee_goto(x1, y1); |
---|
98 | ee_putchar(c); |
---|
99 | if(delta > 0) |
---|
100 | { |
---|
101 | x1 += xinc; |
---|
102 | y1 += yinc; |
---|
103 | delta += dpru; |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | x1 += xinc; |
---|
108 | delta += dpr; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | int dpr = dx << 1; |
---|
115 | int dpru = dpr - (dy << 1); |
---|
116 | int delta = dpr - dy; |
---|
117 | |
---|
118 | for(; dy >= 0; dy--) |
---|
119 | { |
---|
120 | ee_goto(x1, y1); |
---|
121 | ee_putchar(c); |
---|
122 | if(delta > 0) |
---|
123 | { |
---|
124 | x1 += xinc; |
---|
125 | y1 += yinc; |
---|
126 | delta += dpru; |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | y1 += yinc; |
---|
131 | delta += dpr; |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | static uint8_t clip_bits(int x, int y) |
---|
138 | { |
---|
139 | uint8_t b = 0; |
---|
140 | |
---|
141 | if(x < 0) |
---|
142 | b |= (1<<0); |
---|
143 | else if(x >= ee_get_width()) |
---|
144 | b |= (1<<1); |
---|
145 | |
---|
146 | if(y < 0) |
---|
147 | b |= (1<<2); |
---|
148 | else if(y >= ee_get_height()) |
---|
149 | b |= (1<<3); |
---|
150 | |
---|
151 | return b; |
---|
152 | } |
---|
153 | |
---|