在周长上找到表示边界/形状的点 [英] Finding the points on a perimeter to denote a boundary/shape

查看:60
本文介绍了在周长上找到表示边界/形状的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的2D网格,格式为myGrid[x,y]

I have a simple 2D grid with the format of myGrid[x,y]

我正试图找到一种方法来找到所选网格周围的周长,以便随后为所选网格确定形状.

I am trying to find a way to find the perimeter around a selection of grids so i then have a shape for the selection.

这是我的意思的示例:

这里的想法是找到周边图像中所有相关的角"(又名红点")到列表中,这样我便可以从中创建2D网格.但是我不知道最简单的方法.我想知道是否有一个简单的逻辑可以做到这一点?

The idea here is to find all the relevant "corners" aka the red points in the image on the perimeter into a list so i can then create a 2D mesh from it. But i don't know the easiest way to do this. I was wondering if there is a simple logic to do this ?

推荐答案

假设您的形状是完整而坚实的,那么这里有一些想法.
首先,您当然可以通过最小/最大坐标来限制搜索区域:

Assuming your shape is whole and solid, here are some ideas.
First of all, you can of course limit the searching area by min/max coordinates:

然后我将通过以下条件遍历网格单元来找到所有的外角和内角:

Then I'd find all the outer and inner corners by looping through the grid cells with the following criterias:

  • 外角至少有两个空对角线相邻单元格.
  • 内角只有一个相邻的单元格.

然后,您可以在2级循环中迭代找到的角,以找到围绕形状的边界路线:

Then you can just iterate through the found corners in a 2-levels loop to find a bounding route around the shape:

这不是最有效的算法,但是实现起来很简单.

That's not the most effective algorithm, but it's pretty simple to implement.

这是另一种解决方案:

  1. 按照上述条件查找第一个角单元格.
  2. 沿形状一侧移动,直到到达下一个角.利用简单边单元具有两个相邻的空单元和两个相邻的非空单元这一事实.
  3. 重复#2,直到到达#1处的拐角处.

这篇关于在周长上找到表示边界/形状的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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