17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate/*
317c478bdstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bdstevel@tonic-gate * The Regents of the University of California
337c478bdstevel@tonic-gate * All Rights Reserved
347c478bdstevel@tonic-gate *
357c478bdstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bdstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bdstevel@tonic-gate * contributors.
387c478bdstevel@tonic-gate */
397c478bdstevel@tonic-gate
40e5190c1muffin#pragma ident	"%Z%%M%	%I%	%E% SMI"
41e5190c1muffin
427c478bdstevel@tonic-gate#include	<stdio.h>
437c478bdstevel@tonic-gate#include	<math.h>
447c478bdstevel@tonic-gate#define	PI	3.141592654
457c478bdstevel@tonic-gate#define	hmot(n)		hpos += n
467c478bdstevel@tonic-gate#define	hgoto(n)	hpos = n
477c478bdstevel@tonic-gate#define	vmot(n)		vgoto(vpos + n)
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gateextern	int	hpos;
507c478bdstevel@tonic-gateextern	int	vpos;
517c478bdstevel@tonic-gateextern	int	size;
527c478bdstevel@tonic-gateextern	short	*pstab;
537c478bdstevel@tonic-gateextern	int	DX;	/* step size in x */
547c478bdstevel@tonic-gateextern	int	DY;	/* step size in y */
557c478bdstevel@tonic-gateextern	int	drawdot;	/* character to use when drawing */
567c478bdstevel@tonic-gateextern	int	drawsize;	/* shrink point size by this facter */
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gateint	maxdots	= 32000;	/* maximum number of dots in an object */
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate#define	sgn(n)	((n > 0) ? 1 : ((n < 0) ? -1 : 0))
617c478bdstevel@tonic-gate#define	abs(n)	((n) >= 0 ? (n) : -(n))
627c478bdstevel@tonic-gate#define	max(x,y)	((x) > (y) ? (x) : (y))
637c478bdstevel@tonic-gate#define	min(x,y)	((x) < (y) ? (x) : (y))
647c478bdstevel@tonic-gate#define	arcmove(x,y)	{ hgoto(x); vmot(-vpos-(y)); }
657c478bdstevel@tonic-gate
66e5190c1muffinint
677c478bdstevel@tonic-gatedrawline(dx, dy, s)	/* draw line from here to dx, dy using s */
687c478bdstevel@tonic-gateint dx, dy;
697c478bdstevel@tonic-gatechar *s;
707c478bdstevel@tonic-gate{
717c478bdstevel@tonic-gate	int xd, yd;
727c478bdstevel@tonic-gate	float val, slope;
737c478bdstevel@tonic-gate	int i, numdots;
747c478bdstevel@tonic-gate	int dirmot, perp;
757c478bdstevel@tonic-gate	int motincr, perpincr;
767c478bdstevel@tonic-gate	int ohpos, ovpos, osize, ofont;
777c478bdstevel@tonic-gate	float incrway;
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate	int itemp; /*temp. storage for value returned byint function sgn*/
807c478bdstevel@tonic-gate	osize = size;
817c478bdstevel@tonic-gate	setsize(t_size(pstab[osize-1] / drawsize));
827c478bdstevel@tonic-gate	ohpos = hpos;
837c478bdstevel@tonic-gate	ovpos = vpos;
847c478bdstevel@tonic-gate	xd = dx / DX;
857c478bdstevel@tonic-gate	yd = dy / DX;
867c478bdstevel@tonic-gate	if (xd == 0) {
877c478bdstevel@tonic-gate		numdots = abs (yd);
887c478bdstevel@tonic-gate		numdots = min(numdots, maxdots);
897c478bdstevel@tonic-gate		motincr = DX * sgn (yd);
907c478bdstevel@tonic-gate		for (i = 0; i < numdots; i++) {
917c478bdstevel@tonic-gate			vmot(motincr);
927c478bdstevel@tonic-gate			put1(drawdot);
937c478bdstevel@tonic-gate		}
947c478bdstevel@tonic-gate		vgoto(ovpos + dy);
957c478bdstevel@tonic-gate		setsize(osize);
96e5190c1muffin		return (0);
977c478bdstevel@tonic-gate	}
987c478bdstevel@tonic-gate	if (yd == 0) {
997c478bdstevel@tonic-gate		numdots = abs (xd);
1007c478bdstevel@tonic-gate		motincr = DX * sgn (xd);
1017c478bdstevel@tonic-gate		for (i = 0; i < numdots; i++) {
1027c478bdstevel@tonic-gate			hmot(motincr);
1037c478bdstevel@tonic-gate			put1(drawdot);
1047c478bdstevel@tonic-gate		}
1057c478bdstevel@tonic-gate		hgoto(ohpos + dx);
1067c478bdstevel@tonic-gate		setsize(osize);
107e5190c1muffin		return (0);
1087c478bdstevel@tonic-gate	}
1097c478bdstevel@tonic-gate	if (abs (xd) > abs (yd)) {
1107c478bdstevel@tonic-gate		val = slope = (float) xd/yd;
1117c478bdstevel@tonic-gate		numdots = abs (xd);
1127c478bdstevel@tonic-gate		numdots = min(numdots, maxdots);
1137c478bdstevel@tonic-gate		dirmot = 'h';
1147c478bdstevel@tonic-gate		perp = 'v';
1157c478bdstevel@tonic-gate		motincr = DX * sgn (xd);
1167c478bdstevel@tonic-gate		perpincr = DX * sgn (yd);
1177c478bdstevel@tonic-gate	}
1187c478bdstevel@tonic-gate	else {
1197c478bdstevel@tonic-gate		val = slope = (float) yd/xd;
1207c478bdstevel@tonic-gate		numdots = abs (yd);
1217c478bdstevel@tonic-gate		numdots = min(numdots, maxdots);
1227c478bdstevel@tonic-gate		dirmot = 'v';
1237c478bdstevel@tonic-gate		perp = 'h';
1247c478bdstevel@tonic-gate		motincr = DX * sgn (yd);
1257c478bdstevel@tonic-gate		perpincr = DX * sgn (xd);
1267c478bdstevel@tonic-gate	}
1277c478bdstevel@tonic-gate	incrway = itemp = sgn ((int) slope);
1287c478bdstevel@tonic-gate	for (i = 0; i < numdots; i++) {
1297c478bdstevel@tonic-gate		val -= incrway;
1307c478bdstevel@tonic-gate		if (dirmot == 'h')
1317c478bdstevel@tonic-gate			hmot(motincr);
1327c478bdstevel@tonic-gate		else
1337c478bdstevel@tonic-gate			vmot(motincr);
1347c478bdstevel@tonic-gate		if (val * slope < 0) {
1357c478bdstevel@tonic-gate			if (perp == 'h')
1367c478bdstevel@tonic-gate				hmot(perpincr);
1377c478bdstevel@tonic-gate			else
1387c478bdstevel@tonic-gate				vmot(perpincr);
1397c478bdstevel@tonic-gate			val += slope;
1407c478bdstevel@tonic-gate		}
1417c478bdstevel@tonic-gate		put1(drawdot);
1427c478bdstevel@tonic-gate	}
1437c478bdstevel@tonic-gate	hgoto(ohpos + dx);
1447c478bdstevel@tonic-gate	vgoto(ovpos + dy);
1457c478bdstevel@tonic-gate	setsize(osize);
146e5190c1muffin
147e5190c1muffin	return (0);
1487c478bdstevel@tonic-gate}
1497c478bdstevel@tonic-gate
150e5190c1muffinint
1517c478bdstevel@tonic-gatedrawwig(s)	/* draw wiggly line */
1527c478bdstevel@tonic-gate	char *s;
1537c478bdstevel@tonic-gate{
1547c478bdstevel@tonic-gate	int x[50], y[50], xp, yp, pxp, pyp;
1557c478bdstevel@tonic-gate	float t1, t2, t3, w;
1567c478bdstevel@tonic-gate	int i, j, numdots, N;
1577c478bdstevel@tonic-gate	int osize, ofont;
1587c478bdstevel@tonic-gate	char temp[50], *p, *getstr();
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate	osize = size;
1617c478bdstevel@tonic-gate	setsize(t_size(pstab[osize-1] / drawsize));
1627c478bdstevel@tonic-gate	p = s;
1637c478bdstevel@tonic-gate	for (N = 2; (p=getstr(p,temp)) != NULL && N < sizeof(x)/sizeof(x[0]); N++) {
1647c478bdstevel@tonic-gate		x[N] = atoi(temp);
1657c478bdstevel@tonic-gate		p = getstr(p, temp);
1667c478bdstevel@tonic-gate		y[N] = atoi(temp);
1677c478bdstevel@tonic-gate	}
1687c478bdstevel@tonic-gate	x[0] = x[1] = hpos;
1697c478bdstevel@tonic-gate	y[0] = y[1] = vpos;
1707c478bdstevel@tonic-gate	for (i = 1; i < N; i++) {
1717c478bdstevel@tonic-gate		x[i+1] += x[i];
1727c478bdstevel@tonic-gate		y[i+1] += y[i];
1737c478bdstevel@tonic-gate	}
1747c478bdstevel@tonic-gate	x[N] = x[N-1];
1757c478bdstevel@tonic-gate	y[N] = y[N-1];
1767c478bdstevel@tonic-gate	pxp = pyp = -9999;
1777c478bdstevel@tonic-gate	for (i = 0; i < N-1; i++) {	/* interval */
1787c478bdstevel@tonic-gate		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;
1797c478bdstevel@tonic-gate		numdots /= DX;
1807c478bdstevel@tonic-gate		numdots = min(numdots, maxdots);
1817c478bdstevel@tonic-gate		for (j = 0; j < numdots; j++) {	/* points within */
1827c478bdstevel@tonic-gate			w = (float) j / numdots;
1837c478bdstevel@tonic-gate			t1 = 0.5 * w * w;
1847c478bdstevel@tonic-gate			w = w - 0.5;
1857c478bdstevel@tonic-gate			t2 = 0.75 - w * w;
1867c478bdstevel@tonic-gate			w = w - 0.5;
1877c478bdstevel@tonic-gate			t3 = 0.5 * w * w;
1887c478bdstevel@tonic-gate			xp = t1 * x[i+2] + t2 * x[i+1] + t3 * x[i] + 0.5;
1897c478bdstevel@tonic-gate			yp = t1 * y[i+2] + t2 * y[i+1] + t3 * y[i] + 0.5;
1907c478bdstevel@tonic-gate			if (xp != pxp || yp != pyp) {
1917c478bdstevel@tonic-gate				hgoto(xp);
1927c478bdstevel@tonic-gate				vgoto(yp);
1937c478bdstevel@tonic-gate				put1(drawdot);
1947c478bdstevel@tonic-gate				pxp = xp;
1957c478bdstevel@tonic-gate				pyp = yp;
1967c478bdstevel@tonic-gate			}
1977c478bdstevel@tonic-gate		}
1987c478bdstevel@tonic-gate	}
1997c478bdstevel@tonic-gate	setsize(osize);
200e5190c1muffin
201e5190c1muffin	return (0);
2027c478bdstevel@tonic-gate}
2037c478bdstevel@tonic-gate
2047c478bdstevel@tonic-gatechar *getstr(p, temp)	/* copy next non-blank string from p to temp, update p */
2057c478bdstevel@tonic-gatechar *p, *temp;
2067c478bdstevel@tonic-gate{
2077c478bdstevel@tonic-gate	while (*p == ' ' || *p == '\t' || *p == '\n')
2087c478bdstevel@tonic-gate		p++;
2097c478bdstevel@tonic-gate	if (*p == '\0') {
2107c478bdstevel@tonic-gate		temp[0] = 0;
2117c478bdstevel@tonic-gate		return(NULL);
2127c478bdstevel@tonic-gate	}
2137c478bdstevel@tonic-gate	while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2147c478bdstevel@tonic-gate		*temp++ = *p++;
2157c478bdstevel@tonic-gate	*temp = '\0';
2167c478bdstevel@tonic-gate	return(p);
2177c478bdstevel@tonic-gate}
2187c478bdstevel@tonic-gate
219e5190c1muffinint
2207c478bdstevel@tonic-gatedrawcirc(d)
2217c478bdstevel@tonic-gate{
2227c478bdstevel@tonic-gate	int xc, yc;
2237c478bdstevel@tonic-gate
2247c478bdstevel@tonic-gate	xc = hpos;
2257c478bdstevel@tonic-gate	yc = vpos;
2267c478bdstevel@tonic-gate	conicarc(hpos + d/2, -vpos, hpos, -vpos, hpos, -vpos, d/2, d/2);
2277c478bdstevel@tonic-gate	hgoto(xc + d);	/* circle goes to right side */
2287c478bdstevel@tonic-gate	vgoto(yc);
229e5190c1muffin
230e5190c1muffin	return (0);
2317c478bdstevel@tonic-gate}
2327c478bdstevel@tonic-gate
233e5190c1muffinint
2347c478bdstevel@tonic-gatedist(x1, y1, x2, y2)	/* integer distance from x1,y1 to x2,y2 */
2357c478bdstevel@tonic-gate{
2367c478bdstevel@tonic-gate	float dx, dy;
2377c478bdstevel@tonic-gate
2387c478bdstevel@tonic-gate	dx = x2 - x1;
2397c478bdstevel@tonic-gate	dy = y2 - y1;
2407c478bdstevel@tonic-gate	return sqrt(dx*dx + dy*dy) + 0.5;
2417c478bdstevel@tonic-gate}
2427c478bdstevel@tonic-gate
243e5190c1muffinint
2447c478bdstevel@tonic-gatedrawarc(dx1, dy1, dx2, dy2)
2457c478bdstevel@tonic-gate{
2467c478bdstevel@tonic-gate	int x0, y0, x2, y2, r;
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gate	x0 = hpos + dx1;	/* center */
2497c478bdstevel@tonic-gate	y0 = vpos + dy1;
2507c478bdstevel@tonic-gate	x2 = x0 + dx2;	/* "to" */
2517c478bdstevel@tonic-gate	y2 = y0 + dy2;
2527c478bdstevel@tonic-gate	r = sqrt((float) dx1 * dx1 + (float) dy1 * dy1) + 0.5;
2537c478bdstevel@tonic-gate	conicarc(x0, -y0, hpos, -vpos, x2, -y2, r, r);
254e5190c1muffin
255e5190c1muffin	return (0);
2567c478bdstevel@tonic-gate}
2577c478bdstevel@tonic-gate
258e5190c1muffinint
2597c478bdstevel@tonic-gatedrawellip(a, b)
2607c478bdstevel@tonic-gate{
2617c478bdstevel@tonic-gate	int xc, yc;
2627c478bdstevel@tonic-gate
2637c478bdstevel@tonic-gate	xc = hpos;
2647c478bdstevel@tonic-gate	yc = vpos;
2657c478bdstevel@tonic-gate	conicarc(hpos + a/2, -vpos, hpos, -vpos, hpos, -vpos, a/2, b/2);
2667c478bdstevel@tonic-gate	hgoto(xc + a);
2677c478bdstevel@tonic-gate	vgoto(yc);
268e5190c1muffin
269e5190c1muffin	return (0);
2707c478bdstevel@tonic-gate}
2717c478bdstevel@tonic-gate
2727c478bdstevel@tonic-gate#define sqr(x) (long int)(x)*(x)
2737c478bdstevel@tonic-gate
274e5190c1muffinint
2757c478bdstevel@tonic-gateconicarc(x, y, x0, y0, x1, y1, a, b)
2767c478bdstevel@tonic-gate{
2777c478bdstevel@tonic-gate	/* based on Bresenham, CACM, Feb 77, pp 102-3 */
2787c478bdstevel@tonic-gate	/* by Chris Van Wyk */
2797c478bdstevel@tonic-gate	/* capitalized vars are an internal reference frame */
2807c478bdstevel@tonic-gate	long dotcount = 0;
2817c478bdstevel@tonic-gate	int osize, ofont;
2827c478bdstevel@tonic-gate	int	xs, ys, xt, yt, Xs, Ys, qs, Xt, Yt, qt,
2837c478bdstevel@tonic-gate		M1x, M1y, M2x, M2y, M3x, M3y,
2847c478bdstevel@tonic-gate		Q, move, Xc, Yc;
2857c478bdstevel@tonic-gate	int ox1, oy1;
2867c478bdstevel@tonic-gate	long	delta;
2877c478bdstevel@tonic-gate	float	xc, yc;
2887c478bdstevel@tonic-gate	float	radius, slope;
2897c478bdstevel@tonic-gate	float	xstep, ystep;
2907c478bdstevel@tonic-gate
2917c478bdstevel@tonic-gate	osize = size;
2927c478bdstevel@tonic-gate	setsize(t_size(pstab[osize-1] / drawsize));
2937c478bdstevel@tonic-gate	ox1 = x1;
2947c478bdstevel@tonic-gate	oy1 = y1;
2957c478bdstevel@tonic-gate	if (a != b)	/* an arc of an ellipse; internally, will still think of circle */
2967c478bdstevel@tonic-gate		if (a > b) {
2977c478bdstevel@tonic-gate			xstep = (float)a / b;
2987c478bdstevel@tonic-gate			ystep = 1;
2997c478bdstevel@tonic-gate			radius = b;
3007c478bdstevel@tonic-gate		} else {
3017c478bdstevel@tonic-gate			xstep = 1;
3027c478bdstevel@tonic-gate			ystep = (float)b / a;
3037c478bdstevel@tonic-gate			radius = a;
3047c478bdstevel@tonic-gate		}
3057c478bdstevel@tonic-gate	else {	/* a circular arc; radius is computed from center and first point */
3067c478bdstevel@tonic-gate		xstep = ystep = 1;
3077c478bdstevel@tonic-gate		radius = sqrt((float)(sqr(x0 - x) + sqr(y0 - y)));
3087c478bdstevel@tonic-gate	}
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate	xc = x0;
3127c478bdstevel@tonic-gate	yc = y0;
3137c478bdstevel@tonic-gate	/* now, use start and end point locations to figure out
3147c478bdstevel@tonic-gate	the angle at which start and end happen; use these
3157c478bdstevel@tonic-gate	angles with known radius to figure out where start
3167c478bdstevel@tonic-gate	and end should be
3177c478bdstevel@tonic-gate	*/
3187c478bdstevel@tonic-gate	slope = atan2((double)(y0 - y), (double)(x0 - x) );
3197c478bdstevel@tonic-gate	if (slope == 0.0 && x0 < x)
3207c478bdstevel@tonic-gate		slope = 3.14159265;
3217c478bdstevel@tonic-gate	x0 = x + radius * cos(slope) + 0.5;
3227c478bdstevel@tonic-gate	y0 = y + radius * sin(slope) + 0.5;
3237c478bdstevel@tonic-gate	slope = atan2((double)(y1 - y), (double)(x1 - x));
3247c478bdstevel@tonic-gate	if (slope == 0.0 && x1 < x)
3257c478bdstevel@tonic-gate		slope = 3.14159265;
3267c478bdstevel@tonic-gate	x1 = x + radius * cos(slope) + 0.5;
3277c478bdstevel@tonic-gate	y1 = y + radius * sin(slope) + 0.5;
3287c478bdstevel@tonic-gate	/* step 2: translate to zero-centered circle */
3297c478bdstevel@tonic-gate	xs = x0 - x;
3307c478bdstevel@tonic-gate	ys = y0 - y;
3317c478bdstevel@tonic-gate	xt = x1 - x;
3327c478bdstevel@tonic-gate	yt = y1 - y;
3337c478bdstevel@tonic-gate	/* step 3: normalize to first quadrant */
3347c478bdstevel@tonic-gate	if (xs < 0)
3357c478bdstevel@tonic-gate		if (ys < 0) {
3367c478bdstevel@tonic-gate			Xs = abs(ys);
3377c478bdstevel@tonic-gate			Ys = abs(xs);
3387c478bdstevel@tonic-gate			qs = 3;
3397c478bdstevel@tonic-gate			M1x = 0;
3407c478bdstevel@tonic-gate			M1y = -1;
3417c478bdstevel@tonic-gate			M2x = 1;
3427c478bdstevel@tonic-gate			M2y = -1;
3437c478bdstevel@tonic-gate			M3x = 1;
3447c478bdstevel@tonic-gate			M3y = 0;
3457c478bdstevel@tonic-gate		} else {
3467c478bdstevel@tonic-gate			Xs = abs(xs);
3477c478bdstevel@tonic-gate			Ys = abs(ys);
3487c478bdstevel@tonic-gate			qs = 2;
3497c478bdstevel@tonic-gate			M1x = -1;
3507c478bdstevel@tonic-gate			M1y = 0;
3517c478bdstevel@tonic-gate			M2x = -1;
3527c478bdstevel@tonic-gate			M2y = -1;
3537c478bdstevel@tonic-gate			M3x = 0;
3547c478bdstevel@tonic-gate			M3y = -1;
3557c478bdstevel@tonic-gate		}
3567c478bdstevel@tonic-gate	else if (ys < 0) {
3577c478bdstevel@tonic-gate		Xs = abs(xs);
3587c478bdstevel@tonic-gate		Ys = abs(ys);
3597c478bdstevel@tonic-gate		qs = 0;
3607c478bdstevel@tonic-gate		M1x = 1;
3617c478bdstevel@tonic-gate		M1y = 0;
3627c478bdstevel@tonic-gate		M2x = 1;
3637c478bdstevel@tonic-gate		M2y = 1;
3647c478bdstevel@tonic-gate		M3x = 0;
3657c478bdstevel@tonic-gate		M3y = 1;
3667c478bdstevel@tonic-gate	} else {
3677c478bdstevel@tonic-gate		Xs = abs(ys);
3687c478bdstevel@tonic-gate		Ys = abs(xs);
3697c478bdstevel@tonic-gate		qs = 1;
3707c478bdstevel@tonic-gate		M1x = 0;
3717c478bdstevel@tonic-gate		M1y = 1;
3727c478bdstevel@tonic-gate		M2x = -1;
3737c478bdstevel@tonic-gate		M2y = 1;
3747c478bdstevel@tonic-gate		M3x = -1;
3757c478bdstevel@tonic-gate		M3y = 0;
3767c478bdstevel@tonic-gate	}
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate
3797c478bdstevel@tonic-gate	Xc = Xs;
3807c478bdstevel@tonic-gate	Yc = Ys;
3817c478bdstevel@tonic-gate	if (xt < 0)
3827c478bdstevel@tonic-gate		if (yt < 0) {
3837c478bdstevel@tonic-gate			Xt = abs(yt);
3847c478bdstevel@tonic-gate			Yt = abs(xt);
3857c478bdstevel@tonic-gate			qt = 3;
3867c478bdstevel@tonic-gate		} else {
3877c478bdstevel@tonic-gate			Xt = abs(xt);
3887c478bdstevel@tonic-gate			Yt = abs(yt);
3897c478bdstevel@tonic-gate			qt = 2;
3907c478bdstevel@tonic-gate		}
3917c478bdstevel@tonic-gate	else if (yt < 0) {
3927c478bdstevel@tonic-gate		Xt = abs(xt);
3937c478bdstevel@tonic-gate		Yt = abs(yt);
3947c478bdstevel@tonic-gate		qt = 0;
3957c478bdstevel@tonic-gate	} else {
3967c478bdstevel@tonic-gate		Xt = abs(yt);
3977c478bdstevel@tonic-gate		Yt = abs(xt);
3987c478bdstevel@tonic-gate		qt = 1;
3997c478bdstevel@tonic-gate	}
4007c478bdstevel@tonic-gate
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gate	/* step 4: calculate number of quadrant crossings */
4037c478bdstevel@tonic-gate	if (((4 + qt - qs)
4047c478bdstevel@tonic-gate	     % 4 == 0)
4057c478bdstevel@tonic-gate	     && (Xt <= Xs)
4067c478bdstevel@tonic-gate	     && (Yt >= Ys)
4077c478bdstevel@tonic-gate	    )
4087c478bdstevel@tonic-gate		Q = 3;
4097c478bdstevel@tonic-gate	else
4107c478bdstevel@tonic-gate		Q = (4 + qt - qs) % 4 - 1;
4117c478bdstevel@tonic-gate	/* step 5: calculate initial decision difference */
4127c478bdstevel@tonic-gate	delta = sqr(Xs + 1)
4137c478bdstevel@tonic-gate	 + sqr(Ys - 1)
4147c478bdstevel@tonic-gate	-sqr(xs)
4157c478bdstevel@tonic-gate	-sqr(ys);
4167c478bdstevel@tonic-gate	/* here begins the work of drawing
4177c478bdstevel@tonic-gate   we hope it ends here too */
4187c478bdstevel@tonic-gate	while ((Q >= 0)
4197c478bdstevel@tonic-gate	     || ((Q > -2)
4207c478bdstevel@tonic-gate	     && ((Xt > Xc)
4217c478bdstevel@tonic-gate	     && (Yt < Yc)
4227c478bdstevel@tonic-gate	    )
4237c478bdstevel@tonic-gate	    )
4247c478bdstevel@tonic-gate	    ) {
4257c478bdstevel@tonic-gate		if (dotcount++ % DX == 0)
4267c478bdstevel@tonic-gate			putdot((int)xc, (int)yc);
4277c478bdstevel@tonic-gate		if (Yc < 0.5) {
4287c478bdstevel@tonic-gate			/* reinitialize */
4297c478bdstevel@tonic-gate			Xs = Xc = 0;
4307c478bdstevel@tonic-gate			Ys = Yc = sqrt((float)(sqr(xs) + sqr(ys)));
4317c478bdstevel@tonic-gate			delta = sqr(Xs + 1) + sqr(Ys - 1) - sqr(xs) - sqr(ys);
4327c478bdstevel@tonic-gate			Q--;
4337c478bdstevel@tonic-gate			M1x = M3x;
4347c478bdstevel@tonic-gate			M1y = M3y;
4357c478bdstevel@tonic-gate			 {
4367c478bdstevel@tonic-gate				int	T;
4377c478bdstevel@tonic-gate				T = M2y;
4387c478bdstevel@tonic-gate				M2y = M2x;
4397c478bdstevel@tonic-gate				M2x = -T;
4407c478bdstevel@tonic-gate				T = M3y;
4417c478bdstevel@tonic-gate				M3y = M3x;
4427c478bdstevel@tonic-gate				M3x = -T;
4437c478bdstevel@tonic-gate			}
4447c478bdstevel@tonic-gate		} else {
4457c478bdstevel@tonic-gate			if (delta <= 0)
4467c478bdstevel@tonic-gate				if (2 * delta + 2 * Yc - 1 <= 0)
4477c478bdstevel@tonic-gate					move = 1;
4487c478bdstevel@tonic-gate				else
4497c478bdstevel@tonic-gate					move = 2;
4507c478bdstevel@tonic-gate			else if (2 * delta - 2 * Xc - 1 <= 0)
4517c478bdstevel@tonic-gate				move = 2;
4527c478bdstevel@tonic-gate			else
4537c478bdstevel@tonic-gate				move = 3;
4547c478bdstevel@tonic-gate			switch (move) {
4557c478bdstevel@tonic-gate			case 1:
4567c478bdstevel@tonic-gate				Xc++;
4577c478bdstevel@tonic-gate				delta += 2 * Xc + 1;
4587c478bdstevel@tonic-gate				xc += M1x * xstep;
4597c478bdstevel@tonic-gate				yc += M1y * ystep;
4607c478bdstevel@tonic-gate				break;
4617c478bdstevel@tonic-gate			case 2:
4627c478bdstevel@tonic-gate				Xc++;
4637c478bdstevel@tonic-gate				Yc--;
4647c478bdstevel@tonic-gate				delta += 2 * Xc - 2 * Yc + 2;
4657c478bdstevel@tonic-gate				xc += M2x * xstep;
4667c478bdstevel@tonic-gate				yc += M2y * ystep;
4677c478bdstevel@tonic-gate				break;
4687c478bdstevel@tonic-gate			case 3:
4697c478bdstevel@tonic-gate				Yc--;
4707c478bdstevel@tonic-gate				delta -= 2 * Yc + 1;
4717c478bdstevel@tonic-gate				xc += M3x * xstep;
4727c478bdstevel@tonic-gate				yc += M3y * ystep;
4737c478bdstevel@tonic-gate				break;
4747c478bdstevel@tonic-gate			}
4757c478bdstevel@tonic-gate		}
4767c478bdstevel@tonic-gate	}
4777c478bdstevel@tonic-gate
4787c478bdstevel@tonic-gate
4797c478bdstevel@tonic-gate	setsize(osize);
4807c478bdstevel@tonic-gate	drawline((int)xc-ox1,(int)yc-oy1,".");
481e5190c1muffin
482e5190c1muffin	return (0);
4837c478bdstevel@tonic-gate}
4847c478bdstevel@tonic-gate
485e5190c1muffinint
4867c478bdstevel@tonic-gateputdot(x, y)
4877c478bdstevel@tonic-gate{
4887c478bdstevel@tonic-gate	arcmove(x, y);
4897c478bdstevel@tonic-gate	put1(drawdot);
490e5190c1muffin
491e5190c1muffin	return (0);
4927c478bdstevel@tonic-gate}
493