如何根据 X,Y 的集合确定多边形的周长

发布于 2024-10-19 03:28:24 字数 209 浏览 3 评论 0原文

我有一个布尔值[][],其中真实索引构成一个实心多边形。有了这些信息,我如何

  • A. 确定哪些块构成了周长,以及
  • B. 哪些点(按顺序)可以使用多边形类绘制多边形。

我打算使用 Polygon 类来调用 .contains 但我目前的代码给了我乱序的多边形点(因为我只是从左到右、从上到下扫描)。任何有关该主题的帮助都将受到赞赏。

I have a boolean[][] where the true indexes make up a solid polygon. With this information how can I

  • A. determine which blocks make up the perimeter, and
  • B. which points (in order) can draw the polygon using the polygon class.

I intend to use Polygon class to call .contains but the code I have currently gives me the polygon points out of order (as i am simply scanning from left to right, top to bottom). Any help on the subject is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦情居士 2024-10-26 03:28:24

我认为您只需要确定角点并将它们连接起来。凸包算法可能不起作用,因为多边形可能不是凸的。例如,多边形维基百科页面上从左边算起的第三个多边形(绿色多边形)是不是凸的。此外,对于已经是凸的多边形,凸包算法可能会显得过大。

除非我弄错了,否则根据点邻居的状态,布尔值列表中应该有四种类型的点。在常规网格中,索引 i,j 处的元素应该有 8 个邻居。基于这 8 个相邻点,假设您的多边形不接触该区域的任何边缘,您应该有四种不同类型的点:

  1. 内部点 - 将所有 8 个相邻值设置为 true 的点
  2. 内部边缘点 - 具有值的点为 true,并且 8 个邻居中有 5 个设置为 true。这些点位于多边形的远边,但不在两个多边形边的交点处。
  3. 多边形边缘点 - 值为 true 且 8 个邻居中设置为 true 的点少于 5 个的点。这些点是多边形的角点。
  4. 外部点 - 值为 false 的点

您可以遍历布尔值并挑选出多边形的所有边缘点(当多边形接触区域边缘时,您必须格外小心,例如,在最小行和最小列处有一个真值)。获得边缘点后,您可以通过反复试验来确定哪些点连接到哪些点。尝试连接两个点并查看边是否有效。如果边缘有效,那么它将在一侧具有所有真值,在另一侧具有所有假值,直至合理的边际或误差。如果它不具有此属性,则它一定不是多边形的边。

I should think that you would just need to determine the corner points and connect them. A convex hull algorithm may not work because the polygon may not be convex. For instance, the third polygon from the left (the green one) on the wikipedia page for polygons is not convex. Also for polygons that are already convex a convex hull algorithm will likely be overkill.

Unless I am mistaken there should be four types of points in your list of Boolean values based on the status of the point's neighbors. In a regular grid an element at index i,j should have 8 neighbors. Based on these 8 neighbors you should have four different types of points assuming that your polygon does not touch any of the edges of the region:

  1. Interior Points - Points which have all 8 neighboring values set to true
  2. Interior Edge Points - Points which have a value of true and have 5 out of 8 neighbors set to true. These are points which are on the far edge of a polygon but not at the intersection of two polygon edges
  3. Polygon Edge Points - Points which have a value of true and have fewer than 5 out of 8 neighbors set to true. These points occurs are the corner points of the polygon.
  4. Outside Points - Points which have a value of false

You can go through your Boolean values and pick out all of the edge points for the polygon (you will have to take extra care in the case when the polygon is touching an edge of the region, for instance there is a true value at the smallest row and the smallest column). Once you have the edge points you determine which points connect to which by trial and error. Attempt to connect two points and see if the edge is valid. If the edge is valid then it will have all true values on one side and all false values on the other up to a reasonable margin or error. If it does not have this property then it must not be an edge of the polygon.

原谅过去的我 2024-10-26 03:28:24

您正在寻找凸包算法

You're looking for a Convex Hull algorithm.

满意归宿 2024-10-26 03:28:24

首先,您需要一些算法

:一种将 boolean[][] 转换为一组 XY 坐标的方法。

第二:将这些点转换为多边形的方法(阅读凸包算法)

第三:计算该多边形周长的方法。

现在就去填空吧!

You need a couple of algorithms

First: a way to translate your boolean[][] into a set of X-Y coordinates.

Second: a way to convert those points into a polygon (read up on Convex Hull algorithm)

Third: a way to calculate the perimeter of that polygon.

Now go fill in the blanks!

沐歌 2024-10-26 03:28:24

听起来凸包算法还不够,因为多边形可能是凹的。您想要的是位图到矢量算法。查看此链接以获取此类算法的描述:http://cardhouse.com/computer/vector.htm

一旦获得多边形点,您就可以对每个线段进行距离计算。

It doesn't sound like a convex hull algorithm will be sufficient since the polygon might be concave. What you want is a bitmap to vector algorithm. Check out this link for a description of such an algorithm: http://cardhouse.com/computer/vector.htm

Once you've got the polygon points you can just do a distance calculation on each segment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文