-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline.c
More file actions
342 lines (311 loc) · 8.07 KB
/
Copy pathline.c
File metadata and controls
342 lines (311 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/
/*
* $Source: Smoke:miner:source:2d::RCS:line.c $
* $Revision: 1.2 $
* $Author: allender $
* $Date: 1995/09/14 13:45:24 $
*
* Graphical routines for drawing lines.
*
* $Log: line.c $
* Revision 1.2 1995/09/14 13:45:24 allender
* optimizations from Dave Denhart
*
* Revision 1.1 1995/03/09 09:09:46 allender
* Initial revision
*
*
* --- PC RCS information ---
* Revision 1.10 1994/11/18 22:50:02 john
* Changed shorts to ints in parameters.
*
* Revision 1.9 1994/07/13 12:03:04 john
* Added assembly modex line-drawer.
*
* Revision 1.8 1993/12/06 18:18:03 john
* took out aaline.
*
* Revision 1.7 1993/12/03 12:11:17 john
* ,
*
* Revision 1.6 1993/11/18 09:40:22 john
* Added laser-line
*
* Revision 1.5 1993/10/15 16:23:36 john
* y
*
* Revision 1.4 1993/09/29 16:13:58 john
* optimized
*
* Revision 1.3 1993/09/26 18:44:12 matt
* Added gr_uline(), which just calls gr_line(), and made both take
* fixes, and shift down themselves.
*
* Revision 1.2 1993/09/11 19:50:15 matt
* In gr_vline() & gr_hline(), check for start > end, and EXCHG if so
*
* Revision 1.1 1993/09/08 11:43:54 john
* Initial revision
*
*
*/
#include <stdlib.h>
#include "mem.h"
#include "gr.h"
#include "grdef.h"
#include "fix.h"
#include "clip.h"
/*
Symmetric Double Step Line Algorithm
by Brian Wyvill
from "Graphics Gems", Academic Press, 1990
*/
/* non-zero flag indicates the pixels needing EXCHG back. */
//void plot(int x,int y,int flag)
//{ if (flag)
// gr_upixel(y, x);
// else
// gr_upixel(x, y);
//}
#define plot( x, y, flag) \
do{ if (flag) \
DATA[ ROWSIZE*(x)+(y) ] = COLOR; \
else \
DATA[ ROWSIZE*(y)+(x) ] = COLOR; \
}while(0)
int gr_hline(int x1, int x2, int y)
{ int i;
int t;
if (x1 > x2) EXCHG(x1,x2);
t = ROWSIZE * y;
for (i=x1; i<=x2; i++ )
{
// gr_upixel( i, y );
DATA[ t + i ] = COLOR;
}
return 0;
}
int gr_vline(int y1, int y2, int x)
{ int i;
if (y1 > y2) EXCHG(y1,y2);
for (i=y1; i<=y2; i++ )
// gr_upixel( x, i );
DATA[ ROWSIZE*i + x ] = COLOR;
return 0;
}
void gr_universal_uline(int a1, int b1, int a2, int b2)
{
int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
int x1, y1;
int sign_x = 1, sign_y = 1, step, reverse, i;
if (a1==a2) {
gr_vline(b1,b2,a1);
return;
}
if (b1==b2) {
gr_hline(a1,a2,b1);
return;
}
dx = a2 - a1;
dy = b2 - b1;
if (dx < 0) {
sign_x = -1;
dx *= -1;
}
if (dy < 0) {
sign_y = -1;
dy *= -1;
}
/* decide increment sign by the slope sign */
if (sign_x == sign_y)
step = 1;
else
step = -1;
if (dy > dx) { /* chooses axis of greatest movement (make * dx) */
EXCHG(a1, b1);
EXCHG(a2, b2);
EXCHG(dx, dy);
reverse = 1;
} else
reverse = 0;
/* note error check for dx==0 should be included here */
if (a1 > a2) { /* start from the smaller coordinate */
x = a2;
y = b2;
x1 = a1;
y1 = b1;
} else {
x = a1;
y = b1;
x1 = a2;
y1 = b2;
}
/* Note dx=n implies 0 - n or (dx+1) pixels to be set */
/* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
/* In fact (dx-1)/4 as 2 pixels are already plottted */
xend = (dx - 1) / 4;
pixels_left = (dx - 1) % 4; /* number of pixels left over at the
* end */
plot(x, y, reverse);
plot(x1, y1, reverse); /* plot first two points */
incr2 = 4 * dy - 2 * dx;
if (incr2 < 0) { /* slope less than 1/2 */
c = 2 * dy;
incr1 = 2 * c;
D = incr1 - dx;
for (i = 0; i < xend; i++) { /* plotting loop */
++x;
--x1;
if (D < 0) {
/* pattern 1 forwards */
plot(x, y, reverse);
plot(++x, y, reverse);
/* pattern 1 backwards */
plot(x1, y1, reverse);
plot(--x1, y1, reverse);
D += incr1;
} else {
if (D < c) {
/* pattern 2 forwards */
plot(x, y, reverse);
plot(++x, y += step, reverse);
/* pattern 2 backwards */
plot(x1, y1, reverse);
plot(--x1, y1 -= step, reverse);
} else {
/* pattern 3 forwards */
plot(x, y += step, reverse);
plot(++x, y, reverse);
/* pattern 3 backwards */
plot(x1, y1 -= step, reverse);
plot(--x1, y1, reverse);
}
D += incr2;
}
} /* end for */
/* plot last pattern */
if (pixels_left) {
if (D < 0) {
plot(++x, y, reverse); /* pattern 1 */
if (pixels_left > 1)
plot(++x, y, reverse);
if (pixels_left > 2)
plot(--x1, y1, reverse);
} else {
if (D < c) {
plot(++x, y, reverse); /* pattern 2 */
if (pixels_left > 1)
plot(++x, y += step, reverse);
if (pixels_left > 2)
plot(--x1, y1, reverse);
} else {
/* pattern 3 */
plot(++x, y += step, reverse);
if (pixels_left > 1)
plot(++x, y, reverse);
if (pixels_left > 2)
plot(--x1, y1 -= step, reverse);
}
}
} /* end if pixels_left */
}
/* end slope < 1/2 */
else { /* slope greater than 1/2 */
c = 2 * (dy - dx);
incr1 = 2 * c;
D = incr1 + dx;
for (i = 0; i < xend; i++) {
++x;
--x1;
if (D > 0) {
/* pattern 4 forwards */
plot(x, y += step, reverse);
plot(++x, y += step, reverse);
/* pattern 4 backwards */
plot(x1, y1 -= step, reverse);
plot(--x1, y1 -= step, reverse);
D += incr1;
} else {
if (D < c) {
/* pattern 2 forwards */
plot(x, y, reverse);
plot(++x, y += step, reverse);
/* pattern 2 backwards */
plot(x1, y1, reverse);
plot(--x1, y1 -= step, reverse);
} else {
/* pattern 3 forwards */
plot(x, y += step, reverse);
plot(++x, y, reverse);
/* pattern 3 backwards */
plot(x1, y1 -= step, reverse);
plot(--x1, y1, reverse);
}
D += incr2;
}
} /* end for */
/* plot last pattern */
if (pixels_left) {
if (D > 0) {
plot(++x, y += step, reverse); /* pattern 4 */
if (pixels_left > 1)
plot(++x, y += step, reverse);
if (pixels_left > 2)
plot(--x1, y1 -= step, reverse);
} else {
if (D < c) {
plot(++x, y, reverse); /* pattern 2 */
if (pixels_left > 1)
plot(++x, y += step, reverse);
if (pixels_left > 2)
plot(--x1, y1, reverse);
} else {
/* pattern 3 */
plot(++x, y += step, reverse);
if (pixels_left > 1)
plot(++x, y, reverse);
if (pixels_left > 2) {
if (D > c) /* step 3 */
plot(--x1, y1 -= step, reverse);
else /* step 2 */
plot(--x1, y1, reverse);
}
}
}
}
}
}
//unclipped version just calls clipping version for now
int gr_uline(fix _a1, fix _b1, fix _a2, fix _b2)
{
int a1,b1,a2,b2;
a1 = f2i(_a1); b1 = f2i(_b1); a2 = f2i(_a2); b2 = f2i(_b2);
// gr_linear_line( a1, b1, a2, b2 );
gr_universal_uline( a1, b1, a2, b2 );
return 0;
}
// Returns 0 if drawn with no clipping, 1 if drawn but clipped, and
// 2 if not drawn at all.
int gr_line(fix a1, fix b1, fix a2, fix b2)
{
int x1, y1, x2, y2;
int clipped=0;
x1 = i2f(MINX);
y1 = i2f(MINY);
x2 = i2f(MAXX);
y2 = i2f(MAXY);
CLIPLINE(a1,b1,a2,b2,x1,y1,x2,y2,return 2,clipped=1, FSCALE );
gr_uline( a1, b1, a2, b2 );
return clipped;
}