MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在MATLAB中根据无序边缘数据创建填充多边形? (https://www.labfans.com/bbs/showthread.php?t=24202)

poster 2019-12-10 20:48

如何在MATLAB中根据无序边缘数据创建填充多边形?
 
我想使用[B]无序的[/B]边缘数据(边缘的每个点的X,Y坐标)创建多边形,并且想用某种颜色填充该多边形。

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



[B]回答:[/B]

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

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

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

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

[url=https://stackoverflow.com/questions/4391186]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:17

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.