xref: /original-bsd/lib/libplot/t300s/subr.c (revision f1d75c93)
1 /*-
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)subr.c	4.3 (Berkeley) 02/10/93";
10 #endif /* not lint */
11 
12 #include <stdio.h>
13 #include "con.h"
14 abval(q)
15 {
16 	return (q>=0 ? q : -q);
17 }
18 
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 
27 yconv (yp)
28 {
29 	/* see description of xconv */
30 	yp += 2048;
31 	return (yp / yscale);
32 }
33 
34 inplot()
35 {
36 	stty(OUTF, &PTTY);
37 	spew(ESC);
38 	spew (INPLOT);
39 }
40 
41 outplot()
42 {
43 	spew(ESC);
44 	spew(ACK);
45 	spew(ESC);
46 	spew(ACK);
47 	fflush(stdout);
48 	stty (OUTF, &ITTY);
49 }
50 
51 spew(ch)
52 {
53 	putc(ch, stdout);
54 }
55 
56 tobotleft ()
57 {
58 	move(-2048,-2048);
59 }
60 reset()
61 {
62 	outplot();
63 	exit();
64 }
65 
66 double
67 dist2 (x1, y1, x2, y2)
68 {
69 	float t,v;
70 	t = x2-x1;
71 	v = y1-y2;
72 	return (t*t+v*v);
73 }
74 
75 swap (pa, pb)
76 int *pa, *pb;
77 {
78 	int t;
79 	t = *pa;
80 	*pa = *pb;
81 	*pb = t;
82 }
83 
84 #define DOUBLE 010
85 #define ADDR 0100
86 #define COM 060
87 #define MAXX 070
88 #define MAXY 07
89 extern xnow,ynow;
90 #define SPACES 7
91 movep(ix,iy){
92 	int dx,dy,remx,remy,pts,i;
93 	int xd,yd;
94 	int addr,command;
95 	char c;
96 	if(xnow == ix && ynow == iy)return;
97 	inplot();
98 	dx = ix-xnow;
99 	dy = iy-ynow;
100 	command = COM|PENUP|((dx<0)<<1)|(dy<0);
101 	dx = abval(dx);
102 	dy = abval(dy);
103 	xd = dx/(SPACES*2);
104 	yd = dy/(SPACES*2);
105 	pts = xd<yd?xd:yd;
106 	if((i=pts)>0){
107 		c=command|DOUBLE;
108 		addr=ADDR;
109 		if(xd>0)addr|=MAXX;
110 		if(yd>0)addr|=MAXY;
111 		spew(c);
112 		while(i--){
113 			spew(addr);
114 		}
115 	}
116 	if(xd!=yd){
117 		if(xd>pts){
118 			i=xd-pts;
119 			addr=ADDR|MAXX;
120 		}
121 		else{
122 			i=yd-pts;
123 			addr=ADDR|MAXY;
124 		}
125 		c=command|DOUBLE;
126 		spew(c);
127 		while(i--){
128 			spew(addr);
129 		}
130 	}
131 	remx=dx-xd*SPACES*2;
132 	remy=dy-yd*SPACES*2;
133 	addr=ADDR;
134 	i = 0;
135 	if(remx>7){
136 		i=1;
137 		addr|=MAXX;
138 		remx -= 7;
139 	}
140 	if(remy>7){
141 		i=1;
142 		addr|=MAXY;
143 		remy -= 7;
144 	}
145 	while(i--){
146 		spew(command);
147 		spew(addr);
148 	}
149 	if(remx>0||remy>0){
150 		spew(command);
151 		spew(ADDR|remx<<3|remy);
152 	}
153 	xnow=ix;
154 	ynow=iy;
155 	outplot();
156 	return;
157 }
158 xsc(xi){
159 	int xa;
160 	xa = (xi - obotx) * scalex + botx;
161 	return(xa);
162 }
163 ysc(yi){
164 	int ya;
165 	ya = (yi - oboty) *scaley +boty;
166 	return(ya);
167 }
168