If I have two System.Drawing.Rectangle
objects on a canvas and a Point
, what is the best way to calculate which Rectangle
(any part of the Rectangle
, not just its Location
Point
) is closest to that Point
?
An example from a unit test:
Rectangle one = new Rectangle (0, 0, 10, 10);
Rectangle two = new Rectangle (20, 20, 10, 10);
Point point = new Point(14, 14);
Rectangle actual = ClosestToPoint(point, one, two);
// should be closer to one since one's bottom right is at (10, 10)
Assert.That(actual, Is.SameAs(one));
// method to write
public Rectangle ClosestToPoint(Point p, params Rectangle[] rectangles) { }
distance to rectangle = min (distance to each of the 4 line segments that are the edges of the rectangle)
For distance to line segment, see this question