From 01e98d51fedf65ad71d468c3b0410d6e7764a384 Mon Sep 17 00:00:00 2001 From: neoraider Date: Sun, 16 Sep 2007 14:07:03 +0000 Subject: zoomedit: C++ized Vertex --- geometry.cpp | 149 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 69 insertions(+), 80 deletions(-) (limited to 'geometry.cpp') diff --git a/geometry.cpp b/geometry.cpp index ef5acfd..e709fb8 100644 --- a/geometry.cpp +++ b/geometry.cpp @@ -4,20 +4,20 @@ #include -void addVertex(VERTEX_LIST *list, const VERTEX *v) { +void addVertex(VERTEX_LIST *list, const Vertex *v) { list->nVertices++; - list->vertices = (VERTEX*)realloc(list->vertices, list->nVertices*sizeof(VERTEX)); + list->vertices = (Vertex*)realloc(list->vertices, list->nVertices*sizeof(Vertex)); list->vertices[list->nVertices-1] = *v; } -void insertVertex(VERTEX_LIST *list, const VERTEX *v, unsigned int n) { +void insertVertex(VERTEX_LIST *list, const Vertex *v, unsigned int n) { int i; if(n > list->nVertices) n = list->nVertices; list->nVertices++; - list->vertices = (VERTEX*)realloc(list->vertices, list->nVertices*sizeof(VERTEX)); + list->vertices = (Vertex*)realloc(list->vertices, list->nVertices*sizeof(Vertex)); for(i = list->nVertices-1; i > n; i--) list->vertices[i] = list->vertices[i-1]; @@ -33,72 +33,64 @@ void deleteVertex(VERTEX_LIST *list, unsigned int n) { for(i = n; i < list->nVertices; i++) list->vertices[i] = list->vertices[i+1]; - list->vertices = (VERTEX*)realloc(list->vertices, list->nVertices*sizeof(VERTEX)); + list->vertices = (Vertex*)realloc(list->vertices, list->nVertices*sizeof(Vertex)); } -double vertexDistanceSquare(const VERTEX *v1, const VERTEX *v2) { - return (v1->x-v2->x)*(v1->x-v2->x) + (v1->y-v2->y)*(v1->y-v2->y); -} - -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; +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.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; + 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.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; + 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->x-l->v1.x)/(l->v2.x-l->v1.x) - (v->y-l->v1.y)/(l->v2.y-l->v1.y) == 0) 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 vertexInRect(const Vertex *v, const RECTANGLE *rect) { int ret = EDGE_NONE; - if(v->x < rect->x) ret |= EDGE_LEFT; - else if(v->x >= rect->x+rect->width) ret |= EDGE_RIGHT; + if(v->getX() < rect->x) ret |= EDGE_LEFT; + else if(v->getX() >= rect->x+rect->width) ret |= EDGE_RIGHT; - if(v->y < rect->y) ret |= EDGE_TOP; - else if(v->y >= rect->y+rect->height) ret |= EDGE_BOTTOM; + if(v->getY() < rect->y) ret |= EDGE_TOP; + else if(v->getY() >= rect->y+rect->height) ret |= EDGE_BOTTOM; return ret; } -static int quadrant(VERTEX *v) { - if(v->x > 0 && v->y >= 0) return 1; - if(v->x >= 0 && v->y < 0) return 2; - if(v->x < 0 && v->y <= 0) return 3; - if(v->x <= 0 && v->y > 0) return 4; +static int quadrant(Vertex *v) { + if(v->getX() > 0 && v->getY() >= 0) return 1; + if(v->getX() >= 0 && v->getY() < 0) return 2; + if(v->getX() < 0 && v->getY() <= 0) return 3; + if(v->getX() <= 0 && v->getY() > 0) return 4; return 0; } -gboolean vertexInPolygon(const VERTEX *v, const POLYGON *p) { +gboolean vertexInPolygon(const Vertex *v, const POLYGON *p) { int d = 0, i, li; int q, ql, q2; - LINE d1 = {{-1, -1}, {1, 1}}; - LINE d2 = {{-1, 1}, {1, -1}}; + LINE d1 = {Vertex(-1, -1), Vertex(1, 1)}; + LINE d2 = {Vertex(-1, 1), Vertex(1, -1)}; LINE l; - VERTEX v2; + Vertex v2; if(p->nVertices == 0) return FALSE; - v2.x = p->vertices[p->nVertices-1].x - v->x; - v2.y = p->vertices[p->nVertices-1].y - v->y; + v2 = p->vertices[p->nVertices-1] - *v; q = quadrant(&v2); if(q == 0) return TRUE; @@ -106,9 +98,8 @@ gboolean vertexInPolygon(const VERTEX *v, const POLYGON *p) { for(i = 0; i < p->nVertices; i++) { ql = q; - v2.x = p->vertices[i].x - v->x; - v2.y = p->vertices[i].y - v->y; - q = quadrant(&v2); + + v2 = p->vertices[i] - *v; if(q == 0) return TRUE; @@ -124,9 +115,7 @@ gboolean vertexInPolygon(const VERTEX *v, const POLYGON *p) { d--; break; default: - l.v1.x = p->vertices[(i>0)?i-1:p->nVertices-1].x - v->x; - l.v1.y = p->vertices[(i>0)?i-1:p->nVertices-1].y - v->y; - + l.v1 = p->vertices[(i>0)?i-1:p->nVertices-1] - *v; l.v2 = v2; if(q == 1 || q == 3) { @@ -158,7 +147,7 @@ double polygonPerimeter(const POLYGON *p) { double d = 0.0; for(i = 0; i < p->nVertices; i++) - d += vertexDistance(&p->vertices[i], &p->vertices[(i+1)%p->nVertices]); + d += p->vertices[i].distance(p->vertices[(i+1)%p->nVertices]); return d; } @@ -168,19 +157,19 @@ double polygonArea(const POLYGON *p) { double d = 0.0; 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); + d += (p->vertices[(i+1)%p->nVertices].getX()+p->vertices[i].getX())*(p->vertices[(i+1)%p->nVertices].getY()-p->vertices[i].getY()); return fabs(d/2); } -int lineIntersection(const LINE *la, const LINE *lb, VERTEX *v) { - double xa1 = la->v1.x, ya1 = la->v1.y; - double xa2 = la->v2.x, ya2 = la->v2.y; - double xb1 = lb->v1.x, yb1 = lb->v1.y; - double xb2 = lb->v2.x, yb2 = lb->v2.y; +int lineIntersection(const LINE *la, const LINE *lb, Vertex *v) { + double xa1 = la->v1.getX(), ya1 = la->v1.getY(); + double xa2 = la->v2.getX(), ya2 = la->v2.getY(); + double xb1 = lb->v1.getX(), yb1 = lb->v1.getY(); + double xb2 = lb->v2.getX(), yb2 = lb->v2.getY(); double temp; int switched = 0; - VERTEX v2; + Vertex v2; if(v == NULL) v = &v2; @@ -201,12 +190,12 @@ int lineIntersection(const LINE *la, const LINE *lb, VERTEX *v) { return (xa1 == xb1) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE; if(xa1 == xa2) { - v->x = xa1; - v->y = yb1; + v->setX(xa1); + v->setY(yb1); } else if(xb1 == xb2) { - v->x = xb1; - v->y = ya1; + v->setX(xb1); + v->setY(ya1); } else { double ma = (ya2-ya1)/(xa2-xa1); @@ -216,12 +205,12 @@ int lineIntersection(const LINE *la, const LINE *lb, VERTEX *v) { if(ma == mb) return (ba == bb) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE; - v->x = (bb-ba)/(ma-mb); - v->y = ma*v->x + ba; + v->setX((bb-ba)/(ma-mb)); + v->setY(ma*v->getX() + ba); } if(switched) { - temp = v->x; v->x = v->y; v->y = temp; + temp = v->getX(); v->setX(v->getY()); v->setY(temp); //switch back everything for segment tests temp = xa1; xa1 = ya1; ya1 = temp; @@ -230,25 +219,25 @@ int lineIntersection(const LINE *la, const LINE *lb, VERTEX *v) { temp = xb2; xb2 = yb2; yb2 = temp; } - if(v->x < MIN(xa1,xa2) || v->x > MAX(xa1, xa2) || v->y < MIN(ya1,ya2) || v->y > MAX(ya1, ya2)) { - if(v->x < MIN(xb1,xb2) || v->x > MAX(xb1, xb2) || v->y < MIN(yb1,yb2) || v->y > MAX(yb1, yb2)) + if(v->getX() < MIN(xa1,xa2) || v->getX() > MAX(xa1, xa2) || v->getY() < MIN(ya1,ya2) || v->getY() > MAX(ya1, ya2)) { + if(v->getX() < MIN(xb1,xb2) || v->getX() > MAX(xb1, xb2) || v->getY() < MIN(yb1,yb2) || v->getY() > MAX(yb1, yb2)) return INTERSECTION_LINE_LINE; else return INTERSECTION_LINE_SEGMENT; } - else if(v->x < MIN(xb1,xb2) || v->x > MAX(xb1, xb2) || v->y < MIN(yb1,yb2) || v->y > MAX(yb1, yb2)) + else if(v->getX() < MIN(xb1,xb2) || v->getX() > MAX(xb1, xb2) || v->getY() < MIN(yb1,yb2) || v->getY() > MAX(yb1, yb2)) return INTERSECTION_SEGMENT_LINE; else return INTERSECTION_SEGMENT_SEGMENT; } -int lineRectIntersection(const LINE *l, const RECTANGLE *rect, int edge, VERTEX *v) { +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 = {{minX, minY}, {maxX, minY}}; - const LINE bottom = {{minX, maxY}, {maxX, maxY}}; - const LINE left = {{minX, minY}, {minX, maxY}}; - const LINE right = {{maxX, minY}, {maxX, maxY}}; + 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; @@ -259,12 +248,12 @@ int lineRectIntersection(const LINE *l, const RECTANGLE *rect, int edge, VERTEX if((edge & EDGE_RIGHT) && (lineIntersection(&right, l, v) == INTERSECTION_SEGMENT_SEGMENT)) return EDGE_RIGHT; - v->x = v->y = 0; + v->setLocation(0, 0); return EDGE_NONE; } -int lineRectIntersections(const LINE *line, const RECTANGLE *rect, int edge, VERTEX *v1, VERTEX *v2) { +int lineRectIntersections(const LINE *line, const RECTANGLE *rect, int edge, Vertex *v1, Vertex *v2) { int ret = EDGE_NONE; ret |= lineRectIntersection(line, rect, edge, v1); @@ -289,18 +278,18 @@ gboolean linePolygonIntersection(const LINE *l, const POLYGON *p) { } -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); - if(edge & EDGE_LEFT) v->x = rect->x; - else if(edge & EDGE_RIGHT) v->x = rect->x+rect->width; + if(edge & EDGE_LEFT) v->setX(rect->x); + else if(edge & EDGE_RIGHT) v->setX(rect->x+rect->width); - if(edge & EDGE_TOP) v->y = rect->y; - else if(edge & EDGE_BOTTOM) v->y = rect->y+rect->height; + if(edge & EDGE_TOP) v->setY(rect->y); + else if(edge & EDGE_BOTTOM) v->setY(rect->y+rect->height); } -static int simplifyVertex(VERTEX *v, const VERTEX *to, const RECTANGLE *rect) { +static int simplifyVertex(Vertex *v, const Vertex *to, const RECTANGLE *rect) { LINE l = {*v, *to}; int edge, edge2; @@ -317,12 +306,12 @@ static int simplifyVertex(VERTEX *v, const VERTEX *to, const RECTANGLE *rect) { -static void addSimplifiedLine(const VERTEX *v1, const VERTEX *v2, const RECTANGLE *rect, int last, POLYGON *out) { - const LINE d1 = {{rect->x, rect->y}, {rect->x+rect->width, rect->y+rect->height}}; - const LINE d2 = {{rect->x, rect->y+rect->height}, {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 = {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)}; LINE l = {*v1, *v2}; - VERTEX v, vi; + Vertex v, vi; int edge1, edge2; @@ -349,7 +338,7 @@ static void addSimplifiedLine(const VERTEX *v1, const VERTEX *v2, const RECTANGL } void simplifyPolygon(const POLYGON *in, const RECTANGLE *rect, POLYGON *out) { - VERTEX v; + Vertex v; int i; if(in->nVertices == 0) return; -- cgit v1.2.3