我想使用
无序的边缘数据(边缘的每个点的X,Y坐标)创建多边形,并且想用某种颜色填充该多边形。
有什么建议我可以做到这一点吗?
回答:
如果多边形是
凸的 ,则可以使用
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 如果您的多边形是
凹形的 ,那将变得更加棘手。您必须自己比较边缘线的端点并以顺时针或逆时针方式对其进行重新排序。
...但是,如果这听起来像是要编写太多代码,您可以通过对顶点进行
约束的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 上面将显示多边形,并在形成多边形的每个子三角形周围带有边线。如果只想在整个多边形的外部显示一条边线,则可以添加以下内容:
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
更多&回答...