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

发布于 2024-10-06 12:50:34 字数 100 浏览 0 评论 0原文

我想使用无序的边缘数据(边缘每个点的 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌梦深 2024-10-13 12:50:34

如果您的多边形是,您可以使用函数 CONVHULL 并使用绘图函数 补丁。例如:

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 三角剖分找到约束边内侧的三角形,然后使用 补丁。例如:

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

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.

...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
傲世九天 2024-10-13 12:50:34

I think you're looking for the patch() function. You can do 2-D and 3-D polygons with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文