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 PrintPoly(Poly
*p
){
190 printf("NoV: %d\n", p
->novec
);
191 for(i
=0; i
<p
->novec
; i
++)
192 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
]);
195 /* format for saving poly into string:
196 * space delimited values in ascii:
197 * novec x y x y x y ...
199 #define P2SBUFSIZE 64
200 char *Poly2String(Poly
*p
){
206 buf
= malloc(P2SBUFSIZE
*sizeof(char));
209 snprintf(buf
, P2SBUFSIZE
, "%d ", p
->novec
);
210 allocated
= strlen(buf
)+2;
211 chain
= malloc(allocated
*sizeof(char));
214 for(i
=0; i
<p
->novec
; i
++){
215 snprintf(buf
, P2SBUFSIZE
, "%lf %lf ", p
->v
[i
].x
, p
->v
[i
].y
);
216 allocated
+= strlen(buf
);
217 chain
= realloc(chain
, allocated
*sizeof(char)); //TODO: check return value
218 sprintf(&chain
[strlen(chain
)],"%s", buf
);
225 void FreePoly2String(char *s
){
229 Poly
*String2Poly(char *s
){
230 //TODO: return values check, use strtok
237 sscanf(s
, "%d", &novec
);
240 for(i
=0; i
<novec
; i
++){
241 cur
= strchr(&cur
[1], ' ');
242 sscanf(cur
, "%lf", &x
);
243 cur
= strchr(&cur
[1], ' ');
244 sscanf(cur
, "%lf", &y
);
245 AddToPolyXY(p
, x
, y
);
255 a
= malloc(sizeof(Area
));
262 void FreeArea(Area
*a
){
268 for(i
=0; i
< a
->nopol
; i
++){
277 int AddToArea(Area
*a
, Poly
*p
){
278 //TODO: check for return values
281 a
->p
= realloc(a
->p
, sizeof(Poly
*)*a
->nopol
);
282 a
->p
[a
->nopol
-1] = TranslatePolyXY(p
, 0, 0);
287 int InArea(Area
*a
, Vect
*v
){
289 for(i
=0; i
<a
->nopol
; i
++){
290 if(InPoly(a
->p
[i
], v
))
296 int AreaInArea(Area
*a1
, Area
*a2
){
302 for(i
=0; i
<a1
->nopol
; i
++)
303 for(j
=0; j
<a2
->nopol
; j
++)
304 if(PolyInPoly(a1
->p
[i
], a2
->p
[j
]))
309 int AreaInAreaComp(Area
*a1
, Area
*a2
){
310 //TODO: rewrite to correct behavior this is only sufficient behavior to current use in ebulanci but not correct
318 for(i
=0; i
<a1
->nopol
; i
++)
319 for(j
=0; j
<a2
->nopol
; j
++){
320 if(!PolyInPolyComp(a1
->p
[i
], a2
->p
[j
]))
326 Area
*TranslateAreaXY(Area
*a
, double x
, double y
){
332 for(i
=0; i
< a
->nopol
; i
++){
333 p
= TranslatePolyXY(a
->p
[i
], x
, y
);
341 Area
*TranslateArea(Area
*a
, Vect
*v
){
342 return TranslateAreaXY(a
, v
->x
, v
->y
);
345 void PrintArea(Area
*a
){
349 char *Area2String(Area
*a
){
353 buf
= malloc(sizeof(char)* P2SBUFSIZE
);
354 snprintf(buf
, P2SBUFSIZE
, "%d\n", a
->nopol
);
356 al
= strlen(chain
)+1;
358 for(i
=0; i
<a
->nopol
; i
++){
359 buf
= Poly2String(a
->p
[i
]);
360 al
+= strlen(buf
)+2; //+2 for \n
361 chain
= realloc(chain
, al
);
362 sprintf(&chain
[strlen(chain
)], "%s\n", buf
);
363 FreePoly2String(buf
);
368 void FreeArea2String(char *s
){
372 Area
*String2Area(char *s
){
373 //TODO: return values check
380 sscanf(s
, "%d", &nopol
);
383 for(i
=0; i
<nopol
; i
++){
384 cur
= strstr(&cur
[strlen("\n")], "\n"); //BUG: if strlen("\n")>1 && strlen(nopol)==1
385 tp
= String2Poly(cur
);