/// <summary> /// Is this segment interesects the given segment? /// </summary> /// <param name="s">The segment</param> /// <returns>Returns true if this segment intersects the given segment.</returns> public bool Intersects(Segment2d s) { var aa = s.a - a; var det = Vector2d.Cross(ab, s.ab); var t0 = Vector2d.Cross(aa, s.ab) / det; if (t0 > 0.0 && t0 < 1.0) { var t1 = Vector2d.Cross(aa, ab) / det; return(t1 > 0 && t1 < 1); } return(false); }
/// <summary> /// Is this segment is inside or intersects the given bounding box? /// </summary> /// <param name="r">The bounding box.</param> /// <returns>Returns true if this segment is inside or intersects the given bounding box.</returns> public bool Intersects(Box2d r) { var b = a + ab; if (r.Contains(a) || r.Contains(b)) { return(true); } var t = new Box2d(a, b); if (t.xmin > r.xmax || t.xmax < r.xmin || t.ymin > r.ymax || t.ymax < r.ymin) { return(false); } var p0 = Vector2d.Cross(ab, new Vector2d(r.xmin, r.ymin) - a); var p1 = Vector2d.Cross(ab, new Vector2d(r.xmax, r.ymin) - a); if (p1 * p0 <= 0.0) { return(true); } var p2 = Vector2d.Cross(ab, new Vector2d(r.xmin, r.ymax) - a); if (p2 * p0 <= 0.0) { return(true); } var p3 = Vector2d.Cross(ab, new Vector2d(r.xmax, r.ymax) - a); if (p3 * p0 <= 0.0) { return(true); } return(false); }