From 5ea7f0464eb13581322215f0614eaae52393e02a Mon Sep 17 00:00:00 2001 From: neoraider Date: Sun, 16 Sep 2007 19:06:02 +0000 Subject: zoomedit: C++ized Polygon --- Line.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Line.cpp (limited to 'Line.cpp') diff --git a/Line.cpp b/Line.cpp new file mode 100644 index 0000000..c755050 --- /dev/null +++ b/Line.cpp @@ -0,0 +1,72 @@ +#include "Line.h" +#include + + +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; + + + if(!v) v = &v2; + + if(xa1 == xa2 && ya1 == ya2) return INTERSECTION_ERROR; + if(xb1 == xb2 && yb1 == yb2) return INTERSECTION_ERROR; + + if(xa1 == xa2 || xb1 == xb2) { + temp = xa1; xa1 = ya1; ya1 = temp; + temp = xa2; xa2 = ya2; ya2 = temp; + temp = xb1; xb1 = yb1; yb1 = temp; + temp = xb2; xb2 = yb2; yb2 = temp; + + switched = 1; + } + + if(xa1 == xa2 && xb1 == xb2) + return (xa1 == xb1) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE; + + if(xa1 == xa2) { + v->setX(xa1); + v->setY(yb1); + } + else if(xb1 == xb2) { + v->setX(xb1); + v->setY(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->setX((bb-ba)/(ma-mb)); + v->setY(ma*v->getX() + ba); + } + + if(switched) { + temp = v->getX(); v->setX(v->getY()); v->setY(temp); + + //switch back everything for segment tests + temp = xa1; xa1 = ya1; ya1 = temp; + temp = xa2; xa2 = ya2; ya2 = temp; + temp = xb1; xb1 = yb1; yb1 = temp; + temp = xb2; xb2 = yb2; yb2 = temp; + } + + if(v->getX() < fmin(xa1,xa2) || v->getX() > fmax(xa1, xa2) || v->getY() < fmin(ya1,ya2) || v->getY() > fmax(ya1, ya2)) { + if(v->getX() < fmin(xb1,xb2) || v->getX() > fmax(xb1, xb2) || v->getY() < fmin(yb1,yb2) || v->getY() > fmax(yb1, yb2)) + return INTERSECTION_LINE_LINE; + else + return INTERSECTION_LINE_SEGMENT; + } + else if(v->getX() < fmin(xb1,xb2) || v->getX() > fmax(xb1, xb2) || v->getY() < fmin(yb1,yb2) || v->getY() > fmax(yb1, yb2)) + return INTERSECTION_SEGMENT_LINE; + else + return INTERSECTION_SEGMENT_SEGMENT; +} -- cgit v1.2.3