public static Collision FindBetween(Point from, Point to, ConnectionZone fromEl, ConnectionZone toEl,ref HashSet<Element> elementsAlreadyCollided, List<Element> elements = null) { if (elements == null) elements = Element.AllElements; double deltaMin = double.MaxValue; Collision minimum = null; foreach (Element element in elements) { if (element == fromEl.Parent) continue; if (element == toEl.Parent) continue; bool interects = Intersects(element.A, element.B, from, to); if(!interects) interects = Intersects(element.A, element.D, from, to); if (!interects) interects = Intersects(element.C, element.D, from, to); if (!interects) interects = Intersects(element.C, element.B, from, to); if (interects) { double deltaX = element.Center.X - fromEl.Parent.Center.X; double deltaY = element.Center.Y - fromEl.Parent.Center.Y; double delta = deltaX * deltaX + deltaY * deltaY; delta = Math.Sqrt(delta); Collision collision = new Collision(element, from, to); if (elementsAlreadyCollided.Contains(element)) continue; //find closest collision if (deltaMin > delta) { deltaMin = delta; minimum = collision; } } } if(minimum != null) elementsAlreadyCollided.Add(minimum.Element); return minimum; }
public static Collision FindBetween(Point from, Point to, ConnectionZone fromEl, ConnectionZone toEl,ref HashSet<Collision> lastCollision, List<Element> elements = null) { if (elements == null) elements = Element.AllElements; double deltaMin = double.MaxValue; Collision minimum = null; foreach (Element element in elements) { if (element == fromEl.Parent) continue; if (element == toEl.Parent) continue; Point intersection = new Point(); bool interects = Intersects(element.A, element.B, from, to, out intersection); if(!interects) interects = Intersects(element.A, element.D, from, to, out intersection); if (!interects) interects = Intersects(element.C, element.D, from, to, out intersection); if (!interects) interects = Intersects(element.C, element.B, from, to, out intersection); if (interects) //bool interects = Intersects(from, to, element); if (interects) { double deltaX = element.Center.X - fromEl.Parent.Center.X; double deltaY = element.Center.Y - fromEl.Parent.Center.Y; double delta = deltaX * deltaX + deltaY * deltaY; delta = Math.Sqrt(delta); Collision collision = new Collision(intersection, element, from, to); if (lastCollision.FirstOrDefault(x => x.Element == element) != null) continue; if (deltaMin > delta) { deltaMin = delta; minimum = collision; } } } if(minimum != null) lastCollision.Add(minimum); return minimum; }