public static Vector2D?IntersectLineLine(Vector2D s1, Vector2D e1, Vector2D s2, Vector2D e2) { var aabb1 = new Rect2D(s1, e1); var aabb2 = new Rect2D(s2, e2); if (!aabb1.IntersectsWith(aabb2)) { return(null); } double x1 = s1.X; double x2 = e1.X; double x3 = s2.X; double x4 = e2.X; double y1 = s1.Y; double y2 = e1.Y; double y3 = s2.Y; double y4 = e2.Y; double denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); if (Math.Abs(denom) < 1e-4) { return(null); } double px = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom; double py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom; var p = new Vector2D(px, py); double l1 = (p - s1).Length; double l2 = (p - e1).Length; if (l1 + l2 > (s1 - e1).Length + 1e-3) { return(null); } return(p); }
public bool Contains(Rect2D v) { return(v.Min.X >= Min.X && v.Min.Y >= Min.Y && v.Max.Y <= Max.Y && v.Max.X <= Max.X); }
public bool IntersectsWith(Rect2D r) { return(!(r.Max.X <= Min.X || r.Max.Y <= Min.Y || Max.Y <= r.Min.Y || Max.X <= r.Min.X)); }
private bool Equals(Rect2D other) => TopLeft.Equals(other.TopLeft) && BottomRight.Equals(other.BottomRight);