1 /* $Id: line.c,v 1.5 2002-10-10 18:55:32 btb Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
15 * Graphical routines for drawing lines.
42 Symmetric Double Step Line Algorithm
44 from "Graphics Gems", Academic Press, 1990
47 /* non-zero flag indicates the pixels needing EXCHG back. */
48 void plot(int x,int y,int flag)
56 int gr_hline(int x1, int x2, int y)
60 if (x1 > x2) EXCHG(x1,x2);
61 for (i=x1; i<=x2; i++ )
66 int gr_vline(int y1, int y2, int x)
69 if (y1 > y2) EXCHG(y1,y2);
70 for (i=y1; i<=y2; i++ )
75 void gr_universal_uline(int a1, int b1, int a2, int b2)
77 int dx, dy, incr1, incr2, D, x, y, xend, c, pixels_left;
79 int sign_x = 1, sign_y = 1, step, reverse, i;
103 /* decide increment sign by the slope sign */
104 if (sign_x == sign_y)
109 if (dy > dx) { /* chooses axis of greatest movement (make * dx) */
116 /* note error check for dx==0 should be included here */
117 if (a1 > a2) { /* start from the smaller coordinate */
130 /* Note dx=n implies 0 - n or (dx+1) pixels to be set */
131 /* Go round loop dx/4 times then plot last 0,1,2 or 3 pixels */
132 /* In fact (dx-1)/4 as 2 pixels are already plottted */
134 pixels_left = (dx - 1) % 4; /* number of pixels left over at the
137 plot(x1, y1, reverse); /* plot first two points */
138 incr2 = 4 * dy - 2 * dx;
139 if (incr2 < 0) { /* slope less than 1/2 */
144 for (i = 0; i < xend; i++) { /* plotting loop */
148 /* pattern 1 forwards */
150 plot(++x, y, reverse);
151 /* pattern 1 backwards */
152 plot(x1, y1, reverse);
153 plot(--x1, y1, reverse);
157 /* pattern 2 forwards */
159 plot(++x, y += step, reverse);
160 /* pattern 2 backwards */
161 plot(x1, y1, reverse);
162 plot(--x1, y1 -= step, reverse);
164 /* pattern 3 forwards */
165 plot(x, y += step, reverse);
166 plot(++x, y, reverse);
167 /* pattern 3 backwards */
168 plot(x1, y1 -= step, reverse);
169 plot(--x1, y1, reverse);
175 /* plot last pattern */
178 plot(++x, y, reverse); /* pattern 1 */
180 plot(++x, y, reverse);
182 plot(--x1, y1, reverse);
185 plot(++x, y, reverse); /* pattern 2 */
187 plot(++x, y += step, reverse);
189 plot(--x1, y1, reverse);
192 plot(++x, y += step, reverse);
194 plot(++x, y, reverse);
196 plot(--x1, y1 -= step, reverse);
199 } /* end if pixels_left */
201 /* end slope < 1/2 */
202 else { /* slope greater than 1/2 */
206 for (i = 0; i < xend; i++) {
210 /* pattern 4 forwards */
211 plot(x, y += step, reverse);
212 plot(++x, y += step, reverse);
213 /* pattern 4 backwards */
214 plot(x1, y1 -= step, reverse);
215 plot(--x1, y1 -= step, reverse);
219 /* pattern 2 forwards */
221 plot(++x, y += step, reverse);
223 /* pattern 2 backwards */
224 plot(x1, y1, reverse);
225 plot(--x1, y1 -= step, reverse);
227 /* pattern 3 forwards */
228 plot(x, y += step, reverse);
229 plot(++x, y, reverse);
230 /* pattern 3 backwards */
231 plot(x1, y1 -= step, reverse);
232 plot(--x1, y1, reverse);
237 /* plot last pattern */
240 plot(++x, y += step, reverse); /* pattern 4 */
242 plot(++x, y += step, reverse);
244 plot(--x1, y1 -= step, reverse);
247 plot(++x, y, reverse); /* pattern 2 */
249 plot(++x, y += step, reverse);
251 plot(--x1, y1, reverse);
254 plot(++x, y += step, reverse);
256 plot(++x, y, reverse);
257 if (pixels_left > 2) {
258 if (D > c) /* step 3 */
259 plot(--x1, y1 -= step, reverse);
261 plot(--x1, y1, reverse);
270 //unclipped version just calls clipping version for now
271 int gr_uline(fix _a1, fix _b1, fix _a2, fix _b2)
274 a1 = f2i(_a1); b1 = f2i(_b1); a2 = f2i(_a2); b2 = f2i(_b2);
279 ogl_ulinec(a1,b1,a2,b2,COLOR);
284 gr_universal_uline( a1,b1,a2,b2);
286 gr_linear_line( a1, b1, a2, b2 );
291 modex_line_x1 = a1+XOFFSET;
292 modex_line_y1 = b1+YOFFSET;
293 modex_line_x2 = a2+XOFFSET;
294 modex_line_y2 = b2+YOFFSET;
295 modex_line_Color = grd_curcanv->cv_color;
299 gr_universal_uline( a1, b1, a2, b2 );
306 // Returns 0 if drawn with no clipping, 1 if drawn but clipped, and
307 // 2 if not drawn at all.
309 int gr_line(fix a1, fix b1, fix a2, fix b2)
319 CLIPLINE(a1,b1,a2,b2,x1,y1,x2,y2,return 2,clipped=1, FSCALE );
321 gr_uline( a1, b1, a2, b2 );