计算2D形状通过坐标最小边界矩形 [英] Calculate Minimum Bounding Rectangle Of 2D Shape By Coordinates

查看:360
本文介绍了计算2D形状通过坐标最小边界矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用空间数据重新present点的群集上的地图上的解决方案。我不得不使用的重新present群集的盘区的坐标,找到最小外接矩形可以包含所述点的集群的需要。

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.

是否有简单的算法存在能够计算出这个或有任何内置的功能在C#来实现这一目标。我知道了NetTopologySuite,但我不知道如何/如果我可以用它来达到同样的目的。我有坐标表,所以我需要该字符串列表传递到它,并得到了MBR了。

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.

推荐答案

最简单的解决方案,我认为一个你最有可能寻找,是计算轴对齐边框,这是一个简单的病例发现的最小/最大X'放大器; y的值,然后构建从这些盒子。

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.

我给你假code表示,鉴于你还没有发布你的几何pssed在...前$ P $

I'll give you pseudo-code for that, given that you haven't posted the types that your geometry is expressed in...

type point { float x; float y; }
type box { point topleft; point topright; point bottomleft; point 

function bounding_box(points)
{
  xmin = min(points.x)
  xmax = max(points.x)
  ymin = min(points.y)
  ymax = max(points.y)

  return new box{
    topleft = { x = xmin, y = ymax },
    topright = { x = xmax, y = ymax },
    bottomleft = { x = xmin, y = ymin },
    bottomright = { x = xmax, y = ymin }
  };
}

所以给了这些:

So given these:

point[] points = [[x = -2, y = 0], [x = 1, y = 2], [x = 1, y = 1], [x = -1, y = -2]];
box bounds = bounding_box(points);

下面的一切都会是真实的:

All of the following will be true:

bounds.topleft == [x = -2, y = 2];
bounds.topright == [x = 1, y = 2];
bounds.bottomleft == [x = -2, y = -2];
bounds.bottomright == [x = -1, y = -2];

当然,如果该坐标系在顶部具有最低的坐标(例如,像一个典型的显示器) - ,那么你必须反转的计算方法;或者先计算结果的对象空间,然后转换为逻辑空间之后。

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.

的通知我已经走了一个类型为EX presses四个角落,万一你在将来决定升级到未来的一个任意排列箱(尽管出于同样的原因,你可以只箱子使用点+ 2向量的)。

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).

这篇关于计算2D形状通过坐标最小边界矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆