xref: /titanic_50/usr/src/cmd/troff/troff.d/draw.c (revision 8ad60789b9913d5b2118fe76d8cd7a661d8ae8b6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include	<stdio.h>
43 #include	<math.h>
44 #define	PI	3.141592654
45 #define	hmot(n)		hpos += n
46 #define	hgoto(n)	hpos = n
47 #define	vmot(n)		vgoto(vpos + n)
48 
49 extern	int	hpos;
50 extern	int	vpos;
51 extern	int	size;
52 extern	short	*pstab;
53 extern	int	DX;	/* step size in x */
54 extern	int	DY;	/* step size in y */
55 extern	int	drawdot;	/* character to use when drawing */
56 extern	int	drawsize;	/* shrink point size by this facter */
57 
58 int	maxdots	= 32000;	/* maximum number of dots in an object */
59 
60 #define	sgn(n)	((n > 0) ? 1 : ((n < 0) ? -1 : 0))
61 #define	abs(n)	((n) >= 0 ? (n) : -(n))
62 #define	max(x,y)	((x) > (y) ? (x) : (y))
63 #define	min(x,y)	((x) < (y) ? (x) : (y))
64 #define	arcmove(x,y)	{ hgoto(x); vmot(-vpos-(y)); }
65 
66 int
67 drawline(dx, dy, s)	/* draw line from here to dx, dy using s */
68 int dx, dy;
69 char *s;
70 {
71 	int xd, yd;
72 	float val, slope;
73 	int i, numdots;
74 	int dirmot, perp;
75 	int motincr, perpincr;
76 	int ohpos, ovpos, osize, ofont;
77 	float incrway;
78 
79 	int itemp; /*temp. storage for value returned byint function sgn*/
80 	osize = size;
81 	setsize(t_size(pstab[osize-1] / drawsize));
82 	ohpos = hpos;
83 	ovpos = vpos;
84 	xd = dx / DX;
85 	yd = dy / DX;
86 	if (xd == 0) {
87 		numdots = abs (yd);
88 		numdots = min(numdots, maxdots);
89 		motincr = DX * sgn (yd);
90 		for (i = 0; i < numdots; i++) {
91 			vmot(motincr);
92 			put1(drawdot);
93 		}
94 		vgoto(ovpos + dy);
95 		setsize(osize);
96 		return (0);
97 	}
98 	if (yd == 0) {
99 		numdots = abs (xd);
100 		motincr = DX * sgn (xd);
101 		for (i = 0; i < numdots; i++) {
102 			hmot(motincr);
103 			put1(drawdot);
104 		}
105 		hgoto(ohpos + dx);
106 		setsize(osize);
107 		return (0);
108 	}
109 	if (abs (xd) > abs (yd)) {
110 		val = slope = (float) xd/yd;
111 		numdots = abs (xd);
112 		numdots = min(numdots, maxdots);
113 		dirmot = 'h';
114 		perp = 'v';
115 		motincr = DX * sgn (xd);
116 		perpincr = DX * sgn (yd);
117 	}
118 	else {
119 		val = slope = (float) yd/xd;
120 		numdots = abs (yd);
121 		numdots = min(numdots, maxdots);
122 		dirmot = 'v';
123 		perp = 'h';
124 		motincr = DX * sgn (yd);
125 		perpincr = DX * sgn (xd);
126 	}
127 	incrway = itemp = sgn ((int) slope);
128 	for (i = 0; i < numdots; i++) {
129 		val -= incrway;
130 		if (dirmot == 'h')
131 			hmot(motincr);
132 		else
133 			vmot(motincr);
134 		if (val * slope < 0) {
135 			if (perp == 'h')
136 				hmot(perpincr);
137 			else
138 				vmot(perpincr);
139 			val += slope;
140 		}
141 		put1(drawdot);
142 	}
143 	hgoto(ohpos + dx);
144 	vgoto(ovpos + dy);
145 	setsize(osize);
146 
147 	return (0);
148 }
149 
150 int
151 drawwig(s)	/* draw wiggly line */
152 	char *s;
153 {
154 	int x[50], y[50], xp, yp, pxp, pyp;
155 	float t1, t2, t3, w;
156 	int i, j, numdots, N;
157 	int osize, ofont;
158 	char temp[50], *p, *getstr();
159 
160 	osize = size;
161 	setsize(t_size(pstab[osize-1] / drawsize));
162 	p = s;
163 	for (N = 2; (p=getstr(p,temp)) != NULL && N < sizeof(x)/sizeof(x[0]); N++) {
164 		x[N] = atoi(temp);
165 		p = getstr(p, temp);
166 		y[N] = atoi(temp);
167 	}
168 	x[0] = x[1] = hpos;
169 	y[0] = y[1] = vpos;
170 	for (i = 1; i < N; i++) {
171 		x[i+1] += x[i];
172 		y[i+1] += y[i];
173 	}
174 	x[N] = x[N-1];
175 	y[N] = y[N-1];
176 	pxp = pyp = -9999;
177 	for (i = 0; i < N-1; i++) {	/* interval */
178 		numdots = (dist(x[i],y[i], x[i+1],y[i+1]) + dist(x[i+1],y[i+1], x[i+2],y[i+2])) / 2;
179 		numdots /= DX;
180 		numdots = min(numdots, maxdots);
181 		for (j = 0; j < numdots; j++) {	/* points within */
182 			w = (float) j / numdots;
183 			t1 = 0.5 * w * w;
184 			w = w - 0.5;
185 			t2 = 0.75 - w * w;
186 			w = w - 0.5;
187 			t3 = 0.5 * w * w;
188 			xp = t1 * x[i+2] + t2 * x[i+1] + t3 * x[i] + 0.5;
189 			yp = t1 * y[i+2] + t2 * y[i+1] + t3 * y[i] + 0.5;
190 			if (xp != pxp || yp != pyp) {
191 				hgoto(xp);
192 				vgoto(yp);
193 				put1(drawdot);
194 				pxp = xp;
195 				pyp = yp;
196 			}
197 		}
198 	}
199 	setsize(osize);
200 
201 	return (0);
202 }
203 
204 char *getstr(p, temp)	/* copy next non-blank string from p to temp, update p */
205 char *p, *temp;
206 {
207 	while (*p == ' ' || *p == '\t' || *p == '\n')
208 		p++;
209 	if (*p == '\0') {
210 		temp[0] = 0;
211 		return(NULL);
212 	}
213 	while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
214 		*temp++ = *p++;
215 	*temp = '\0';
216 	return(p);
217 }
218 
219 int
220 drawcirc(d)
221 {
222 	int xc, yc;
223 
224 	xc = hpos;
225 	yc = vpos;
226 	conicarc(hpos + d/2, -vpos, hpos, -vpos, hpos, -vpos, d/2, d/2);
227 	hgoto(xc + d);	/* circle goes to right side */
228 	vgoto(yc);
229 
230 	return (0);
231 }
232 
233 int
234 dist(x1, y1, x2, y2)	/* integer distance from x1,y1 to x2,y2 */
235 {
236 	float dx, dy;
237 
238 	dx = x2 - x1;
239 	dy = y2 - y1;
240 	return sqrt(dx*dx + dy*dy) + 0.5;
241 }
242 
243 int
244 drawarc(dx1, dy1, dx2, dy2)
245 {
246 	int x0, y0, x2, y2, r;
247 
248 	x0 = hpos + dx1;	/* center */
249 	y0 = vpos + dy1;
250 	x2 = x0 + dx2;	/* "to" */
251 	y2 = y0 + dy2;
252 	r = sqrt((float) dx1 * dx1 + (float) dy1 * dy1) + 0.5;
253 	conicarc(x0, -y0, hpos, -vpos, x2, -y2, r, r);
254 
255 	return (0);
256 }
257 
258 int
259 drawellip(a, b)
260 {
261 	int xc, yc;
262 
263 	xc = hpos;
264 	yc = vpos;
265 	conicarc(hpos + a/2, -vpos, hpos, -vpos, hpos, -vpos, a/2, b/2);
266 	hgoto(xc + a);
267 	vgoto(yc);
268 
269 	return (0);
270 }
271 
272 #define sqr(x) (long int)(x)*(x)
273 
274 int
275 conicarc(x, y, x0, y0, x1, y1, a, b)
276 {
277 	/* based on Bresenham, CACM, Feb 77, pp 102-3 */
278 	/* by Chris Van Wyk */
279 	/* capitalized vars are an internal reference frame */
280 	long dotcount = 0;
281 	int osize, ofont;
282 	int	xs, ys, xt, yt, Xs, Ys, qs, Xt, Yt, qt,
283 		M1x, M1y, M2x, M2y, M3x, M3y,
284 		Q, move, Xc, Yc;
285 	int ox1, oy1;
286 	long	delta;
287 	float	xc, yc;
288 	float	radius, slope;
289 	float	xstep, ystep;
290 
291 	osize = size;
292 	setsize(t_size(pstab[osize-1] / drawsize));
293 	ox1 = x1;
294 	oy1 = y1;
295 	if (a != b)	/* an arc of an ellipse; internally, will still think of circle */
296 		if (a > b) {
297 			xstep = (float)a / b;
298 			ystep = 1;
299 			radius = b;
300 		} else {
301 			xstep = 1;
302 			ystep = (float)b / a;
303 			radius = a;
304 		}
305 	else {	/* a circular arc; radius is computed from center and first point */
306 		xstep = ystep = 1;
307 		radius = sqrt((float)(sqr(x0 - x) + sqr(y0 - y)));
308 	}
309 
310 
311 	xc = x0;
312 	yc = y0;
313 	/* now, use start and end point locations to figure out
314 	the angle at which start and end happen; use these
315 	angles with known radius to figure out where start
316 	and end should be
317 	*/
318 	slope = atan2((double)(y0 - y), (double)(x0 - x) );
319 	if (slope == 0.0 && x0 < x)
320 		slope = 3.14159265;
321 	x0 = x + radius * cos(slope) + 0.5;
322 	y0 = y + radius * sin(slope) + 0.5;
323 	slope = atan2((double)(y1 - y), (double)(x1 - x));
324 	if (slope == 0.0 && x1 < x)
325 		slope = 3.14159265;
326 	x1 = x + radius * cos(slope) + 0.5;
327 	y1 = y + radius * sin(slope) + 0.5;
328 	/* step 2: translate to zero-centered circle */
329 	xs = x0 - x;
330 	ys = y0 - y;
331 	xt = x1 - x;
332 	yt = y1 - y;
333 	/* step 3: normalize to first quadrant */
334 	if (xs < 0)
335 		if (ys < 0) {
336 			Xs = abs(ys);
337 			Ys = abs(xs);
338 			qs = 3;
339 			M1x = 0;
340 			M1y = -1;
341 			M2x = 1;
342 			M2y = -1;
343 			M3x = 1;
344 			M3y = 0;
345 		} else {
346 			Xs = abs(xs);
347 			Ys = abs(ys);
348 			qs = 2;
349 			M1x = -1;
350 			M1y = 0;
351 			M2x = -1;
352 			M2y = -1;
353 			M3x = 0;
354 			M3y = -1;
355 		}
356 	else if (ys < 0) {
357 		Xs = abs(xs);
358 		Ys = abs(ys);
359 		qs = 0;
360 		M1x = 1;
361 		M1y = 0;
362 		M2x = 1;
363 		M2y = 1;
364 		M3x = 0;
365 		M3y = 1;
366 	} else {
367 		Xs = abs(ys);
368 		Ys = abs(xs);
369 		qs = 1;
370 		M1x = 0;
371 		M1y = 1;
372 		M2x = -1;
373 		M2y = 1;
374 		M3x = -1;
375 		M3y = 0;
376 	}
377 
378 
379 	Xc = Xs;
380 	Yc = Ys;
381 	if (xt < 0)
382 		if (yt < 0) {
383 			Xt = abs(yt);
384 			Yt = abs(xt);
385 			qt = 3;
386 		} else {
387 			Xt = abs(xt);
388 			Yt = abs(yt);
389 			qt = 2;
390 		}
391 	else if (yt < 0) {
392 		Xt = abs(xt);
393 		Yt = abs(yt);
394 		qt = 0;
395 	} else {
396 		Xt = abs(yt);
397 		Yt = abs(xt);
398 		qt = 1;
399 	}
400 
401 
402 	/* step 4: calculate number of quadrant crossings */
403 	if (((4 + qt - qs)
404 	     % 4 == 0)
405 	     && (Xt <= Xs)
406 	     && (Yt >= Ys)
407 	    )
408 		Q = 3;
409 	else
410 		Q = (4 + qt - qs) % 4 - 1;
411 	/* step 5: calculate initial decision difference */
412 	delta = sqr(Xs + 1)
413 	 + sqr(Ys - 1)
414 	-sqr(xs)
415 	-sqr(ys);
416 	/* here begins the work of drawing
417    we hope it ends here too */
418 	while ((Q >= 0)
419 	     || ((Q > -2)
420 	     && ((Xt > Xc)
421 	     && (Yt < Yc)
422 	    )
423 	    )
424 	    ) {
425 		if (dotcount++ % DX == 0)
426 			putdot((int)xc, (int)yc);
427 		if (Yc < 0.5) {
428 			/* reinitialize */
429 			Xs = Xc = 0;
430 			Ys = Yc = sqrt((float)(sqr(xs) + sqr(ys)));
431 			delta = sqr(Xs + 1) + sqr(Ys - 1) - sqr(xs) - sqr(ys);
432 			Q--;
433 			M1x = M3x;
434 			M1y = M3y;
435 			 {
436 				int	T;
437 				T = M2y;
438 				M2y = M2x;
439 				M2x = -T;
440 				T = M3y;
441 				M3y = M3x;
442 				M3x = -T;
443 			}
444 		} else {
445 			if (delta <= 0)
446 				if (2 * delta + 2 * Yc - 1 <= 0)
447 					move = 1;
448 				else
449 					move = 2;
450 			else if (2 * delta - 2 * Xc - 1 <= 0)
451 				move = 2;
452 			else
453 				move = 3;
454 			switch (move) {
455 			case 1:
456 				Xc++;
457 				delta += 2 * Xc + 1;
458 				xc += M1x * xstep;
459 				yc += M1y * ystep;
460 				break;
461 			case 2:
462 				Xc++;
463 				Yc--;
464 				delta += 2 * Xc - 2 * Yc + 2;
465 				xc += M2x * xstep;
466 				yc += M2y * ystep;
467 				break;
468 			case 3:
469 				Yc--;
470 				delta -= 2 * Yc + 1;
471 				xc += M3x * xstep;
472 				yc += M3y * ystep;
473 				break;
474 			}
475 		}
476 	}
477 
478 
479 	setsize(osize);
480 	drawline((int)xc-ox1,(int)yc-oy1,".");
481 
482 	return (0);
483 }
484 
485 int
486 putdot(x, y)
487 {
488 	arcmove(x, y);
489 	put1(drawdot);
490 
491 	return (0);
492 }
493