#include "Line.h" #include bool Line::contains(const Vertex &v) const { if(fabs(v1.getX() - v2.getX()) < 1E-6 && fabs(v1.getY() - v2.getY()) < 1E-6) { if(fabs(v1.getX() - v.getX()) < 1E-6 && fabs(v1.getY() - v.getY()) < 1E-6) return true; else return false; } if(fabs(v1.getX() - v2.getX()) < 1E-6) { if(fabs(v1.getX() - v.getX()) >= 1E-6) return false; else if(v.getY() - fmin(v1.getY(), v2.getY()) > -1E-6 && v.getY() - fmax(v1.getY(), v2.getY()) < 1E-6) return true; else return false; } if(fabs(v1.getY() - v2.getY()) < 1E-6) { if(fabs(v1.getY() - v.getY()) >= 1E-6) return false; else if(v.getX() - fmin(v1.getX(), v2.getX()) > -1E-6 && v.getX() - fmax(v1.getX(), v2.getX()) < 1E-6) return true; else return false; } if(fabs((v.getX()-v1.getX())/(v2.getX()-v1.getX()) - (v.getY()-v1.getY())/(v2.getY()-v1.getY())) < 1E-6) return true; else return false; } int Line::intersects(const Line &l, Vertex *v) const { float xa1 = v1.getX(), ya1 = v1.getY(); float xa2 = v2.getX(), ya2 = v2.getY(); float xb1 = l.v1.getX(), yb1 = l.v1.getY(); float xb2 = l.v2.getX(), yb2 = l.v2.getY(); float temp; int switched = 0; Vertex v2; if(!v) v = &v2; if(fabs(xa1 - xa2) < 1E-6 && fabs(ya1 - ya2) < 1E-6) return INTERSECTION_ERROR; if(fabs(xb1 - xb2) < 1E-6 && fabs(yb1 - yb2) < 1E-6) return INTERSECTION_ERROR; if(fabs(xa1 - xa2) < 1E-6 || fabs(xb1 - xb2) < 1E-6) { 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(fabs(xa1 - xa2) < 1E-6 && fabs(xb1 - xb2) < 1E-6) return (fabs(xa1 - xb1) < 1E-6) ? INTERSECTION_IDENTICAL : INTERSECTION_NONE; if(fabs(xa1 - xa2) < 1E-6) { v->setX(xa1); v->setY(yb1); } else if(fabs(xb1 - xb2) < 1E-6) { v->setX(xb1); v->setY(ya1); } else { float ma = (ya2-ya1)/(xa2-xa1); float mb = (yb2-yb1)/(xb2-xb1); float ba = ya1 - ma*xa1; float bb = yb1 - mb*xb1; if(fabs(ma - mb) < 1E-6) return (fabs(ba - bb) < 1E-6) ? 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; }