11 length
= v
->x
*v
->x
+v
->y
*v
->y
;
12 length
= sqrt(length
);
17 void PrintVect(Vect
*a
){
18 printf("[%lf, %lf]", a
->x
, a
->y
);
21 double DotProduct(Vect
*a
, Vect
*b
){
22 return a
->x
*b
->x
+a
->y
*b
->y
;
26 int InPoly(Poly
*p
, Vect
*v
){
29 for(i
=0; i
<p
->novec
; i
++){
30 if( DotProduct(v
, &p
->n
[i
]) >= p
->a
[i
]) return false;
40 p
=malloc(sizeof(Poly
));
49 int AddToPoly(Poly
*p
,Vect
*v
){
50 if( p
->novec
> (p
->alloc
-1)){
56 tn
= realloc(p
->n
, sizeof(Vect
)*p
->alloc
*2);
57 tv
= realloc(p
->v
, sizeof(Vect
)*p
->alloc
*2);
64 ta
= realloc(p
->a
, sizeof(double)*p
->alloc
*2);
72 p
->v
[p
->novec
].x
= v
->x
;
73 p
->v
[p
->novec
].y
= v
->y
;
76 //compute normal between actual and preceding
77 p
->n
[p
->novec
-1].x
= -p
->v
[p
->novec
-1].y
+ p
->v
[p
->novec
].y
;
78 p
->n
[p
->novec
-1].y
= +p
->v
[p
->novec
-1].x
- p
->v
[p
->novec
].x
;
79 NormV(&p
->n
[p
->novec
-1]);
80 p
->a
[p
->novec
-1] = p
->n
[p
->novec
-1].x
*v
->x
+ p
->n
[p
->novec
-1].y
*v
->y
;
82 //compute normal between actual and first
83 p
->n
[p
->novec
].x
= -p
->v
[p
->novec
].y
+ p
->v
[0].y
;
84 p
->n
[p
->novec
].y
= +p
->v
[p
->novec
].x
- p
->v
[0].x
;
85 NormV(&p
->n
[p
->novec
]);
86 p
->a
[p
->novec
] = p
->n
[p
->novec
].x
*v
->x
+ p
->n
[p
->novec
].y
*v
->y
;
93 int AddToPolyXY(Poly
*p
, double x
, double y
){
97 return AddToPoly(p
, &v
);
100 void FreePoly(Poly
*p
){
111 Poly
*NewRect(double x
, double y
, double xx
, double yy
){
115 AddToPolyXY(p
, x
, y
);
116 AddToPolyXY(p
, x
+xx
, y
);
117 AddToPolyXY(p
, x
+xx
, y
+yy
);
118 AddToPolyXY(p
, x
, y
+yy
);
123 int PolyInPoly(Poly
*p1
, Poly
*p2
){
127 //we check if any vertex of p1 is in p2
128 for(cur1
=0; cur1
< p1
->novec
; cur1
++){
129 if(InPoly(p2
, &p1
->v
[cur1
])){
134 //we check if any vertex of p1 is in p2
135 for(cur2
=0; cur2
< p2
->novec
; cur2
++){
136 if(InPoly(p1
, &p2
->v
[cur2
])){
144 int PolyInPolyComp(Poly
*p1
, Poly
*p2
){
149 //we check if every vertex of p1 is in p2
151 for(cur1
=0; cur1
< p1
->novec
; cur1
++){
152 if(!InPoly(p2
, &p1
->v
[cur1
])){
159 //we check if any vertex of p1 is in p2
161 for(cur2
=0; cur2
< p2
->novec
; cur2
++){
162 if(!InPoly(p1
, &p2
->v
[cur2
])){
172 Poly
*TranslatePoly(Poly
*p
, Vect
*v
){
173 return TranslatePolyXY(p
, v
->x
, v
->y
);
176 Poly
*TranslatePolyXY(Poly
*p
, double x
, double y
){
181 for(i
=0; i
< p
->novec
; i
++){
182 AddToPolyXY(r
, p
->v
[i
].x
+x
, p
->v
[i
].y
+y
);
187 void MultiplyPoly(Poly
*p
, double xm
, double ym
){
189 for(i
=0; i
< p
->novec
; i
++){
196 void PrintPoly(Poly
*p
){
199 printf("NoV: %d\n", p
->novec
);
200 for(i
=0; i
<p
->novec
; i
++)
201 printf("[%lf, %lf] [%lf, %lf] %lf \n", p
->v
[i
].x
, p
->v
[i
].y
, p
->n
[i
].x
, p
->n
[i
].y
, p
->a
[i
]);
204 /* format for saving poly into string:
205 * space delimited values in ascii:
206 * novec x y x y x y ...
208 #define P2SBUFSIZE 64
209 char *Poly2String(Poly
*p
){
215 buf
= malloc(P2SBUFSIZE
*sizeof(char));
218 snprintf(buf
, P2SBUFSIZE
, "%d ", p
->novec
);
219 allocated
= strlen(buf
)+2;
220 chain
= malloc(allocated
*sizeof(char));
223 for(i
=0; i
<p
->novec
; i
++){
224 snprintf(buf
, P2SBUFSIZE
, "%lf %lf ", p
->v
[i
].x
, p
->v
[i
].y
);
225 allocated
+= strlen(buf
);
226 chain
= realloc(chain
, allocated
*sizeof(char)); //TODO: check return value
227 sprintf(&chain
[strlen(chain
)],"%s", buf
);
234 void FreePoly2String(char *s
){
238 Poly
*String2Poly(char *s
){
239 //TODO: return values check, use strtok
246 sscanf(s
, "%d", &novec
);
249 for(i
=0; i
<novec
; i
++){
250 cur
= strchr(&cur
[1], ' ');
251 sscanf(cur
, "%lf", &x
);
252 cur
= strchr(&cur
[1], ' ');
253 sscanf(cur
, "%lf", &y
);
254 AddToPolyXY(p
, x
, y
);
264 a
= malloc(sizeof(Area
));
271 void FreeArea(Area
*a
){
277 for(i
=0; i
< a
->nopol
; i
++){
287 int AddToArea(Area
*a
, Poly
*p
){
288 //TODO: check for return values
291 a
->p
= realloc(a
->p
, sizeof(Poly
*)*a
->nopol
);
292 a
->p
[a
->nopol
-1] = TranslatePolyXY(p
, 0, 0);
297 int InArea(Area
*a
, Vect
*v
){
299 for(i
=0; i
<a
->nopol
; i
++){
300 if(InPoly(a
->p
[i
], v
))
306 int AreaInArea(Area
*a1
, Area
*a2
){
312 for(i
=0; i
<a1
->nopol
; i
++)
313 for(j
=0; j
<a2
->nopol
; j
++)
314 if(PolyInPoly(a1
->p
[i
], a2
->p
[j
]))
319 int AreaInAreaComp(Area
*a1
, Area
*a2
){
320 //TODO: rewrite to correct behavior this is only sufficient behavior to current use in ebulanci but not correct
328 for(i
=0; i
<a1
->nopol
; i
++)
329 for(j
=0; j
<a2
->nopol
; j
++){
330 if(!PolyInPolyComp(a1
->p
[i
], a2
->p
[j
]))
336 Area
*TranslateAreaXY(Area
*a
, double x
, double y
){
342 for(i
=0; i
< a
->nopol
; i
++){
343 p
= TranslatePolyXY(a
->p
[i
], x
, y
);
351 Area
*TranslateArea(Area
*a
, Vect
*v
){
352 return TranslateAreaXY(a
, v
->x
, v
->y
);
355 void PrintArea(Area
*a
){
359 char *Area2String(Area
*a
){
363 buf
= malloc(sizeof(char)* P2SBUFSIZE
);
364 snprintf(buf
, P2SBUFSIZE
, "%d\n", a
->nopol
);
366 al
= strlen(chain
)+1;
368 for(i
=0; i
<a
->nopol
; i
++){
369 buf
= Poly2String(a
->p
[i
]);
370 al
+= strlen(buf
)+2; //+2 for \n
371 chain
= realloc(chain
, al
);
372 sprintf(&chain
[strlen(chain
)], "%s\n", buf
);
373 FreePoly2String(buf
);
378 void FreeArea2String(char *s
){
382 Area
*String2Area(char *s
){
383 //TODO: return values check
390 sscanf(s
, "%d", &nopol
);
393 for(i
=0; i
<nopol
; i
++){
394 cur
= strstr(&cur
[strlen("\n")], "\n"); //BUG: if strlen("\n")>1 && strlen(nopol)==1
395 tp
= String2Poly(cur
);