algorithm - 如何在 MATLAB 中根据无序边数据创建填充多边形?

标签 algorithm matlab polygon

我想使用无序的边缘数据(每个边缘点的 X、Y 坐标)创建一个多边形,并且我想用某种颜色填充该多边形。

有什么建议可以实现吗?

最佳答案

如果您的多边形是 convex ,您可以使用函数 CONVHULL 从顶点计算凸包并使用绘图函数 PATCH 绘制多边形.例如:

x = [0 1 0 1];  %# Unordered x coordinates of vertices
y = [0 1 1 0];  %# Corresponding y coordinates of vertices
hullIndices = convhull(x,y);  %# Gives vertex indices running counterclockwise
                              %#   around the hull
patch(x(hullIndices),y(hullIndices),'r');  %# Plot the polygon in red

如果您的多边形是 凹面,那就更棘手了。您必须自己重新排序边缘线,方法是比较它们的端点并以顺时针或逆时针方式对它们进行排序。

...但是如果编写代码听起来工作量太大,您可以通过创建一个 constrained Delaunay triangulation 来回避这个问题。的顶点,find the triangles on the inside of the constrained edges ,然后使用 PATCH 绘制形成多边形的这些单独的三角形.例如:

x = [0 1 0 1 0.5];    %# Unordered x coordinates of vertices
y = [0 1 1 0 0.5];    %# Corresponding y coordinates of vertices
edgeLines = [1 3;...  %# Point 1 connects to point 3
             1 4;...  %# Point 1 connects to point 4
             2 3;...  %# Point 2 connects to point 3
             2 5;...  %# Point 2 connects to point 5
             5 4];    %# Point 5 connects to point 4
dt = DelaunayTri(x(:),y(:),edgeLines);  %# Create a constrained triangulation
isInside = inOutStatus(dt);  %# Find the indices of inside triangles
faces = dt(isInside,:);      %# Get the face indices of the inside triangles
vertices = [x(:) y(:)];      %# Vertex data for polygon
hPolygon = patch('Faces',faces,...
                 'Vertices',vertices,...
                 'FaceColor','r');  %# Plot the triangular faces in red

上面的代码将显示多边形,每个子三角形周围都有边线。如果您只想在整个多边形的外部显示一条边缘线,您可以添加以下内容:

set(hPolygon,'EdgeColor','none');  %# Turn off the edge coloring
xEdge = x(edgeLines).';           %'# Create x coordinates for the edge
yEdge = y(edgeLines).';           %'# Create y coordinates for the edge
hold on;                           %# Add to the existing plot
line(xEdge,yEdge,'Color','k');     %# Plot the edge in black

关于algorithm - 如何在 MATLAB 中根据无序边数据创建填充多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4391186/

相关文章:

algorithm - 求阶乘 n 模 m 比 O(n) 更快

algorithm - 当我想选择项目以尽可能满地填充容器时,它叫什么 - 我应该使用什么算法?

matlab - matlab中用于变换3D图像的插值或重采样算法,最好是sinc插值

c - 组织/显示用 C 或 MATLAB 编码的算法的方法

python - 关闭多边形的算法

c++ - 使用 Boost.Polygon 对曼哈顿多边形进行切片

c++ - 使用三数比较器排序

algorithm - 如何在平面图中找到最大流?

python - 我的 python 代码一般都很慢,这正常吗?

api - 从 Opensea API/Polygonscan API 获取 MATIC/Polygon Assets 元数据