-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclip.c
134 lines (120 loc) · 3.23 KB
/
clip.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
#include <stdio.h>
#include "points.h"
#include "xwin.h"
double xmin = 0.0;
double xmax = 0.0;
double ymin = 0.0;
double ymax = 0.0;
/* Cohen-Sutherland 2-D Clipping Algorithm */
/* See: Foley & Van Dam, p146-148 */
void clip_set(xn, xx, yn, yx)
double xn, xx, yn, yx;
{
xmin = xn;
xmax = xx;
ymin = yn;
ymax = yx;
}
void updatebounds(pd, x1, y1, x2, y2)
PLOTDAT *pd;
double x1, y1, x2, y2;
{
double xmin, xmax, ymin, ymax;
xmin=x1; xmax=x2;
if (x2 < x1) {
xmin = x2;
xmax = x1;
}
ymin=y1; ymax=y2;
if (y2 < y1) {
ymin = y2;
ymax = y1;
}
if (pd->boundsflag == 0) {
pd->bbxmin = xmin;
pd->bbxmax = xmax;
pd->bbymin = ymin;
pd->bbymax = ymax;
} else {
if (xmin < pd->bbxmin) pd->bbxmin = xmin;
if (xmax > pd->bbxmax) pd->bbxmax = xmax;
if (ymin < pd->bbymin) pd->bbymin = ymin;
if (ymax > pd->bbymax) pd->bbymax = ymax;
}
pd->boundsflag=1;
}
void clip(pd, x1, y1, x2, y2)
PLOTDAT *pd;
double x1, y1, x2, y2;
{
extern double xmin, xmax, ymin, ymax;
int debug=0;
int done=0;
int accept=0;
int code1=0;
int code2=0;
double tmp;
double xs1, xs2, ys1, ys2;
while (!done) {
/* compute "outcodes" */
code1=0;
if((ymax-y1) < 0) code1 += 1;
if((y1-ymin) < 0) code1 += 2;
if((xmax-x1) < 0) code1 += 4;
if((x1-xmin) < 0) code1 += 8;
code2=0;
if((ymax-y2) < 0) code2 += 1;
if((y2-ymin) < 0) code2 += 2;
if((xmax-x2) < 0) code2 += 4;
if((x2-xmin) < 0) code2 += 8;
if (debug) printf("code1: %d, code2: %d\n", code1, code2);
if (code1 & code2) {
if (debug) printf("trivial reject\n");
done++; /* trivial reject */
} else {
if ((accept = !((code1 | code2)))) {
if (debug) printf("trivial accept\n");
done++;
} else {
if (!code1) { /* x1,y1 inside box, so SWAP */
if (debug) printf("swapping\n");
tmp=y1; y1=y2; y2=tmp;
tmp=x1; x1=x2; x2=tmp;
tmp=code1; code1=code2; code2=tmp;
}
if (debug) printf("preclip: %g,%g %g,%g\n", x1, y1, x2, y2);
if (code1 & 1) { /* divide line at top */
x1 = x1 + (x2-x1)*(ymax-y1)/(y2-y1);
y1 = ymax;
} else if (code1 & 2) { /* divide line at bot */
x1 = x1 + (x2-x1)*(ymin-y1)/(y2-y1);
y1 = ymin;
} else if (code1 & 4) { /* divide line at right */
y1 = y1 + (y2-y1)*(xmax-x1)/(x2-x1);
x1 = xmax;
} else if (code1 & 8) { /* divide line at left */
y1 = y1 + (y2-y1)*(xmin-x1)/(x2-x1);
x1 = xmin;
}
if (debug) printf("after: %g,%g %g,%g\n", x1, y1, x2, y2);
}
}
}
if (debug) printf("accept = %d\n", accept);
if (accept) {
updatebounds(pd, x1, y1, x2, y2);
xs1 =(pd->urx-pd->llx)*(x1-xmin)/(xmax-xmin)+pd->llx;
ys1 =(pd->ury-pd->lly)*(y1-pd->ymin)/(pd->ymax-pd->ymin)+pd->lly;
xs2 =(pd->urx-pd->llx)*(x2-xmin)/(xmax-xmin)+pd->llx;
ys2 =(pd->ury-pd->lly)*(y2-pd->ymin)/(pd->ymax-pd->ymin)+pd->lly;
if (linenum != 0) {
xwin_draw_line(xs1,ys1,xs2,ys2);
} else {
xwin_draw_point(xs1,ys1);
xwin_draw_point(xs2,ys2);
}
// printf("%g %g\n", xs1,ys1);
// printf("%g %g\n", xs2,ys2);
// printf("jump\n");
}
}