summaryrefslogtreecommitdiffstats
path: root/geometry.c
diff options
context:
space:
mode:
Diffstat (limited to 'geometry.c')
-rw-r--r--geometry.c46
1 files changed, 35 insertions, 11 deletions
diff --git a/geometry.c b/geometry.c
index 248f354..2bbd6aa 100644
--- a/geometry.c
+++ b/geometry.c
@@ -4,13 +4,13 @@
#include <gtk/gtk.h>
-void addVertex(VERTEX_LIST *list, VERTEX *v) {
+void addVertex(VERTEX_LIST *list, const VERTEX *v) {
list->nVertices++;
list->vertices = realloc(list->vertices, list->nVertices*sizeof(VERTEX));
list->vertices[list->nVertices-1] = *v;
}
-void insertVertex(VERTEX_LIST *list, VERTEX *v, unsigned int n) {
+void insertVertex(VERTEX_LIST *list, const VERTEX *v, unsigned int n) {
int i;
if(n > list->nVertices)
@@ -44,6 +44,27 @@ double vertexDistance(const VERTEX *v1, const VERTEX *v2) {
return sqrt(vertexDistanceSquare(v1, v2));
}
+int vertexOnLine(const VERTEX *v, const LINE *l) {
+ if(l->v1.x == l->v2.x && l->v1.y == l->v2.y) {
+ if(l->v1.x == v->x && l->v1.y == v->y) return 1;
+ else return 0;
+ }
+
+ if(l->v1.x == l->v2.x) {
+ if(l->v1.x != v->x) return 0;
+ else if(v->y >= MIN(l->v1.y, l->v2.y) && v->y <= MAX(l->v1.y, l->v2.y)) return 1;
+ else return 1;
+ }
+
+ if(l->v1.y == l->v2.y) {
+ if(l->v1.y != v->y) return 0;
+ else if(v->x >= MIN(l->v1.x, l->v2.x) && v->x <= MAX(l->v1.x, l->v2.x)) return 1;
+ else return 1;
+ }
+ if((v->x-l->v1.x)/(l->v2.x-l->v1.x) - (v->y-l->v1.y)/(l->v2.y-l->v1.y) == 0) return 1;
+ else return 0;
+}
+
int vertexInRect(const VERTEX *v, const RECTANGLE *rect) {
int ret = EDGE_NONE;
@@ -149,7 +170,7 @@ double polygonArea(const POLYGON *p) {
for(i = 0; i < p->nVertices; i++)
d += (p->vertices[(i+1)%p->nVertices].x+p->vertices[i].x)*(p->vertices[(i+1)%p->nVertices].y-p->vertices[i].y);
- return d/2;
+ return fabs(d/2);
}
int lineIntersection(const LINE *la, const LINE *lb, VERTEX *v) {
@@ -176,22 +197,25 @@ int lineIntersection(const LINE *la, const LINE *lb, VERTEX *v) {
switched = 1;
}
- double ma = (ya2-ya1)/(xa2-xa1);
- double mb = (yb2-yb1)/(xb2-xb1);
- double ba = ya1 - ma*xa1;
- double bb = yb1 - mb*xb1;
-
- if(ma == mb) return (ba == bb) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE;
+ if(xa1 == xa2 && xb1 == xb2)
+ return (xa1 == xb1) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE;
- if(isinf(ma)) {
+ if(xa1 == xa2) {
v->x = xa1;
v->y = yb1;
}
- else if(isinf(mb)) {
+ else if(xb1 == xb2) {
v->x = xb1;
v->y = ya1;
}
else {
+ double ma = (ya2-ya1)/(xa2-xa1);
+ double mb = (yb2-yb1)/(xb2-xb1);
+ double ba = ya1 - ma*xa1;
+ double bb = yb1 - mb*xb1;
+
+ if(ma == mb) return (ba == bb) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE;
+
v->x = (bb-ba)/(ma-mb);
v->y = ma*v->x + ba;
}