/** * Returns true iff the line segments overlap; weaker than <code>theSame</code>. * * @param other * @return true iff this and other overlap */ public boolean isOverlapping(LineSegment other) { if (!VectorUtils.areEqual(m, other.m) && !VectorUtils.areEqual(b, other.b)) return false; // same line, now check for overlap--- // the start point of one should be inside the other return containsPoint(other.start) || other.containsPoint(start); }
/** * Return the point where this LineSegment intersects another line segment. * * @param other the other line segment. * @return the point where the two segments intersect, or null if they do not. */ public PointF findIntercept(LineSegment other) { // Special case for vertical lines if (Float.isInfinite(m) && Float.isInfinite(other.m)) { if (start.x == other.start.x) return new PointF(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY); return null; } // special case for horizontal lines if (m == 0 && other.m == 0) { if (start.y == other.start.y) return new PointF(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY); return null; } float x, y; if (Float.isInfinite(other.m)) { x = other.start.x; } else if (Float.isInfinite(m)) { return other.findIntercept(this); } else { // first find intercept. // solve for X // m1x + b1 = m2x + b2 // b1 - b2 = (m2 - m1)x x = (b - other.b) / (other.m - m); } // Now solve for y y = m * x + b; // now see if this point is on both line segments. if (isPointInSegment(x, y) && other.isPointInSegment(x, y)) { return new PointF(x, y); } return null; }