public static bool IsSimplifiedRectanglesInterescting(SimplifiedRectangle rect1, SimplifiedRectangle rect2) { var minx1 = Math.Min(rect1.V1.X, rect1.V2.X); var maxx1 = Math.Max(rect1.V1.X, rect1.V2.X); var miny1 = Math.Min(rect1.V1.Y, rect1.V2.Y); var maxy1 = Math.Max(rect1.V1.Y, rect1.V2.Y); var minx2 = Math.Min(rect2.V1.X, rect2.V2.X); var maxx2 = Math.Max(rect2.V1.X, rect2.V2.X); var miny2 = Math.Min(rect2.V1.Y, rect2.V2.Y); var maxy2 = Math.Max(rect2.V1.Y, rect2.V2.Y); return((minx1 >= minx2 && minx1 <= maxx2 || maxx1 >= minx2 && maxx1 <= maxx2 || minx2 >= minx1 && maxx2 <= maxx1) && (miny1 >= miny2 && miny1 <= maxy2 || maxy1 >= miny2 && maxy1 <= maxy2 || miny2 >= miny1 && maxy2 <= maxy1)); }
public static IntersectionResult GetSegmentSimplifiedRectangleIntersection(Segment seg, SimplifiedRectangle rect, out object result, bool filledRectangle = false) { var points = new List <Vector2>(); foreach (var s in rect.Segments) { var intersection = GetSegmentsIntersection(seg, s, out var intres); switch (intersection) { case IntersectionResult.Segment: result = intres; return(IntersectionResult.Segment); case IntersectionResult.Point: points.Add((Vector2)intres); break; case IntersectionResult.None: continue; } } points = points.Distinct().ToList(); // ох ужас // если точек пересечения нет, то вернем None // если точка пересечения одна, то она либо вершина прямоугольника и тогда мы вернем ее, // либо точка пересечения является вершиной отрезка, и тогда мы вернем ее, если not filledRectangle, // иначе вернем отрезок, // если точек пересечения две, то вернем либо отрезок, либо 2 точки в зависимости от filledRectangle var beginInRect = IsPointInsideSimplifiedRectangle(seg.Begin, rect); var endInRect = IsPointInsideSimplifiedRectangle(seg.End, rect); if (points.Count == 0) { if (beginInRect && endInRect && filledRectangle) { result = seg; return(IntersectionResult.Segment); } else { result = null; return(IntersectionResult.None); } } if (points.Count == 1) { var point = points[0]; if (filledRectangle) { if (!beginInRect && !endInRect) { result = point; return(IntersectionResult.Point); } if (beginInRect && endInRect) { result = seg; return(IntersectionResult.Segment); } var pointInRect = beginInRect ? seg.Begin : seg.End; if (pointInRect.Equals(point)) { result = point; return(IntersectionResult.Point); } else { result = new Segment(pointInRect, point); return(IntersectionResult.Segment); } } else { result = points[0]; return(IntersectionResult.Point); } } // точки две if (filledRectangle) { result = new Segment(points[0], points[1]); return(IntersectionResult.Segment); } else { result = new[] { points[0], points[1] }; return(IntersectionResult.Points); } }
public static bool IsPointInsideSimplifiedRectangle(Vector2 point, SimplifiedRectangle rect) { return(IsSimplifiedRectanglesInterescting(rect, new SimplifiedRectangle(point, point))); // :P смекалочка }
public bool Equals(SimplifiedRectangle other) { return(V1.Equals(other.V1) && V2.Equals(other.V2)); }