Drawing and filling different polygons at the same time in MATLAB
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 P开发者_开发问答oly2,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
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.
I think patch
is a better tool for drawing filled polygons. Check it out!
精彩评论