xref: /onnv-gate/usr/src/lib/libplot/t300s/common/arc.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4	*/
32*0Sstevel@tonic-gate /*LINTLIBRARY*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <math.h>
36*0Sstevel@tonic-gate #include <plot.h>
37*0Sstevel@tonic-gate #include "con.h"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate static int del = 20;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate static void
step(int d)42*0Sstevel@tonic-gate step(int d)
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate 	del = d;
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /* local definition of quad */
48*0Sstevel@tonic-gate static int
quad_l(short x,short y,short xp,short yp)49*0Sstevel@tonic-gate quad_l(short x, short y, short xp, short yp)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	if (x < xp)
52*0Sstevel@tonic-gate 		if (y <= yp)
53*0Sstevel@tonic-gate 			return (1);
54*0Sstevel@tonic-gate 		else return (4);
55*0Sstevel@tonic-gate 	else if (x > xp)
56*0Sstevel@tonic-gate 		if (y < yp)
57*0Sstevel@tonic-gate 			return (2);
58*0Sstevel@tonic-gate 		else return (3);
59*0Sstevel@tonic-gate 	else if (y < yp)
60*0Sstevel@tonic-gate 		return (2);
61*0Sstevel@tonic-gate 	else return (4);
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate void
arc(short x,short y,short x0,short y0,short x1,short y1)65*0Sstevel@tonic-gate arc(short x, short y, short x0, short y0, short x1, short y1)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	double pc;
68*0Sstevel@tonic-gate 	int flg, m, xc, yc, xs, ys, qs, qf, qt, qtctr = 0;
69*0Sstevel@tonic-gate 	int m0, m1;
70*0Sstevel@tonic-gate 	float dx, dy, r;
71*0Sstevel@tonic-gate 	char use;
72*0Sstevel@tonic-gate 	dx = x - x0;
73*0Sstevel@tonic-gate 	dy = y - y0;
74*0Sstevel@tonic-gate 	r = dx * dx + dy * dy;
75*0Sstevel@tonic-gate 	pc = r;
76*0Sstevel@tonic-gate 	pc = sqrt(pc);
77*0Sstevel@tonic-gate 	flg = (short)(pc / 4);
78*0Sstevel@tonic-gate 	if (flg == 0)
79*0Sstevel@tonic-gate 		step(1);
80*0Sstevel@tonic-gate 	else if (flg < del)
81*0Sstevel@tonic-gate 		step(flg);
82*0Sstevel@tonic-gate 	xc = xs = x0;
83*0Sstevel@tonic-gate 	yc = ys = y0;
84*0Sstevel@tonic-gate 	move((short)xs, (short)ys);
85*0Sstevel@tonic-gate 	if ((x0 == x1) && (y0 == y1))
86*0Sstevel@tonic-gate 		flg = 0;
87*0Sstevel@tonic-gate 	else flg = 1;
88*0Sstevel@tonic-gate 	qs = quad_l(x, y, x0, y0);
89*0Sstevel@tonic-gate 	qf = quad_l(x, y, x1, y1);
90*0Sstevel@tonic-gate 	if (abs(x - x1) < abs(y - y1)) {
91*0Sstevel@tonic-gate 		use = 'x';
92*0Sstevel@tonic-gate 		if ((qs == 2) || (qs == 3))
93*0Sstevel@tonic-gate 			m = -1;
94*0Sstevel@tonic-gate 		else m = 1;
95*0Sstevel@tonic-gate 	} else {
96*0Sstevel@tonic-gate 		use = 'y';
97*0Sstevel@tonic-gate 		if (qs > 2)
98*0Sstevel@tonic-gate 			m = -1;
99*0Sstevel@tonic-gate 		else m = 1;
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 	if (qs == qf) {
102*0Sstevel@tonic-gate 		m0 = (y0 - y) / (x0 - x);
103*0Sstevel@tonic-gate 		m1 = (y1 - y) / (x1 - x);
104*0Sstevel@tonic-gate 		if (m0 >= m1)
105*0Sstevel@tonic-gate 			qt = 4;
106*0Sstevel@tonic-gate 		else qt = 0;
107*0Sstevel@tonic-gate 	} else if ((qt = qf - qs) < 0)
108*0Sstevel@tonic-gate 			qt += 4;
109*0Sstevel@tonic-gate 	/*LINTED*/
110*0Sstevel@tonic-gate 	while (1) {
111*0Sstevel@tonic-gate 		switch (use) {
112*0Sstevel@tonic-gate 		case 'x':
113*0Sstevel@tonic-gate 			if ((qs == 2) || (qs == 3))
114*0Sstevel@tonic-gate 				yc -= del;
115*0Sstevel@tonic-gate 			else yc += del;
116*0Sstevel@tonic-gate 			dy = yc - y;
117*0Sstevel@tonic-gate 			pc = r - dy * dy;
118*0Sstevel@tonic-gate 			xc = (int)(m * sqrt(pc) + x);
119*0Sstevel@tonic-gate 			if (((x < xs) && (x >= xc)) ||
120*0Sstevel@tonic-gate 			    ((x > xs) && (x <= xc)) ||
121*0Sstevel@tonic-gate 			    ((y < ys) && (y >= yc)) ||
122*0Sstevel@tonic-gate 			    ((y > ys) && (y <=  yc))) {
123*0Sstevel@tonic-gate 				if (++qtctr > qt)
124*0Sstevel@tonic-gate 					return;
125*0Sstevel@tonic-gate 				if (++qs > 4)
126*0Sstevel@tonic-gate 					qs = 1;
127*0Sstevel@tonic-gate 				if ((qs == 2) || (qs == 3))
128*0Sstevel@tonic-gate 					m = -1;
129*0Sstevel@tonic-gate 				else m = 1;
130*0Sstevel@tonic-gate 				flg = 1;
131*0Sstevel@tonic-gate 			}
132*0Sstevel@tonic-gate 			cont((short)xc, (short)yc);
133*0Sstevel@tonic-gate 			xs = xc;
134*0Sstevel@tonic-gate 			ys = yc;
135*0Sstevel@tonic-gate 			if ((qs == qf) && (flg == 1))
136*0Sstevel@tonic-gate 				switch (qf) {
137*0Sstevel@tonic-gate 				case 3:
138*0Sstevel@tonic-gate 				case 4:
139*0Sstevel@tonic-gate 					if (xs >= x1)
140*0Sstevel@tonic-gate 						return;
141*0Sstevel@tonic-gate 					continue;
142*0Sstevel@tonic-gate 				case 1:
143*0Sstevel@tonic-gate 				case 2:
144*0Sstevel@tonic-gate 					if (xs <= x1)
145*0Sstevel@tonic-gate 						return;
146*0Sstevel@tonic-gate 				}
147*0Sstevel@tonic-gate 			continue;
148*0Sstevel@tonic-gate 		case 'y':
149*0Sstevel@tonic-gate 			if (qs > 2)
150*0Sstevel@tonic-gate 				xc += del;
151*0Sstevel@tonic-gate 			else xc -= del;
152*0Sstevel@tonic-gate 			dx = xc - x;
153*0Sstevel@tonic-gate 			pc = r - dx * dx;
154*0Sstevel@tonic-gate 			yc = (int)(m * sqrt(pc) + y);
155*0Sstevel@tonic-gate 			if (((x < xs) && (x >= xc)) ||
156*0Sstevel@tonic-gate 			    ((x > xs) && (x <= xc)) ||
157*0Sstevel@tonic-gate 			    ((y < ys) && (y >= yc)) ||
158*0Sstevel@tonic-gate 			    ((y > ys) && (y <= yc))) {
159*0Sstevel@tonic-gate 				if (++qtctr > qt)
160*0Sstevel@tonic-gate 					return;
161*0Sstevel@tonic-gate 				if (++qs > 4)
162*0Sstevel@tonic-gate 					qs = 1;
163*0Sstevel@tonic-gate 				if (qs > 2)
164*0Sstevel@tonic-gate 					m = -1;
165*0Sstevel@tonic-gate 				else m = 1;
166*0Sstevel@tonic-gate 				flg = 1;
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 			cont((short)xc, (short)yc);
169*0Sstevel@tonic-gate 			xs = xc;
170*0Sstevel@tonic-gate 			ys = yc;
171*0Sstevel@tonic-gate 			if ((qs == qf) && (flg == 1))
172*0Sstevel@tonic-gate 				switch (qs) {
173*0Sstevel@tonic-gate 				case 1:
174*0Sstevel@tonic-gate 				case 4:
175*0Sstevel@tonic-gate 					if (ys >= y1)
176*0Sstevel@tonic-gate 						return;
177*0Sstevel@tonic-gate 					continue;
178*0Sstevel@tonic-gate 				case 2:
179*0Sstevel@tonic-gate 				case 3:
180*0Sstevel@tonic-gate 					if (ys <= y1)
181*0Sstevel@tonic-gate 						return;
182*0Sstevel@tonic-gate 				}
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 	}
185*0Sstevel@tonic-gate }
186