PDA

查看完整版本 : 如何在MATLAB中根据无序边缘数据创建填充多边形?


poster
2019-12-10, 20:48
我想使用无序的边缘数据(边缘的每个点的X,Y坐标)创建多边形,并且想用某种颜色填充该多边形。

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



回答:

如果多边形是凸的 (http://en.wikipedia.org/wiki/Convex_and_concave_polygons) ,则可以使用CONVHULL (http://www.mathworks.com/help/techdoc/ref/convhull.html)函数从顶点计算凸包,并使用绘制函数PATCH (http://www.mathworks.com/help/techdoc/ref/patch.html)绘制多边形。例如:

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 如果您的多边形是凹形的 ,那将变得更加棘手。您必须自己比较边缘线的端点并以顺时针或逆时针方式对其进行重新排序。

...但是,如果这听起来像是要编写太多代码,您可以通过对顶点进行约束的Delaunay三角剖分来 (http://www.mathworks.com/help/techdoc/ref/delaunaytri.html)解决问题,在约束边缘的内部找到三角形 (http://www.mathworks.com/help/techdoc/ref/delaunaytri.inoutstatus.html) ,然后绘制出形成三角形的各个三角形使用PATCH的 (http://www.mathworks.com/help/techdoc/ref/patch.html)多边形。例如:

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

更多&回答... (https://stackoverflow.com/questions/4391186)