如何在MATLAB中从无序边缘数据创建填充多边形?

8
我希望创建一个多边形,使用边缘数据(每个边点的X,Y坐标),这些数据是无序的,我想用一些颜色填充该多边形。 有什么建议吗?

你的边缘数据是如何存储的(变量数量、大小等)? - gnovice
2个回答

6

如果你的多边形是凸多边形,你可以使用函数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

如果你的多边形是凹的,那就变得更加棘手了。你需要通过比较它们的端点并按顺时针或逆时针的方式对它们进行重新排序。
...但是,如果这听起来太费力来编码,你可以通过创建一个受限德劳内三角剖分的顶点点集,找到受限边内部的三角形,然后使用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

+1 有用的答案。我在考虑将点转换为极坐标,然后按角度排序。 - Amro
@Amro:一个有趣的想法,但从技术上讲,如果拐角够尖锐,就可以拥有一个凹多边形,它可以反转角度方向,有点像这个多边形 - gnovice
我发现我的问题更加复杂,但这是对我的问题的很好的答案。 - MicTech
超级棒的分析。 - Summer Sun

0

我认为你正在寻找patch()函数。你可以用它来创建二维和三维多边形。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接