在MATLAB中同时绘制和填充不同的多边形 [英] Drawing and filling different polygons at the same time in MATLAB

查看:686
本文介绍了在MATLAB中同时绘制和填充不同的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
.....

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

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


推荐答案

此解决方案可能适用于您:

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

这会使用 PATCH 创建多边形功能.要为补丁添加不同的颜色,请查看此MATLAB文档.

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

这篇关于在MATLAB中同时绘制和填充不同的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆