-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclip_testDS.c
More file actions
154 lines (123 loc) · 3.61 KB
/
clip_testDS.c
File metadata and controls
154 lines (123 loc) · 3.61 KB
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
#include <FPT.h>
int counter = 0;
// Bounding Box For Line Segment
//(x1,y1) = start of the line
//(x2, y2) = the end of the line
int pointincontainer(double x1, double y1, double x2, double y2,
double xprime, double yprime) {
double left, top, right, bottom;
if (x1 < x2) {
left = x1;
right = x2;
} else {
left = x2;
right = x1;
}
if (y1 > y2) {
top = y1;
bottom = y2;
} else {
top = y2;
bottom = y1;
}
//printf("l:%lf, r:%lf, t:%lf, b:%lf\n", left, right, top, bottom);
//printf("x': %lf, y': %lf\n", xprime, yprime);
if ((xprime + 0.01) >= left && (xprime - 0.01) <= right &&
(yprime + 0.01) <= top && (yprime - 0.01) >= bottom) {
return 1;
} else {
return 0;
}
}
int looper(int i, int n) {
if (i == n - 1) {
return 0;
} else {
return i + 1;
}
}
//gets the slope of all the lines; returns them via modified array
void getslope(double *x, double *y, int z, double *slope) {
int i;
for (i = 0; i < z; i++) {
slope[i] = (y[looper(i, z)] - y[i]) / (x[looper(i, z)] - x[i]);
}
}
//finds the intersection of the all lines
//returns intersection coordinates via array
void f_intersect(double *px, double *py, int pn,
double *wx, double *wy, int wn, double *c) {
int i, j;
double yprime, xprime, wslope[100], pslope[100], c1, c2;
getslope(px, py, pn, pslope);
getslope(wx, wy, wn, wslope);
for (i = 0; i < wn; i++) {
for (j = 0; j < pn; j++) {
c1 = py[j] - pslope[j] * px[j]; //y = mx + c -> c = y -mx
c2 = wy[i] - wslope[i] * wx[i];
if ((pslope[j] - wslope[i]) == 0) { //prevents divison by 0
printf("Slope division = 0; ERROR!\n");
} else {
xprime = (c2 - c1) / (pslope[j] - wslope[i]); //intersection of x
yprime = pslope[j] * xprime + c1; //intersection of y
if (pointincontainer(px[j], py[j], px[looper(j, pn)], py[looper(j, pn)],
xprime, yprime) == 1 &&
pointincontainer(wx[i], wy[i], wx[looper(i, wn)], wy[looper(i, wn)],
xprime, yprime ) == 1) {
c[counter] = xprime; c[counter + 1] = yprime;
counter += 2;
}
}
}
}
}
int Clip_Polygon_Against_Convex_Window(double *px, double *py, int pn,
double *wx, double *wy, int wn) {
int j, i;
double cd[100], final;
f_intersect(px, py, pn, wx, wy, wn, cd);
double x[counter], y[counter]; //cleaning the array
for (i = 0; i < pn; i++){
px[i] = 0.0;
py[i] = 0.0;
}
for (i = 0; i < 100; i += 2) {
if (cd[i] > 0.1 && cd[i] < 700 && 0 == isnan(cd[i])
&& cd[i + 1] > 0.1 && cd[i + 1] < 700 && isnan(cd[i + 1]) == 0) {
px[i/2] = cd[i];
py[i/2] = cd[i + 1];
//printf("x:%.2lf, y:%.2lf\n", cd[i], cd[i + 1]);
}
}
return counter / 2;
}
int main()
// this tests clipping of polygon to convex window
//prefix -p == polygon; prefix -w == window
{
int pn, wn ;
double pt[2], u, v, q ;
double px[100] = { 70, 460, 400} ;
double py[100] = { 350, 25, 550} ;
pn = 3 ;
double wx[100] = { 100, 600, 550, 150} ;
double wy[100] = { 150, 200, 450, 500} ;
wn = 4 ;
srand48(100) ;
G_init_graphics (700, 700) ;
G_rgb (0, 0, 0) ;
G_clear() ;
G_rgb (1, 0, 0) ;
G_polygon(wx, wy, wn) ;
G_rgb (0, 0, 1) ;
G_polygon(px, py, pn) ;
q = G_wait_key() ;
pn = Clip_Polygon_Against_Convex_Window (px, py, pn, wx, wy, wn) ;
G_rgb (1, 1, 0) ;
for (int i = 0; i < pn; i++) {
G_fill_circle(px[i], py[i], 2);
printf("x:%.2lf, y:%.2lf\n", px[i], py[i]);
}
G_fill_polygon(px, py, pn) ;
q = G_wait_key() ;
}