public Rectangle PutNextRectangle(Size rectangleSize) { while (true) { var currentPoint = spiral.GetNextPoint(); var rectangle = RectangleUtils.GetClosestRectangleThatDoesNotIntersectWithOthers( currentPoint, rectangleSize, Center, rectangles); if (rectangle == null) { continue; } rectangles.Add(rectangle.Value); return(rectangle.Value); } }
public static Rectangle?GetClosestRectangleThatDoesNotIntersectWithOthers( Point possibleLocation, Size size, Point center, IEnumerable <Rectangle> rectangles) { var currentRectangles = GetPossibleRectangles(possibleLocation, size) .Where(r1 => !rectangles.Select(r2 => RectangleUtils.RectanglesAreIntersected(r1, r2)).Any(t => t)).ToList(); Rectangle?closest = null; while (true) { var newClosest = DistanceUtils.GetClosestToThePointRectangle(center, currentRectangles); if (newClosest == null) { return(closest); } closest = newClosest; currentRectangles = GetRectanglesThatCloserToPoint(center, closest.Value, 1) .Where(r1 => !rectangles.Select(r2 => RectangleUtils.RectanglesAreIntersected(r1, r2)).Any(t => t)).ToList(); } }
public static double GetDistanceFromRectangleToPoint(Point point, Rectangle rectangle) { return(RectangleUtils .GetRectangleSides(rectangle) .Min(s => GetDistanceFromSegmentToPoint(point, s))); }