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