private void DetectIntersectSegments(List <Vector2d> footprint, Vector2d start, Vector2d end, out Vector2d first, out int firstIndex, out Vector2d second, out int secondIndex) { firstIndex = -1; secondIndex = -1; first = default(Vector2d); second = default(Vector2d); for (int i = 0; i < footprint.Count; i++) { var p1 = footprint[i]; var p2 = footprint[i == footprint.Count - 1 ? 0 : i + 1]; double r; if (Vector2dUtils.LineIntersects(start, end, p1, p2, out r)) { var intersectionPoint = Vector2dUtils.GetPointAlongLine(p1, p2, r); if (firstIndex == -1) { firstIndex = i; first = intersectionPoint; } else { secondIndex = i; second = intersectionPoint; break; } } } }
/// <summary> Tries to find intersection with transit walls. </summary> private static bool HasIntersectPoint(List <Wall> extrudedWalls, List <Wall> connectedSkeletonWalls, Vector2d p1, Vector2d p2, out Vector2d intersectPoint, out int transitWallIndex) { intersectPoint = new Vector2d(); transitWallIndex = -1; var distance = double.MaxValue; for (var i = 0; i < extrudedWalls.Count; i++) { var wall = extrudedWalls[i]; if (wall.IsOuter) { continue; } var start = wall.Start; var end = wall.End; double r; if (!Vector2dUtils.LineIntersects(p1, p2, start, end, out r)) { continue; } var currIntersectPoint = new Vector2d( Math.Round(p1.X + (r * (p2.X - p1.X))), Math.Round((p1.Y + (r * (p2.Y - p1.Y))))); // skip intersections of skeleton edges which are connected with footprint var skip = false; foreach (var connectedWall in connectedSkeletonWalls) { if (Vector2dUtils.LineIntersects(p1, currIntersectPoint, connectedWall.Start, connectedWall.End, out r)) { skip = true; break; } } if (skip) { continue; } if (Vector2dUtils.IsPointOnSegment(start, end, currIntersectPoint)) { var currDistance = p1.DistanceTo(currIntersectPoint); if (currDistance > 0 && currDistance < distance) { intersectPoint = currIntersectPoint; transitWallIndex = i; distance = currDistance; } } } return(transitWallIndex != -1); }
public void CanDetectIntersection() { // ARRANGE var s1 = new Vector2d(0, 0); var e1 = new Vector2d(10, 10); var s2 = new Vector2d(10, 0); var e2 = new Vector2d(0, 10); double r; // ACT var result = Vector2dUtils.LineIntersects(s1, e1, s2, e2, out r); var currIntersectPoint = Vector2dUtils.GetPointAlongLine(s1, s2, r); // ASSERT Assert.IsTrue(result); Assert.IsTrue(Math.Abs(currIntersectPoint.X - 5) < MathUtils.Epsion); Assert.IsTrue(Math.Abs(currIntersectPoint.X - 5) < MathUtils.Epsion); }