- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab3.c
More file actions
Latest commit
315 lines (271 loc) · 7.89 KB
/
Copy pathlab3.c
File metadata and controls
315 lines (271 loc) · 7.89 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include<FPT.h>
#include<D2d_matrix.h>
intWIDTH=500; intHEIGHT=500;
intcounter=0;
intpoints[100]; //number of points in whatever
doublex[100][100], y[100][100]; //x and y coordinates
intpolys[10];
intshapes[100][100]; //the shapes[what argument][which shape]
intshapeorder[100][100][100]; //where things connect to
doublered[100][100], green[100][100], blue[100][100]; //individual colors of each[what arg][which shape]
intboxheight, boxwidth, centerx, centery;
doublesmallx, smally, bigx, bigy; //specific smallest & largest (X,Y)'s
//determines if a point is on one side of the line or the other
doublewhatside(doubleax, doubleay, doublebx,
doubleby, doublex1, doubley1,
doublex2, doubley2) {
return ((y1-y2) * (ax-x1) + (x2-x1) * (ay-y1)) *
((y1-y2) * (bx-x1) + (x2-x1) * (by-y1));
}
//Bounding Box For Line Segment
//(x1,y1) = start of the line
//(x2, y2) = the end of the line
intpointincontainer(doublex1, doubley1, doublex2, doubley2,
doublexprime, doubleyprime) {
doubleleft, 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) {
return1;
} else {
return0;
}
}
intlooper(inti, intn) {
if (i==n-1) {
return0;
} else {
returni+1;
}
}
//gets the slope of all the lines; returns them via modified array
voidgetslope(double*x, double*y, intz, double*slope) {
inti;
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
voidf_intersect(double*px, double*py, intpn,
double*wx, double*wy, intwn, double*c) {
inti, j;
doubleyprime, 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 by 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;
G_wait_key();
G_rgb(0,0,0);
G_fill_circle(xprime,yprime, 2);
counter+=2;
}
}
}
}
}
//click a point and it saves it into an array
intclickAndSave(double*x, double*y) {
doubleca[2];
inti=0;
intgoing=1;
G_rgb(0, 0, 0);;
G_draw_string("Click here to end clipping", 0, 0);
while (going) {
G_wait_click(ca);
//printf("%lf, %lf\n", ca[0], ca[1]);
if (ca[0] <260&ca[1] <15) {
going=0;
x[i] =x[0];
y[i] =y[0];
//printf("done\n");
} else {
x[i] =ca[0];
y[i] =ca[1];
G_rgb(0, 1, 1);
G_circle(x[i], y[i], 1);
G_rgb(0, 0, 0);
G_circle(x[i], y[i], 2);
i++;
//printf(" %d\n", i);
}
}
returni;
}
//draws the polygon; unfilled
voidmyPolygon(double*x, double*y, intn) {
inti=0;
while (i <= n) {
if (i!=n) {
G_line(x[i], y[i], x[i+1], y[i+1]);
}
i++;
}
}
//Creates bounding box around figure
doubleboundingbox(inti) {
intk;
smallx=WIDTH*2; smally=HEIGHT*2;
bigx=WIDTH*-2; bigy=HEIGHT*-2;
for (k=0; k<points[i]; k++) {
if (x[i][k] <smallx) {
smallx=x[i][k];
}
elseif (x[i][k] >bigx) {
bigx=x[i][k];
}
if (y[i][k] <smally) {
smally=y[i][k];
}
elseif (y[i][k] >bigy) {
bigy=y[i][k];
}
}
boxheight=bigy-smally;
boxwidth=bigx-smallx;
centerx= (bigx+smallx) / 2;
centery= (smally+bigy) / 2;
if (WIDTH / boxwidth<HEIGHT / boxheight) {
returnWIDTH / boxwidth;
} else {
returnHEIGHT / boxheight;
}
}
//reads the file and attaches the correct variable to each input
voidreadobject(FILE*g, inti) {
intk, j;
fscanf(g, "%d", &points[i]);
//printf("there are %d points\n", points[i]);
for (k=0; k<points[i]; k++) {
fscanf(g, "%lf %lf", &x[i][k], &y[i][k]);
//printf("(%lf,%lf)\n", x[i][k], y[i][k]);
}
fscanf(g, "%d", &polys[i]);
//printf("there are %d polygons in this whatever\n", polys[i]);
for (k=0; k<polys[i]; k++) {
fscanf(g, "%d", &shapes[i][k]);
for (j=0; j<shapes[i][k]; j++) {
fscanf(g, "%d", &shapeorder[i][k][j]);
//printf("%d\n", shapeorder[i][k][j]);
}
}
for (k=0; k<polys[i]; k++) {
fscanf(g, "%lf %lf %lf", &red[i][k], &green[i][k], &blue[i][k]); //the rgb
//printf("(%lf, %lf, %lf)\n", red[i][k], green[i][k], blue[i][k]);
}
}
//draws the image
voiddrawit(inti) {
intk, j;
doubletempx[points[i]], tempy[points[i]];
for (k=0; k<polys[i]; k++) {
for (j=0; j<shapes[i][k]; j++) {
tempx[j] =x[i][shapeorder[i][k][j]];
tempy[j] =y[i][shapeorder[i][k][j]];
}
G_rgb(red[i][k], green[i][k], blue[i][k]);
G_fill_polygon(tempx, tempy, j);
}
}
//controls the movement of the object
voidmovement(inti, doublesf, doublem[3][3], doubleminv[3][3]) {
D2d_translate(m, minv, -centerx, -centery);
D2d_scale(m, minv, sf, sf);
D2d_translate(m, minv, WIDTH / 2, HEIGHT / 2);
D2d_mat_mult_points(x[i], y[i], m, x[i], y[i], points[i]);
}
//writes the opening instructions
voidwelcome(inti) {
printf("Press");
if (i==1) {
printf(" 1 to start,");
} elseif (i==2) {
printf(" 1 or 2 to start,");
} else {
printf(" 1 through %d to begin,", i);
}
printf("\nthen press TAB to begin clipping\n");
}
//the clipping container
voidclipping(double*clipx, double*clipy, inti) {
doublec[100];
inttemp_i=clickAndSave(clipx, clipy);
myPolygon(clipx, clipy, temp_i);
f_intersect(clipx, clipy, temp_i, x[i], y[i], points[i], c);
G_wait_key();
}
intmain(intargc, char**argv) {
charkey, c; FILE*g;
doublem[3][3], minv[3][3], sf, clipx[100], clipy[100];
intcc;
intinner_cycle=1;
for (cc=1; cc<argc; cc++) {
g=fopen(argv[cc], "r"); //opens a file; r = read only
if (g==NULL) { //if the file is empty, it will let me know
printf("can't open (1)\n");
exit(1);
} else {
readobject(g, cc);
D2d_make_identity(m); D2d_make_identity(minv);
sf=boundingbox(cc); //Remember, sf == scale factor
movement(cc, sf, m, minv); //(arg, scale, m, m inverse)
}
}
welcome(argc-1);
scanf("%c", &key);
inti=key-'0';
if (i<argc&&i>0) {
G_init_graphics(WIDTH, HEIGHT);
D2d_make_identity(m); D2d_make_identity(minv);
D2d_translate(m, minv, -WIDTH / 2, -HEIGHT / 2);
D2d_rotate(m, minv, M_PI / 45);
D2d_translate(m, minv, WIDTH / 2, HEIGHT / 2);
while (inner_cycle) {
G_rgb(1, 1, 1);
G_clear();
D2d_mat_mult_points(x[i], y[i], m, x[i], y[i], points[i]);
drawit(i);
G_rgb(0, 0, 0);
c=G_wait_key();
cc=c-'0';
if (cc>0&&cc<argc) {
if (cc!=i) {
i=cc;
}
} elseif (cc==-39) {
inner_cycle=0;
clipping(clipx, clipy, i);
} else {
printf("can't open (2)\n");
exit(1);
}
}
}
}