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

Mic*_*ech 8 algorithm matlab polygon

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

有什么建议我怎么能做到这一点?

gno*_*ice 7

如果多边形是凸的,则可以使用函数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
Run Code Online (Sandbox Code Playgroud)

如果你的多边形是凹的,那就变得更加棘手了.您必须通过比较它们的端点并以顺时针或逆时针方式对它们进行排序来自行重新排序边线.

...但是,如果这听起来像编写太多的工作,你可以通过创建一个受约束的顶点的Delaunay三角剖分,找到约束边内侧的三角形,然后绘制形成这些三角形的这些单独的三角形来回避这个问题.使用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
Run Code Online (Sandbox Code Playgroud)

上面将显示多边形,其边缘线围绕形成它的每个子三角形.如果只想在整个多边形的外部显示边线,可以添加以下内容:

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
Run Code Online (Sandbox Code Playgroud)