-
Notifications
You must be signed in to change notification settings - Fork 0
/
polylab.c
143 lines (100 loc) · 2.51 KB
/
polylab.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
#include <FPT.h>
double gap = 20 ;
void sort(double *x, int n){
int k,s,i;
double t;
for(k=0; k<n; k++){
s=k;
for(i = k+1; i<n; i++){
if(x[i]<x[s]){
s=i;
}
}
t=x[k];x[k]=x[s];x[s]=t;
}
}
void my_fill_polygon(double *x, double *y, int numpoints){
int j; //index of y-point arrays
double i; //y value of horizontal line
int h=0; //index of sorted xavlue array
// --double intersection[][2]; //array of points of intersection
int k=0; //increasing j and necessary to set loop back to 0
double xvalues[100];
for(i=0.1; i<600; i++){
//instead of going from 0 to 600 just find the min value and the max value of y array
double slope=0;
int t=0; //index of xvalues array
for(j=0; j<numpoints;j++){
//k=(j+1)%numpoints; just another way to set k back to 0 at the end of loop
k=j+1; if(k==numpoints) {j=0;}
if((i>=y[j] && i<=y[k]) || (i<=y[j] && i>=y[k])){
slope=((y[j]-y[k])/(x[j]-x[k]));
xvalues[t]=(i-y[j]+x[j]*slope)/slope;
// --intersection[t][0]=(i-y[j]+x[j]*slope)/slope;
// --intersection[t][1]=i; double array not necessary bc y always the same
t++;
}
}
sort(xvalues,t);
for(h=0;h<t;h++){
if((h+1)%2==1){
G_rgb(0,0,1);
G_line(xvalues[h],i,xvalues[h+1],i);
}
}
}
}
void grid()
{
int i ;
for (i = 0 ; i < 600 ; i+= gap) {
G_line(i,0, i,600) ;
G_line(0,i, 600,i) ;
}
}
int click_and_save(double *x, double *y)
{
double xy[2] ;
int numpoints ;
double xc,yc ;
G_rgb(1, 0, 0) ; // red
G_fill_rectangle(0,0, 20,10) ;
numpoints = 0 ;
while (0 == 0) {
G_wait_click(xy) ;
if ((xy[0] >= 0) && (xy[0] <= 20) &&
(xy[1] >= 0) && (xy[1] <= 10)) { break ; }
G_rgb(1, 1, 0) ; // yellow
xc = gap*floor((xy[0]+0.5*gap)/gap) ;
yc = gap*floor((xy[1]+0.5*gap)/gap) ;
G_circle(xc, yc, 3) ;
x[numpoints] = xc ; y[numpoints] = yc ;
if (numpoints > 0) {
G_line (x[numpoints-1],y[numpoints-1], x[numpoints],y[numpoints]) ;
}
numpoints++ ;
}
return numpoints ;
}
int main()
{
int q ;
double xp[1000],yp[1000] ;
int np ;
int k ;
G_init_graphics (600,600) ;
do {
G_rgb(0,0,0) ;
G_clear() ;
G_rgb(1,0.5,0) ;
grid() ;
np = click_and_save(xp,yp) ;
G_rgb(1,0,0) ;
my_fill_polygon(xp,yp,np) ;
G_rgb(0,1,0) ;
for (k = 0 ; k < np ; k++) {
G_circle(xp[k],yp[k],2) ;
}
q = G_wait_key() ;
} while (q != 'q') ;
}