public SegmentsIntersectionResult Intersect(Segment other) { var result = new SegmentsIntersectionResult(); result.IsIntersection = false; var S = P2 - P1; var otherS = other.P2 - other.P1; //t = (other.P1 − P1) × otherS / (S × otherS) //u = (other.P1 − P1) × S / (S × otherS) var otherP1ToP1 = (other.P1 - P1); var den = S * otherS; var t = otherP1ToP1 * otherS; var u = otherP1ToP1 * S; //Actually it's more complex than this, if only one is 0 it means parallel, otherwise coincident if (t == 0 || u == 0 || den == 0) return result; t /= den; u /= den; if (t < 0 || t > 1 || u < 0 || u > 1) return result; result.IsIntersection = true; result.Point = P1 + S * t; var otherLeftNormal = otherS.Rotated90CCW.Normalized; var projOnNormal = (-otherP1ToP1).Dot(otherLeftNormal); if (projOnNormal > 0) result.Normal = otherLeftNormal; else result.Normal = -otherLeftNormal; return result; }
private static Boolean IntersectAnyPolygonEdgeExceptLast(Polygon polygon, DblPoint2 p1, DblPoint2 p2) { var s12 = new Segment(p1, p2); for (var i = 0; i < polygon.Points.Count - 1; i++) { var result = new Segment(polygon.Points[i], polygon.Points[i + 1]).Intersect(s12); if (result.IsIntersection) return true; } return false; }