xref: /original-bsd/lib/libplot/t450/subr.c (revision c3e32dec)
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"
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 	spew (ESC);
37 	spew(PLOTIN);
38 }
39 
40 outplot()
41 {
42 	spew(ESC);
43 	spew(PLOTOUT);
44 	fflush(stdout);
45 }
46 
47 spew(ch)
48 {
49 	if(ch == UP){
50 		putc(ESC,stdout);
51 		ch = DOWN;
52 	}
53 	putc(ch, stdout);
54 }
55 
56 tobotleft ()
57 {
58 	move(-2048,-2048);
59 }
60 reset()
61 {
62 	signal(2,1);
63 	outplot();
64 	stty(OUTF,&ITTY);
65 	exit();
66 }
67 
68 double
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 
77 swap (pa, pb)
78 int *pa, *pb;
79 {
80 	int t;
81 	t = *pa;
82 	*pa = *pb;
83 	*pb = t;
84 }
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 
118 xsc(xi){
119 	int xa;
120 	xa = (xi - obotx) * scalex + botx;
121 	return(xa);
122 }
123 ysc(yi){
124 	int ya;
125 	ya = (yi - oboty) *scaley +boty;
126 	return(ya);
127 }
128