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

时间:2010-12-08 19:01:08

标签: algorithm matlab polygon

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

我有什么建议可以做到这一点吗?

2 个答案:

答案 0 :(得分:7)

如果多边形为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

答案 1 :(得分:0)

我认为您正在寻找patch()功能。你可以用它做2-D和3-D多边形。