如何建立从MATLAB中无序边缘数据的多边形填充? [英] How can I create a filled polygon from unordered edge data in MATLAB?
问题描述
我想用边缘数据来创建一个多边形(X,Y每个点边的坐标)是无序,然后我想填补这个多边形一些颜色。
I want to create a polygon using edge data (X,Y coordinates of each point of edge) that is unordered, and I want to fill that polygon with some color.
任何建议,我怎么能做到这一点?
Any suggestions how I can accomplish this?
推荐答案
如果您的多边形是凸,您可以用函数 CONVHULL 公正计算从顶点的凸包>并使用绘图功能 PATCH 绘制多边形。例如:
If your polygon is convex, you can just compute the convex hull from the vertices using the function CONVHULL and plot the polygon using the plotting function PATCH. For example:
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
如果您的多边形来代替的凹的,即变得棘手。你必须通过比较它们的端点和命令他们在顺时针或者逆时针的方式来重新调整边缘线自己。
If your polygon is instead concave, that becomes trickier. You would have to reorder the edge lines yourself by comparing their end points and ordering them in either a clockwise or counterclockwise fashion.
...但是,如果这听起来像工作太多了,code,你可以通过创建的的顶点的约束Delaunay三角,的找到关于受约束边缘的内部的三角形,然后绘制形成使用的 PATCH 。例如:
...but if that sounds like too much work to code up, you can sidestep the issue by creating a constrained Delaunay triangulation of the vertex points, find the triangles on the inside of the constrained edges, then plot these individual triangles that form the polygon using PATCH. For example:
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
上面会显示周围的每个子三角形,形成其边缘线的多边形。如果你只是想围绕整个多边形的外所示的边缘线,你可以添加以下内容:
The above will display the polygon with edge lines around each sub-triangle that forms it. If you just want an edge line shown around the outside of the entire polygon, you can add the following:
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
这篇关于如何建立从MATLAB中无序边缘数据的多边形填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!