-
Notifications
You must be signed in to change notification settings - Fork 1
/
polygon.c
275 lines (229 loc) · 6.43 KB
/
polygon.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
#include "objects.h"
#include "operations.h"
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
POINT getXmin(POINT *points, int sizePoints){
int i;
double xmin = 900000;
POINT p;
for(int i=0; i<sizePoints; i++){
if(points[i].x < xmin){
xmin = points[i].x;
p = points[i];
}
}
return p;
}
POINT getXmax(POINT* points, int sizePoints){
int i;
double xmax = -900000;
POINT p;
for(int i=0; i<sizePoints; i++){
if(points[i].x > xmax){
xmax = points[i].x;
p = points[i];
}
}
return p;
}
POINT getYmin(POINT* points, int sizePoints){
int i;
double ymin = 900000;
POINT p;
for(int i=0; i<sizePoints; i++){
if(points[i].y < ymin){
ymin = points[i].y;
p = points[i];
}
}
return p;
}
POINT getYmax(POINT* points, int sizePoints){
int i;
double ymax = -900000;
POINT p;
for(int i=0; i<sizePoints; i++){
if(points[i].y > ymax){
ymax = points[i].y;
p = points[i];
}
}
return p;
}
// Obtiene el valor de D
double getD(VECTOR normal, POINT punto){
// D = -Ax -Bx -Cx
return (-normal.x * punto.x) - (normal.y * punto.y) - (normal.z * punto.z);
}
// Calcula la ecuación del polígono
PEQUATION getABCD(POINT *points){
VECTOR v1 = getVector(points[0], points[2]);
VECTOR v2 = getVector(points[1], points[2]);
VECTOR cruxP = cruxProduct(v1, v2); // Obtiene la normal [A, B, C]
VECTOR ncruxP = normalizeVector(cruxP); // Normaliza la normal
PEQUATION eq;
eq.a = ncruxP.x;
eq.b = ncruxP.y;
eq.c = ncruxP.z;
eq.d = getD(cruxP, points[2])/getMagnitude(cruxP);
return eq;
}
// Aplasta el polígono a 2D
void flattenPolygon(POLYGON p){
int i;
char m = p.tag;
for(i = 0; i < p.sizePoints; i++){
if(m == 'a'){
p.points2D[i].u = p.points[i].y;
p.points2D[i].v = p.points[i].z;
}
else if(m == 'b'){
p.points2D[i].u = p.points[i].x;
p.points2D[i].v = p.points[i].z;
}
else if(m == 'c'){
p.points2D[i].u = p.points[i].x;
p.points2D[i].v = p.points[i].y;
}
}
}
// Crea un polígono y lo agrega a un objeto
OBJECT createPolygon(POINT *vertices, int numVertices, COLOR color, long double kd, long double ka, long double ks, long double kn, long double o1, long double o2){
POLYGON p;
p.points = vertices;
p.sizePoints = numVertices;
p.equation = getABCD(p.points); // GUARDA LA ECUACIÓN DEL POLIGONO
p.tag = max(fabs(p.equation.a), fabs(p.equation.b), fabs(p.equation.c));
p.points2D = (POINT*)malloc(p.sizePoints*sizeof(POINT)); // APLASTA EL POLÍGONO
flattenPolygon(p);
POINT xmin, ymin, xmax, ymax, p1, p2, p3, p4;
xmin = getXmin(vertices, numVertices);
ymin = getYmin(vertices, numVertices);
xmax = getXmax(vertices, numVertices);
ymax = getYmax(vertices, numVertices);
p1 = getZ(p.equation, xmin.x, ymin.y);
p2 = getZ(p.equation, xmin.x, ymax.y);
p3 = getZ(p.equation, xmax.x, ymax.y);
p4 = getZ(p.equation, xmax.x, ymin.y);
POINT rectangle[4];
rectangle[0] = p1;
rectangle[1] = p2;
rectangle[2] = p3;
rectangle[3] = p4;
p.rectangle = rectangle;
OBJECT newObject;
newObject.id = 'P';
newObject.polygon = p;
newObject.color = color;
newObject.ka = ka;
newObject.kd = kd;
newObject.ks = ks;
newObject.kn = kn;
newObject.o1 = o1;
newObject.o2 = o2;
return newObject;
}
//------------------------------------------- CALCULAR INTERSECCIONES ------------------------------------------
POINT2D flattenPoint(POINT intersectionPoint, char tag){
POINT2D newIntersectionPoint;
if(tag == 'a'){
newIntersectionPoint.u = intersectionPoint.y;
newIntersectionPoint.v = intersectionPoint.z;
}
else if(tag == 'b'){
newIntersectionPoint.u = intersectionPoint.x;
newIntersectionPoint.v = intersectionPoint.z;
}
else if(tag == 'c'){
newIntersectionPoint.u = intersectionPoint.x;
newIntersectionPoint.v = intersectionPoint.y;
}
return newIntersectionPoint;
}
// Traslada los puntos al origen
void translatePoints(POINT2D* points, int sizePoints, POINT2D intersectionPoint){
int i;
for(i = 0; i<sizePoints; i++){
points[i].u = points[i].u - intersectionPoint.u;
points[i].v = points[i].v - intersectionPoint.v;
}
}
int countEdges(POINT2D *points2D, int sizePoints){
int numEdges = 0;
int i;
POINT2D p1, p2;
for(i = 0; i < sizePoints; i++){
if(i == sizePoints-1){
p1 = points2D[i];
p2 = points2D[0];
}
else{
p1 = points2D[i];
p2 = points2D[i+1];
}
int u_positive = p1.u >= 0 && p2.u >= 0;
int u_different = (p1.u >= 0 && p2.u < 0) || (p1.u < 0 && p2.u >= 0);
int v_different = (p1.v >= 0 && p2.v < 0) || (p1.v < 0 && p2.v >= 0);
if(u_positive && v_different){
numEdges++;
}
else if(u_different && v_different){
long double m = (p2.v - p1.v) / (p2.u - p1.u);
long double b = p2.v - m * p2.u;
long double x = -b/m;
if(x >= 0){
numEdges++;
}
}
}
return (numEdges % 2);
}
bool verifyPoint(POINT2D *points2D, int sizePoints, POINT2D intersectionPoint){
int i;
POINT2D * newPoints2D = (POINT2D*)malloc(sizePoints*sizeof(POINT2D));
for(i = 0; i < sizePoints; i++){
newPoints2D[i].u = points2D[i].u;
newPoints2D[i].v = points2D[i].v;
}
translatePoints(newPoints2D, sizePoints, intersectionPoint);
if(countEdges(newPoints2D, sizePoints) == 1){ // Si la cantidad es impar
return true;
}
return false;
}
PEQUATION reverse(POLYGON p){
PEQUATION newEq;
newEq.a = p.equation.a * -1;
newEq.b = p.equation.b * -1;
newEq.c = p.equation.c * -1;
newEq.d = p.equation.d * -1;
return newEq;
}
INTERSECTION findIntersection_polygon(VECTOR direction, POINT eye, POLYGON p){
VECTOR norm = eq2vector(p.equation); // Construye la normal a partir de la ecuación del polígono
INTERSECTION intersection;
// Primera fase: Revisa si hay intersección con el plano
if(pointProduct(norm, direction) > EPSILON){ // Hay interseccion
// Calcula t
double t = -(norm.x*eye.x + norm.y*eye.y + norm.z*eye.z + p.equation.d)/(norm.x*direction.x + norm.y*direction.y + norm.z*direction.z);
POINT intersectionPoint = getIntersectionPoint(pointToVector(eye), direction, t);
// Segunda fase: Revisa si el punto está en el interior del polígono
if(verifyPoint(p.points2D, p.sizePoints, flattenPoint(intersectionPoint, p.tag))){
intersection.tmin = t;
intersection.tmax = 0;
intersection.flag = 1;
}
else{
intersection.tmin = 0;
intersection.tmax = 0;
intersection.flag = 0;
}
}
else{
intersection.tmin = 0;
intersection.tmax = 0;
intersection.flag = 0;
}
return intersection;
}