xref: /onnv-gate/usr/src/lib/libplot/t4014/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 <math.h>
36 #include <plot.h>
37 #include "con.h"
38 
39 static int del = 20;
40 
41 static void
step(int d)42 step(int d)
43 {
44 	del = d;
45 }
46 
47 /*
48  * local definition of quad
49  * legacy code
50  */
51 static short
quad_l(short x,short y,short xp,short yp)52 quad_l(short x, short y, short xp, short yp)
53 {
54 	if (x < xp)
55 		if (y <= yp)
56 			return (1);
57 		else return (4);
58 	else if (x > xp)
59 		if (y < yp)
60 			return (2);
61 		else return (3);
62 	else if (y < yp)
63 		return (2);
64 	else return (4);
65 }
66 
67 void
arc(short x,short y,short x0,short y0,short x1,short y1)68 arc(short x, short y, short x0, short y0, short x1, short y1)
69 {
70 	double pc;
71 	int flg, m, xc, yc, xs, ys, qs, qf, qt, qtctr = 0;
72 	int m0, m1;
73 	float dx, dy, r;
74 	char use;
75 	dx = x - x0;
76 	dy = y - y0;
77 	r = dx * dx + dy * dy;
78 	pc = r;
79 	pc = sqrt(pc);
80 	flg = (int)(pc / 4);
81 	if (flg == 0)
82 		step(1);
83 	else if (flg < del)
84 		step(flg);
85 	xc = xs = x0;
86 	yc = ys = y0;
87 	move((short)xs, (short)ys);
88 	if ((x0 == x1) && (y0 == y1))
89 		flg = 0;
90 	else flg = 1;
91 	qs = quad_l(x, y, x0, y0);
92 	qf = quad_l(x, y, x1, y1);
93 	if (abs(x - x1) < abs(y - y1)) {
94 		use = 'x';
95 		if ((qs == 2) || (qs == 3))
96 			m = -1;
97 		else m = 1;
98 	} else {
99 		use = 'y';
100 		if (qs > 2)
101 			m = -1;
102 		else m = 1;
103 	}
104 	if (qs == qf) {
105 		m0 = (y0 - y) / (x0 - x);
106 		m1 = (y1 - y) / (x1 - x);
107 		if (m0 >= m1)
108 			qt = 4;
109 		else qt = 0;
110 	} else if ((qt = qf - qs) < 0)
111 			qt += 4;
112 	/* LINTED */
113 	while (1) {
114 		switch (use) {
115 		case 'x':
116 			if ((qs == 2) || (qs == 3))
117 				yc -= del;
118 			else yc += del;
119 			dy = yc - y;
120 			pc = r - dy * dy;
121 			xc = (int)(m * sqrt(pc) + x);
122 			if (((x < xs) && (x >= xc)) ||
123 			    ((x > xs) && (x <= xc)) ||
124 			    ((y < ys) && (y >= yc)) ||
125 			    ((y > ys) && (y <= yc))) {
126 				if (++qtctr > qt)
127 					return;
128 				if (++qs > 4)
129 					qs = 1;
130 				if ((qs == 2) || (qs == 3))
131 					m = -1;
132 				else m = 1;
133 				flg = 1;
134 			}
135 			cont((short)xc, (short)yc);
136 			xs = xc;
137 			ys = yc;
138 			if ((qs == qf) && (flg == 1))
139 				switch (qf) {
140 				case 3:
141 				case 4:
142 					if (xs >= x1)
143 						return;
144 					continue;
145 				case 1:
146 				case 2:
147 					if (xs <= x1)
148 						return;
149 				}
150 			continue;
151 		case 'y':
152 			if (qs > 2)
153 				xc += del;
154 			else xc -= del;
155 			dx = xc - x;
156 			pc = r - dx * dx;
157 			yc = (int)(m * sqrt(pc) + y);
158 			if (((x < xs) && (x >= xc)) ||
159 			    ((x > xs) && (x <= xc)) ||
160 			    ((y < ys) && (y >= yc)) ||
161 			    ((y > ys) && (y <= yc))) {
162 				if (++qtctr > qt)
163 					return;
164 				if (++qs > 4)
165 					qs = 1;
166 				if (qs > 2)
167 					m = -1;
168 				else m = 1;
169 				flg = 1;
170 			}
171 			cont((short)xc, (short)yc);
172 			xs = xc;
173 			ys = yc;
174 			if ((qs == qf) && (flg == 1))
175 				switch (qs) {
176 				case 1:
177 				case 4:
178 					if (ys >= y1)
179 						return;
180 					continue;
181 				case 2:
182 				case 3:
183 					if (ys <= y1)
184 						return;
185 				}
186 		}
187 	}
188 }
189