如果我有一個畫布上的兩個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