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