1 /*-
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.proprietary.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)subr.c 8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11
12 #include <stdio.h>
13 #include "con.h"
abval(q)14 abval(q)
15 {
16 return (q>=0 ? q : -q);
17 }
18
xconv(xp)19 xconv (xp)
20 {
21 /* x position input is -2047 to +2047, output must be 0 to PAGSIZ*HORZRES */
22 xp += 2048;
23 /* the computation is newx = xp*(PAGSIZ*HORZRES)/4096 */
24 return (xoffset + xp /xscale);
25 }
26
yconv(yp)27 yconv (yp)
28 {
29 /* see description of xconv */
30 yp += 2048;
31 return (yp / yscale);
32 }
33
inplot()34 inplot()
35 {
36 spew (ESC);
37 spew(PLOTIN);
38 }
39
outplot()40 outplot()
41 {
42 spew(ESC);
43 spew(PLOTOUT);
44 fflush(stdout);
45 }
46
spew(ch)47 spew(ch)
48 {
49 if(ch == UP){
50 putc(ESC,stdout);
51 ch = DOWN;
52 }
53 putc(ch, stdout);
54 }
55
tobotleft()56 tobotleft ()
57 {
58 move(-2048,-2048);
59 }
reset()60 reset()
61 {
62 signal(2,1);
63 outplot();
64 stty(OUTF,&ITTY);
65 exit();
66 }
67
68 double
dist2(x1,y1,x2,y2)69 dist2 (x1, y1, x2, y2)
70 {
71 float t,v;
72 t = x2-x1;
73 v = y1-y2;
74 return (t*t+v*v);
75 }
76
swap(pa,pb)77 swap (pa, pb)
78 int *pa, *pb;
79 {
80 int t;
81 t = *pa;
82 *pa = *pb;
83 *pb = t;
84 }
movep(xg,yg)85 movep (xg,yg)
86 {
87 int i,ch;
88 if((xg == xnow) && (yg == ynow))return;
89 /* if we need to go to left margin, just CR */
90 if (xg < xnow/2)
91 {
92 spew(CR);
93 xnow = 0;
94 }
95 i = (xg-xnow)/HORZRES;
96 if(xnow < xg)ch = RIGHT;
97 else ch = LEFT;
98 xnow += i*HORZRES;
99 i = abval(i);
100 while(i--)spew(ch);
101 i = abval(xg-xnow);
102 inplot();
103 while(i--) spew(ch);
104 outplot();
105 i=(yg-ynow)/VERTRES;
106 if(ynow < yg)ch = UP;
107 else ch = DOWN;
108 ynow += i*VERTRES;
109 i = abval(i);
110 while(i--)spew(ch);
111 i=abval(yg-ynow);
112 inplot();
113 while(i--)spew(ch);
114 outplot();
115 xnow = xg; ynow = yg;
116 }
117
xsc(xi)118 xsc(xi){
119 int xa;
120 xa = (xi - obotx) * scalex + botx;
121 return(xa);
122 }
ysc(yi)123 ysc(yi){
124 int ya;
125 ya = (yi - oboty) *scaley +boty;
126 return(ya);
127 }
128