在 MATLAB 中同时绘制和填充不同的多边形

发布于 2024-09-03 10:51:00 字数 874 浏览 6 评论 0原文

我有下面的代码。它将 CSV 文件加载到内存中。该文件包含不同多边形的坐标。该文件的每一行都有 X、Y 坐标和一个字符串,该字符串告诉该数据点属于哪个多边形。例如,名为“Poly1”且包含 100 个数据点的多边形在此文件中具有 100 行,如下所示:

Poly1,X1,Y1
Poly1,X2,Y2
...
Poly1,X100,Y100
Poly2,X1,Y1
.....

index.csv 文件具有文件 Polygons.csv 中每个多边形的数据点数量(行数)。这些细节并不重要。事情是: 我可以使用下面的代码成功提取每个多边形的数据点。 然而,当我绘制不同多边形的线时,它们是相互连接的,并且绘图看起来很糟糕。我需要将多边形分开(尽管它们是连接的并且某些区域重叠)。我认为通过使用“填充”我实际上可以更好地看到它们。但“填充”只是填充它能找到的每个多边形,这是不可取的。我只想填充多边形内部。有人可以帮助我吗?如有必要,我还可以向您发送我的数据点,它们小于 200Kb。 谢谢

 
[coordinates,routeNames,polygonData] = xlsread('Polygons.csv');
index = dlmread('Index.csv');
firstPointer = 0
lastPointer = index(1)
for Counter=2:size(index)
    firstPointer = firstPointer + index(Counter) + 1
    hold on
    plot(coordinates(firstPointer:lastPointer,2),coordinates(firstPointer:lastPointer,1),'r-')
    lastPointer = lastPointer + index(Counter)
end


I have the code below. It load a CSV file into memory. This file contains the coordinates for different polygons.Each row of this file has X,Y coordinates and a string which tells that to which polygon this datapoint belongs. for example a polygone named "Poly1" with 100 data points has 100 rows in this file like :

Poly1,X1,Y1
Poly1,X2,Y2
...
Poly1,X100,Y100
Poly2,X1,Y1
.....

The index.csv file has the number of datapoint(number of rows) for each polygon in file Polygons.csv. These details are not important. The thing is:
I can successfully extract the datapoints for each polygon using the code below.
However, When I plot the lines of different polygons are connected to each other and the plot looks crappy. I need the polygons to be separated(they are connected and overlapping the some areas though). I thought by using "fill" I can actually see them better. But "fill" just filles every polygon that it can find and that is not desirable. I only want to fill inside the polygons. Can someone help me? I can also send you my datapoint if necessary, they are less than 200Kb.
Thanks

 
[coordinates,routeNames,polygonData] = xlsread('Polygons.csv');
index = dlmread('Index.csv');
firstPointer = 0
lastPointer = index(1)
for Counter=2:size(index)
    firstPointer = firstPointer + index(Counter) + 1
    hold on
    plot(coordinates(firstPointer:lastPointer,2),coordinates(firstPointer:lastPointer,1),'r-')
    lastPointer = lastPointer + index(Counter)
end


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

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

发布评论

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

评论(2

海螺姑娘 2024-09-10 10:51:00

此解决方案可能适合您:

[coordinates,routeNames,polygonData] = xlsread('Polygons.csv');  %# Load the data
for polyName = unique(routeNames(:).')       %'# Loop over unique polygons
  polyIndex = ismember(routeNames,polyName);  %# Find index of polygon points
  x = coordinates(polyIndex,:);               %# Get x coordinates
  y = coordinates(polyIndex,:);               %# Get y coordinates
  patch(x,y);                                 %# Plot a patch
  hold on;                                    %# Add to the existing plot
end

这使用 PATCH 创建多边形 函数。要为补丁设置不同的颜色,请查看 此 MATLAB文档

This solution may work for you:

[coordinates,routeNames,polygonData] = xlsread('Polygons.csv');  %# Load the data
for polyName = unique(routeNames(:).')       %'# Loop over unique polygons
  polyIndex = ismember(routeNames,polyName);  %# Find index of polygon points
  x = coordinates(polyIndex,:);               %# Get x coordinates
  y = coordinates(polyIndex,:);               %# Get y coordinates
  patch(x,y);                                 %# Plot a patch
  hold on;                                    %# Add to the existing plot
end

This creates the polygons using the PATCH function. To color the patches differently, check out this MATLAB documentation.

街道布景 2024-09-10 10:51:00

我认为 patch 是绘制填充多边形的更好工具。 看看

I think patch is a better tool for drawing filled polygons. Check it out!

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