c# - 如何找到哪个矩形最接近一个点

标签 c# geometry system.drawing

如果我在 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/

相关文章:

c# - pdf 文档中的条形码生成器

c# - MySql:使用 .NET/Connector 使用准备好的语句调用存储过程会更快吗?

c - 使用三角函数绘制实心圆

asp.net - System.Drawing错误: Can or Cannot it be used in an ASP.网络应用程序

c# - 最小起订量:我可以验证一个 setter 只被调用了 N 次吗?

c# - NUnit 或 Fluent Assertions 测试引用相等性?

html - 如何在 html5 Canvas 上绘制一个圆扇区?

ios - 使用iBeacons进行双边

c# - OutOfMemoryException : Out of memory - System. Drawing.Graphics.FromImage

.net - System.Drawing.Color 相等运算符不会将 0xffffffff 视为 Color.White