通过坐标计算二维形状的最小外接矩形
我有一个解决方案,它使用空间数据来表示地图上的一组点。我需要使用表示簇范围的坐标来查找可以包含所述点簇的最小边界矩形。
是否存在能够计算此值的简单算法,或者 C# 中是否有任何内置功能可以实现此目的。我知道 NetTopologySuite,但不确定如何/是否可以使用它来实现相同的目标。我有一个坐标列表,因此我需要将此字符串列表传递到其中并取出 MBR。
I have a solution that uses spatial data to represent a cluster of points on a map. I have the need to used the coordinates that represent the extents of a cluster to find the minimum bounding rectangle that can contain said cluster of points.
Does any simple algorithm exist to be able to calculate this or is there any built in functionality in C# to achieve this. I am aware of the NetTopologySuite but am not sure how/if I could use this to achieve the same goal. I have a list of coordinates so I would need to pass this list of strings into it and get the MBR out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的解决方案(我认为您最有可能正在寻找的解决方案)是计算轴对齐边界框,这只是查找最小/最大 x 和 x 的情况。 y 值,然后根据这些值构建一个盒子。
鉴于您还没有发布几何图形所表达的类型,我将为您提供伪代码...
所以考虑到这些:
以下所有内容都是正确的:
当然,如果坐标系具有顶部的最低坐标(例如,像典型的显示器一样)-那么您必须反转计算;或者先在对象空间中计算结果,然后再转换到逻辑空间。
请注意,我已经选择了表达所有四个角的框的类型,以防您将来决定更新为任意对齐的框(尽管出于同样的原因,您可以只使用点 + 2 个向量那)。
The easiest solution, and I assume the one you're most likely to be looking for, is to calculate the axis-aligned bounding box, which is simply a case of finding the min/max x & y values, then constructing a box from those.
I'll give you pseudo-code for that, given that you haven't posted the types that your geometry is expressed in...
So given these:
All of the following will be true:
Of course, if the coordinate system has the lowest coordinates at the top (e.g. like a typical display) - then you have to invert the calculation; or calculate the result in object-space first and then translate to logical space afterwards.
Notice I've gone for a type for the box that expresses all four corners, in case you decide in the future to update to an arbitrarily aligned box in the future (although by the same token you could just use a point + 2 vectors for that).
一种可能但简单的方法可能是这样的:
当然,这假设您正在寻找垂直和水平对齐的矩形。因此,如果您正在寻找尽可能小的矩形,无论它如何旋转,这都不适合您。
One possible, though simple, way to do it could be like this:
This does of course assume that you're looking for a rectangle that is aligned vertically and horizontally. So if you're looking for the smallest possible rectangle, no matter how it is rotated, this is not for you.
G#
在 http://www.ceometric.com/products/g.html尝试 具有最小面积和最小外接矩形以及最小外接圆。
Try G# at http://www.ceometric.com/products/g.html
It has minimum area and minimum perimeter enclosing rectangles and also minimum enclosing circles.