diff options
Diffstat (limited to 'geometry.cpp')
-rw-r--r-- | geometry.cpp | 105 |
1 files changed, 17 insertions, 88 deletions
diff --git a/geometry.cpp b/geometry.cpp index e3adb6b..7f6bb6c 100644 --- a/geometry.cpp +++ b/geometry.cpp @@ -1,108 +1,37 @@ #include "geometry.h" -#include <math.h> -#include <stdlib.h> -#include <gtk/gtk.h> -int vertexOnLine(const Vertex *v, const LINE *l) { - if(l->v1.getX() == l->v2.getX() && l->v1.getY() == l->v2.getY()) { - if(l->v1.getX() == v->getX() && l->v1.getY() == v->getY()) return 1; - else return 0; - } - - if(l->v1.getX() == l->v2.getX()) { - if(l->v1.getX() != v->getX()) return 0; - else if(v->getY() >= MIN(l->v1.getY(), l->v2.getY()) && v->getY() <= MAX(l->v1.getY(), l->v2.getY())) return 1; - else return 1; - } - - if(l->v1.getY() == l->v2.getY()) { - if(l->v1.getY() != v->getY()) return 0; - else if(v->getX() >= MIN(l->v1.getX(), l->v2.getX()) && v->getX() <= MAX(l->v1.getX(), l->v2.getX())) return 1; - else return 1; - } - if((v->getX()-l->v1.getX())/(l->v2.getX()-l->v1.getX()) - (v->getY()-l->v1.getY())/(l->v2.getY()-l->v1.getY()) == 0) return 1; - else return 0; -} - -int vertexInRect(const Vertex *v, const RECTANGLE *rect) { - int ret = EDGE_NONE; - - if(v->getX() < rect->x) ret |= EDGE_LEFT; - else if(v->getX() >= rect->x+rect->width) ret |= EDGE_RIGHT; - - if(v->getY() < rect->y) ret |= EDGE_TOP; - else if(v->getY() >= rect->y+rect->height) ret |= EDGE_BOTTOM; - - return ret; -} - - -int lineRectIntersection(const LINE *l, const RECTANGLE *rect, int edge, Vertex *v) { - const double minX = rect->x, maxX = rect->x+rect->width; - const double minY = rect->y, maxY = rect->y+rect->height; - const LINE top = {Vertex(minX, minY), Vertex(maxX, minY)}; - const LINE bottom = {Vertex(minX, maxY), Vertex(maxX, maxY)}; - const LINE left = {Vertex(minX, minY), Vertex(minX, maxY)}; - const LINE right = {Vertex(maxX, minY), Vertex(maxX, maxY)}; - - if((edge & EDGE_TOP) && (lineIntersection(&top, l, v) == INTERSECTION_SEGMENT_SEGMENT)) - return EDGE_TOP; - if((edge & EDGE_BOTTOM) && (lineIntersection(&bottom, l, v) == INTERSECTION_SEGMENT_SEGMENT)) - return EDGE_BOTTOM; - if((edge & EDGE_LEFT) && (lineIntersection(&left, l, v) == INTERSECTION_SEGMENT_SEGMENT)) - return EDGE_LEFT; - if((edge & EDGE_RIGHT) && (lineIntersection(&right, l, v) == INTERSECTION_SEGMENT_SEGMENT)) - return EDGE_RIGHT; - - v->setLocation(0, 0); - - return EDGE_NONE; -} - -int lineRectIntersections(const LINE *line, const RECTANGLE *rect, int edge, Vertex *v1, Vertex *v2) { - int ret = EDGE_NONE; - - ret |= lineRectIntersection(line, rect, edge, v1); - ret |= lineRectIntersection(line, rect, EDGE_ALL^edge, v2); - - return ret; -} - - -static void edgeVertex(Vertex *v, int edge, const RECTANGLE *rect) { +static void edgeVertex(Vertex *v, int edge, const Rectangle *rect) { if(edge == EDGE_NONE) - edge = vertexInRect(v, rect); + edge = rect->edges(*v); - if(edge & EDGE_LEFT) v->setX(rect->x); - else if(edge & EDGE_RIGHT) v->setX(rect->x+rect->width); + if(edge & EDGE_LEFT) v->setX(rect->getVertex1().getX()); + else if(edge & EDGE_RIGHT) v->setX(rect->getVertex2().getX()); - if(edge & EDGE_TOP) v->setY(rect->y); - else if(edge & EDGE_BOTTOM) v->setY(rect->y+rect->height); + if(edge & EDGE_TOP) v->setY(rect->getVertex1().getY()); + else if(edge & EDGE_BOTTOM) v->setY(rect->getVertex2().getY()); } -static int simplifyVertex(Vertex *v, const Vertex *to, const RECTANGLE *rect) { - LINE l = {*v, *to}; +static int simplifyVertex(Vertex *v, const Vertex *to, const Rectangle *rect) { + Line l(*v, *to); int edge, edge2; - edge = vertexInRect(v, rect); + edge = rect->edges(*v); if(edge == EDGE_NONE) return EDGE_NONE; - edge2 = lineRectIntersection(&l, rect, edge, v); + edge2 = rect->intersects(l, v, edge); if(edge2 != EDGE_NONE) return edge2; edgeVertex(v, edge, rect); return edge; } - - -static void addSimplifiedLine(const Vertex *v1, const Vertex *v2, const RECTANGLE *rect, int last, Polygon *out) { - const LINE d1 = {Vertex(rect->x, rect->y), Vertex(rect->x+rect->width, rect->y+rect->height)}; - const LINE d2 = {Vertex(rect->x, rect->y+rect->height), Vertex(rect->x+rect->width, rect->y)}; +static void addSimplifiedLine(const Vertex *v1, const Vertex *v2, const Rectangle *rect, int last, Polygon *out) { + const Line d1(rect->getVertex1(), rect->getVertex2()); + const Line d2(rect->getVertex1().getX(), rect->getVertex2().getY(), rect->getVertex2().getX(), rect->getVertex1().getY()); - LINE l = {*v1, *v2}; + Line l(*v1, *v2); Vertex v, vi; int edge1, edge2; @@ -115,11 +44,11 @@ static void addSimplifiedLine(const Vertex *v1, const Vertex *v2, const RECTANGL edge2 = simplifyVertex(&v, v1, rect); if(edge1 != EDGE_NONE && edge2 != EDGE_NONE && !(edge1 & edge2)) { - if(lineIntersection(&l, &d1, &vi) == INTERSECTION_SEGMENT_LINE) { + if(l.intersects(d1, &vi) == INTERSECTION_SEGMENT_LINE) { edgeVertex(&vi, 0, rect); out->push_back(vi); } - else if(lineIntersection(&l, &d2, &vi) == INTERSECTION_SEGMENT_LINE) { + else if(l.intersects(d1, &vi) == INTERSECTION_SEGMENT_LINE) { edgeVertex(&vi, 0, rect); out->push_back(vi); } @@ -129,7 +58,7 @@ static void addSimplifiedLine(const Vertex *v1, const Vertex *v2, const RECTANGL out->push_back(v); } -void simplifyPolygon(const Polygon *in, const RECTANGLE *rect, Polygon *out) { +void simplifyPolygon(const Polygon *in, const Rectangle *rect, Polygon *out) { Vertex v; if(in->empty()) return; |