xref: /onnv-gate/usr/src/lib/libplot/t300/common/arc.c (revision 0:68f95e015346)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4	*/
32 /*LINTLIBRARY*/
33 
34 #include <stdlib.h>
35 #include <plot.h>
36 #include <math.h>
37 #include "con.h"
38 
39 static int quad_l(short, short, short, short);
40 static int del = 20;
41 
42 static void
step(int d)43 step(int d)
44 {
45 	del = d;
46 }
47 
48 void
arc(short x,short y,short x0,short y0,short x1,short y1)49 arc(short x, short y, short x0, short y0, short x1, short y1)
50 {
51 	double pc;
52 	short flg, m, xc, yc, xs, ys, qs, qf, qt, qtctr = 0;
53 	short m0, m1;
54 	float dx, dy, r;
55 	char use;
56 	dx = x - x0;
57 	dy = y - y0;
58 	r = (dx * dx) + (dy * dy);
59 	pc = r;
60 	pc = sqrt(pc);
61 	flg = (short)(pc / 4);
62 	if (flg == 0)
63 		step(1);
64 	else if (flg < del)
65 		step(flg);
66 	xc = xs = x0;
67 	yc = ys = y0;
68 	move(xs, ys);
69 	if ((x0 == x1) && (y0 == y1))
70 		flg = 0;
71 	else flg = 1;
72 	qs = quad_l(x, y, x0, y0);
73 	qf = quad_l(x, y, x1, y1);
74 	if ((abs(x - x1)) < (abs(y - y1))) {
75 		use = 'x';
76 		if ((qs == 2) || (qs == 3))
77 			m = -1;
78 		else m = 1;
79 	} else {
80 		use = 'y';
81 		if (qs > 2)
82 			m = -1;
83 		else m = 1;
84 	}
85 	if (qs == qf) {
86 		m0 = (y0 - y) / (x0 - x);
87 		m1 = (y1 - y) / (x1 - x);
88 		if (m0 >= m1)
89 			qt = 4;
90 		else qt = 0;
91 	} else if ((qt = qf - qs) < 0)
92 		qt += 4;
93 	/* LINTED */
94 	while (1) {
95 		switch (use) {
96 		case 'x':
97 			if ((qs == 2) || (qs == 3))
98 				yc -= del;
99 			else yc += del;
100 			dy = yc - y;
101 			pc = r - dy * dy;
102 			xc = (short)(m * sqrt(pc) + x);
103 			if (((x < xs) && (x >= xc)) ||
104 			    ((x > xs) && (x <= xc)) ||
105 			    ((y < ys) && (y >= yc)) ||
106 			    ((y > ys) && (y <=  yc))) {
107 				if (++qtctr > qt)
108 					return;
109 				if (++qs > 4)
110 					qs = 1;
111 				if ((qs == 2) || (qs == 3))
112 					m = -1;
113 				else m = 1;
114 				flg = 1;
115 			}
116 			cont(xc, yc);
117 			xs = xc;
118 			ys = yc;
119 			if ((qs == qf) && (flg == 1))
120 				switch (qf) {
121 				case 3:
122 				case 4:
123 					if (xs >= x1)
124 						return;
125 					continue;
126 				case 1:
127 				case 2:
128 					if (xs <= x1)
129 						return;
130 				}
131 			continue;
132 		case 'y':
133 			if (qs > 2)
134 				xc += del;
135 			else xc -= del;
136 			dx = xc - x;
137 			pc = r - dx * dx;
138 			yc = (short)(m * sqrt(pc) + y);
139 			if (((x < xs) && (x >= xc)) ||
140 			    ((x > xs) && (x <= xc)) ||
141 			    ((y < ys) && (y >= yc)) ||
142 			    ((y > ys) && (y <= yc))) {
143 				if (++qtctr > qt)
144 					return;
145 				if (++qs > 4)
146 					qs = 1;
147 				if (qs > 2)
148 					m = -1;
149 				else m = 1;
150 				flg = 1;
151 			}
152 			cont(xc, yc);
153 			xs = xc;
154 			ys = yc;
155 			if ((qs == qf) && (flg == 1))
156 				switch (qs) {
157 				case 1:
158 				case 4:
159 					if (ys >= y1)
160 						return;
161 					continue;
162 				case 2:
163 				case 3:
164 					if (ys <= y1)
165 						return;
166 				}
167 		}
168 	}
169 }
170 
171 /*
172  * these is a local function not to be confused with the
173  * system wide quad
174  */
175 static int
quad_l(short x,short y,short xp,short yp)176 quad_l(short x, short y, short xp, short yp)
177 {
178 	if (x < xp)
179 		if (y <= yp)
180 			return (1);
181 		else return (4);
182 	else if (x > xp)
183 		if (y < yp)
184 			return (2);
185 		else return (3);
186 	else if (y < yp)
187 		return (2);
188 	else return (4);
189 }
190