如果我在 Canvas 上有两个 System.Drawing.Rectangle
对象和一个 Point
,什么是最佳方法来计算哪个 Rectangle
(Rectangle
的任何部分,而不仅仅是它的 Location
Point
)最接近那个 Point
?
来自单元测试的示例:
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)
到线段的距离,见this question
关于c# - 如何找到哪个矩形最接近一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4250703/