-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcom_distance.c
210 lines (191 loc) · 5.53 KB
/
com_distance.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <string.h>
#include <math.h>
#include "db.h"
#include "xwin.h"
#include "token.h"
#include "rubber.h"
#include "rlgetc.h"
#define UNUSED(x) (void)(x)
#define MAXDBUF 20
/*
*
* DISTANCE xypnt1 xypnt2
* Computes and display both the orthogonal and diagonal distances
* no refresh is done.
*
*/
static double x1, yy1; /* cant call it "y1" because of math lib */
void draw_dist();
int com_distance(LEXER *lp, char *arg)
{
UNUSED(arg);
enum {START,NUM1,NUM2,END} state = START;
double x2,y2;
int done=0;
TOKEN token;
char *word;
int debug=0;
while (!done) {
token = token_look(lp,&word);
if (debug) printf("got %s: %s\n", tok2str(token), word);
if (token==CMD) {
state=END;
}
switch(state) {
case START: /* get option or first xy pair */
if (token == OPT ) {
token_get(lp,&word); /* ignore for now */
state = START;
} else if (token == NUMBER) {
state = NUM1;
} else if (token == EOL) {
token_get(lp,&word); /* just eat it up */
state = START;
} else if (token == EOC || token == CMD) {
state = END;
} else {
token_err("DISTANCE", lp, "expected NUMBER", token);
state = END; /* error */
}
break;
case NUM1:
if (token==NUMBER) {
if (getnum(lp, "DISTANCE", &x1, &yy1)) {
rubber_set_callback(draw_dist);
state = NUM2;
} else {
state = END;
}
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == EOC || token == CMD) {
printf("DISTANCE: cancelling DISTANCE\n");
state = END;
} else {
token_err("DISTANCE", lp, "expected NUMBER", token);
state = END;
}
break;
case NUM2:
if (token==NUMBER) {
if (getnum(lp, "DISTANCE", &x2, &y2)) {
printf("xy1=(%g,%g) xy2=(%g,%g) dx=%g, dy=%g, dxy=%g theta=%g (deg.)\n",
x1, yy1, x2, y2, fabs(x1-x2), fabs(yy1-y2),
sqrt(pow((x1-x2),2.0)+pow((yy1-y2),2.0)),
360.0*atan2(y2-yy1, x2-x1)/(2.0*M_PI));
rubber_clear_callback();
state = NUM1;
} else {
state = END;
}
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == EOC || token == CMD) {
printf("DISTANCE: cancelling DISTANCE\n");
state = END;
} else {
token_err("DISTANCE", lp, "expected NUMBER", token);
state = END;
}
break;
case END:
default:
if (token == EOC || token == CMD) {
;
} else {
token_flush_EOL(lp);
}
rubber_clear_callback();
done++;
break;
}
}
return(1);
}
// #define RES 6 /* defined in db.h */
/* return nearest exact multiple of 1/(10^RES)) */
double grid(double num) {
// return (num-fmod(num,1.0/pow(10.0,RES)));
return ((double)((int)(num*pow(10.0,RES)+0.5)))/pow(10.0,RES);
}
void draw_dist(double x2, double y2, int count)
{
static double x1old, x2old, y1old, y2old;
static char dxbuf[MAXDBUF];
static char dybuf[MAXDBUF];
static char dxybuf[MAXDBUF];
static char dxbufold[MAXDBUF];
static char dybufold[MAXDBUF];
static char dxybufold[MAXDBUF];
double dxval,dyval,dxyval;
BOUNDS bb;
bb.init=0;
dxbuf[0]='\0';
dybuf[0]='\0';
dxybuf[0]='\0';
dxval=grid(fabs(x1-x2));
dyval=grid(fabs(yy1-y2));
dxyval=grid(sqrt(pow((x1-x2),2.0)+pow((yy1-y2),2.0)));
if (dxval!=0.0 && dxval != dxyval) {
snprintf(dxbuf, MAXDBUF, "%g", dxval);
}
if (dyval!=0 && dyval != dxyval) {
snprintf(dybuf, MAXDBUF, "%g", dyval);
}
if (dxyval!=0) {
snprintf(dxybuf, MAXDBUF, "%g", dxyval);
}
if (count == 0) { /* first call */
jump(&bb, D_RUBBER); /* draw new shape */
draw(x1,yy1, &bb, D_RUBBER);
draw(x2,y2, &bb, D_RUBBER);
draw(x2,yy1, &bb, D_RUBBER);
draw(x1,yy1, &bb, D_RUBBER);
xwin_draw_point(x1, yy1);
xwin_draw_point(x2, y2);
xwin_draw_text(x2, (yy1+2.0*y2)/3.0, dybuf);
xwin_draw_text((x1+x2)/2.0, yy1, dxbuf);
xwin_draw_text((2.0*x1+x2)/3.0, (2.0*yy1+y2)/3.0, dxybuf);
} else if (count > 0) { /* intermediate calls */
jump(&bb, D_RUBBER); /* erase old shape */
draw(x1old,y1old, &bb, D_RUBBER);
draw(x2old,y2old, &bb, D_RUBBER);
draw(x2old,y1old, &bb, D_RUBBER);
draw(x1old,y1old, &bb, D_RUBBER);
xwin_draw_point(x1old, y1old);
xwin_draw_point(x2old, y2old);
xwin_draw_text(x2old, (y1old+2.0*y2old)/3.0, dybufold);
xwin_draw_text((x1old+x2old)/2.0, y1old, dxbufold);
xwin_draw_text((2.0*x1old+x2old)/3.0, (2.0*y1old+y2old)/3.0, dxybufold);
jump(&bb, D_RUBBER); /* draw new shape */
draw(x1,yy1, &bb, D_RUBBER);
draw(x2,y2, &bb, D_RUBBER);
draw(x2,yy1, &bb, D_RUBBER);
draw(x1,yy1, &bb, D_RUBBER);
xwin_draw_text(x2, (yy1+2.0*y2)/3.0, dybuf);
xwin_draw_text((x1+x2)/2.0, yy1, dxbuf);
xwin_draw_text((2.0*x1+x2)/3.0, (2.0*yy1+y2)/3.0, dxybuf);
xwin_draw_point(x1, yy1);
xwin_draw_point(x2, y2);
} else { /* last call, cleanup */
jump(&bb, D_RUBBER); /* erase old shape */
draw(x1old,y1old, &bb, D_RUBBER);
draw(x2old,y2old, &bb, D_RUBBER);
draw(x2old,y1old, &bb, D_RUBBER);
draw(x1old,y1old, &bb, D_RUBBER);
xwin_draw_point(x1old, y1old);
xwin_draw_point(x2old, y2old);
xwin_draw_text(x2old, (y1old+2.0*y2old)/3.0, dybufold);
xwin_draw_text((x1old+x2old)/2.0, y1old, dxbufold);
xwin_draw_text((2.0*x1old+x2old)/3.0, (2.0*y1old+y2old)/3.0, dxybufold);
}
/* save old values */
x1old=x1;
y1old=yy1;
x2old=x2;
y2old=y2;
strncpy(dxbufold, dxbuf, MAXDBUF);
strncpy(dybufold, dybuf, MAXDBUF);
strncpy(dxybufold, dxybuf, MAXDBUF);
jump(&bb, D_RUBBER);
}