diff options
author | neoraider <devnull@localhost> | 2007-09-16 21:06:02 +0200 |
---|---|---|
committer | neoraider <devnull@localhost> | 2007-09-16 21:06:02 +0200 |
commit | 5ea7f0464eb13581322215f0614eaae52393e02a (patch) | |
tree | 073163463ab20fd00016a5fbcd56c6dcf9857182 /Line.cpp | |
parent | 01e98d51fedf65ad71d468c3b0410d6e7764a384 (diff) | |
download | zoomedit-5ea7f0464eb13581322215f0614eaae52393e02a.tar zoomedit-5ea7f0464eb13581322215f0614eaae52393e02a.zip |
zoomedit: C++ized Polygon
Diffstat (limited to 'Line.cpp')
-rw-r--r-- | Line.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
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 <math.h> + + +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; +} |