-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgeom_line.c
298 lines (258 loc) · 7.22 KB
/
geom_line.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* FIXME: "^" function is only partially working and is quite flaky */
/* it doesn't back up until you move the mouse (?) and can eat up */
/* the last coord and cause a segmentation violation */
#include "db.h"
#include "token.h"
#include "xwin.h"
#include "rubber.h"
#include <math.h>
#include <string.h> /* for strncmp */
#include "rlgetc.h"
#include "opt_parse.h"
#include "lock.h"
#include "ev.h"
static COORDS *CP;
// DB_TAB dbtab;
DB_LINE dbline;
void draw_line();
/*
*
* +--------------+---------<--+---------------+
* v v ^ |
* (ADD)--0---(R)------+-1------------+--|xy|---|xy|----|prim|->
* | | | | |cmd |
* +-(R)<layer>-+ +-(:W<width>-+
* | |
* +-(:FILL)----+
*/
OPTS lopts;
double x1=0.0;
double yy1=0.0;
void cleanup(COORDS *CP) {
int n;
int i;
double x,y;
n=coord_count(CP);
// printf("found %d points in line\n",n);
for (i=1; i<=n; i++) {
coord_get(CP, i, &x, &y);
// printf("point %d: %g %g\n",i,x,y);
}
}
int add_line(LEXER *lp, int *layer)
{
enum {START,NUM1,NUM2,END,ERR} state = START;
int count;
TOKEN token;
char *word;
double x2,y2;
double xold, yold;
int debug=0;
int done=0;
int nsegs=0;
if (debug) {printf("layer %d\n",*layer);}
rl_saveprompt();
rl_setprompt("ADD_LINE> ");
x2 = y2 = xold = yold = 0.0;
opt_set_defaults(&lopts);
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 */
db_checkpoint(lp);
nsegs=0;
if (debug) printf("in start\n");
if (token == OPT ) {
token_get(lp,&word);
if (opt_parse(word, LINE_OPTS, &lopts) == -1) {
state = END;
} else {
state = START;
}
} else if (token == NUMBER) {
state = NUM1;
} else if (token == EOL || token == EOC) {
token_get(lp,&word); /* just eat it up */
state = START;
} else if (token == CMD || token==EOF) {
state = END;
} else {
token_err("LINE", lp, "expected OPT or COORD", token);
state = END;
}
xold=yold=0.0;
break;
case NUM1: /* get pair of xy coordinates */
xold=x1;
yold=yy1;
if (token == NUMBER) {
if (getnum(lp, "LINE", &x1, &yy1)) {
nsegs++;
CP = coord_new(x1,yy1);
coord_append(CP, x1,yy1);
setlockpoint(x1,yy1);
if (debug) coord_print(CP);
rubber_set_callback(draw_line);
x2 = x1; y2 = yy1;
state = NUM2;
} else {
state = END;
}
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == EOC || CMD) {
state = END;
} else {
token_err("LINE", lp, "expected NUMBER", token);
state = END;
}
break;
case NUM2: /* get pair of xy coordinates */
if (debug) printf("in num2\n");
xold=x2;
yold=y2;
if (token == NUMBER) {
if (getnum(lp, "LINE", &x2, &y2)) {
nsegs++;
/* two identical clicks terminates this line */
/* but keeps the ADD L command in effect */
// printf("%d: xo-x2 = %le, yo-y2 = %le\n", nsegs, xold-x2, yold-y2);
if (nsegs==1 && x1==x2 && yy1==y2) {
rubber_clear_callback();
printf("error: a line must have finite length\n");
state = START;
} else if (x2==xold && y2==yold && nsegs > 0) {
if (debug) coord_print(CP);
printf("dropping coord\n");
coord_drop(CP); /* drop last coord */
if (debug) coord_print(CP);
if (debug) printf("got %d coords\n", coord_count(CP));
if (coord_count(CP) > 1) {
cleanup(CP); // clean up coords
db_add_line(currep, *layer, opt_copy(&lopts), CP);
}
need_redraw++;
rubber_clear_callback();
state = START;
} else {
rubber_clear_callback();
if (debug) printf("doing append\n");
/* only apply lock if this is an interactive edit */
/* and not if reading from a file */
if (strcmp(lp->name, "STDIN") == 0) {
lockpoint(&x2, &y2, currep->lock_angle);
}
coord_swap_last(CP, x2, y2);
coord_append(CP, x2,y2);
setlockpoint(x2,y2);
rubber_set_callback(draw_line);
rubber_draw(x2,y2, 0);
state = NUM2; /* loop till EOC */
}
} else {
state = END;
}
} else if (token == EOL) {
token_get(lp,&word); /* just ignore it */
} else if (token == BACK) {
token_get(lp,&word); /* eat it */
if (debug) printf("dropping coord\n");
rubber_clear_callback();
rubber_draw(x2, y2, 0);
// printf("num coords %d\n", coord_count(CP));
if ((count = coord_count(CP)) >= 3) {
coord_drop(CP); /* drop last coord */
coord_swap_last(CP, x2, y2);
coord_get(CP, count-2, &x2, &y2);
setlockpoint(x2,y2);
} else {
printf("can't drop last point!\n");
}
rubber_set_callback(draw_line);
rubber_draw(x2, y2, 0);
} else if (token == CMD || token == EOC) {
state = END;
} else {
token_err("LINE", lp, "expected NUMBER", token);
state = END;
}
break;
case END:
if (debug) printf("in end\n");
if (token == EOC && nsegs >= 2) {
coord_drop(CP); /* drop last coord */
cleanup(CP); // clean up coords
db_add_line(currep, *layer, opt_copy(&lopts), CP);
need_redraw++;
; /* add geom */
} else if (token == EOC || token == CMD || token == EOF) {
done++; /* should a CMD terminate a line? */
} else {
token_flush_EOL(lp);
}
rubber_clear_callback();
state=START;
break;
default:
break;
}
}
rl_restoreprompt();
return(1);
}
void draw_line(double x2, double y2, int count)
{
double xl, yl;
int debug=0;
BOUNDS bb;
static int called = 0;
bb.init=0;
static DB_DEFLIST dbdeflist;
/* DB_TAB dbtab; */
/* DB_DEFLIST dbdeflist; */
/* DB_LINE dbline; */
getlockpoint(&xl, &yl);
lockpoint(&x2, &y2, currep->lock_angle);
// can check here to see if new point is collinear
// with last and previous to last point
// if not, then find new point and previous point
// such that p2 is p1+alpha(p1->lp) and
// p2 + beta(lp->mousepoint) == mousepoint
// dbtab.dbhead = &dbdeflist;
// dbtab.next = NULL;
// dbtab.name = "callback";
dbdeflist.u.l = &dbline;
dbdeflist.type = LINE;
dbline.layer=1;
dbline.opts=&lopts;
dbline.coords=CP;
if (debug) {printf("in draw_line\n");}
if (count == 0) { /* first call */
jump(&bb, D_RUBBER); /* draw new shape */
do_line(&dbdeflist, &bb, D_RUBBER);
called++;
} else if (count > 0) { /* intermediate calls */
jump(&bb, D_RUBBER); /* erase old shape */
do_line(&dbdeflist, &bb, D_RUBBER);
jump(&bb, D_RUBBER); /* draw new shape */
coord_edit(CP, 0, x2, y2); // change the last point
do_line(&dbdeflist, &bb, D_RUBBER);
called++;
} else { /* last call, cleanup */
if (called) {
jump(&bb, D_RUBBER); /* erase old shape */
do_line(&dbdeflist, &bb, D_RUBBER);
}
called = 0;
}
/* save old values */
// x1old=x1;
// y1old=yy1;
// x2old=x2;
// y2old=y2;
jump(&bb, D_RUBBER);
}