如果我有一个画布上的两个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) { }
用一张纸和一支铅笔对此进行分类。数量有限的情况。否则它是一个模棱两可的问题,你需要指定当一个点在一个或多个矩形内时会发生什么。 – 2010-11-22 22:20:32
一些答案也可以在这里找到: http://stackoverflow.com/questions/5254838/calculating-distance-between-a-point-and-a-rectangular-box-nearest-point/ – Mo0gles 2014-04-22 13:21:26