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