How to plot all the points between two lines?

So, I have one line that goes from these lat lons :
lat10 = [6.6038,21.841667]; lon10 = [94.4166,62.375000];
plot(x10,y10,'-','color','y');%plotting the line
plot(x10-d,y10,'-','color','y');%plotting a line parallel to the above line
now I'm reading a set of cordinates from an excel sheet :
lat1 = xlsread('101010.xlsx', 'B2:B300');
lon1 = xlsread('101010.xlsx', 'C2:C300');
I want to only plot the points which are within these two lines.If any one point is not in between the lines, I don't want to plot anything. Please help me with how I can do this?

 Accepted Answer

It seems that you have an area inside a parallelogram defined by four points. Then you can use the inpolygon method as follows:
% find the X of the four points
X = [lat10(1),lat10(2),lat10(1)-d,lat10(2)-d];
Y = [lon10(1),lon10(2),lon10(1),lon10(2)];
% the X and Y of the other points which may or not be inside the parallelogram
lat1 = xlsread('101010.xlsx', 'B2:B300');
lon1 = xlsread('101010.xlsx', 'C2:C300');
[in,on] = inpolygon(lat1,lon1,X,Y);
% (in) contains the indices of the points inside the parallelogram
% (on) contains the indices of the points on the parallelogram
% do some plots
figure
plot(X,Y) % parallelogram
hold on;
plot(lat1(in),lon1(in),'r+') % points inside
plot(lat1(~in),lon1(~in),'bo') % points outside
hold off

13 Comments

The command plot(lat1(in),lon1(in),'r+') % points inside
Doesn't workUntitled.png
Where did the polar axes come from? You do not have any calls to polar() or polarplot() or to any mapping toolbox routines that might have a polar projection.
Okay My bad... I had to put the whole code so that it would become more clear.
This is what I'm doing..
Reading the data:
lat1 = xlsread('plot.xlsx', 'C2:C83400');
lon1 = xlsread('plot.xlsx', 'D2:D83400');
lat10 = [6.6038,21.841667]; lon10 = [94.4166,62.375000];
Renderind the data:
%% Render the recorded data
% Prepare figure
d=370400;
load coords %#ok<*UNRCH>
close all
opengl software % Hardware OpenGL rendering is unreliable for exporting images
figure('Renderer','opengl',...
'DefaultTextFontName', 'Miryad Pro', ...
'DefaultTextFontSize', 10,...
'DefaultTextHorizontalAlignment', 'right')
% Settings
centerLoc = [12.9716,77.5946]; % LEVC
lineColor = [0.7,0.7,0.7];
vertiExag = 5; % vertical exaggeration
markSize = 30;
maxRadius = 500e3; % bullseye max radius (m)
hold on
% Prepare UTM scenario
mstruct = defaultm('utm');
mstruct.zone = utmzone(centerLoc(1),centerLoc(2));
mstruct = defaultm(mstruct);
% Plot land contours
SHPdir = '.\SHPs\';
countries = shaperead([SHPdir 'ne_10m_admin_0_countries.shp'],...
'Selector',{@(x) strcmpi(x,'es'),'foo'},'UseGeoCoords', true);
% Change 'ES.VC' for the provinces/states of your preference or use a RegExp
% for all provinces: @(x) strcmpi(x,'ES.VC') => @(x) ~isempty(regexpi(x,'^ES.*$'))
provinces = shaperead([SHPdir 'ne_10m_admin_1_states_provinces.shp'],...
'Selector',{@(x) strcmpi(x,'ES.VC'),'region_cod'},'UseGeoCoords', true);
[x,y] = mfwdtran(mstruct,[countries.Lat provinces.Lat],[countries.Lon provinces.Lon]);
[xc,yc] = mfwdtran(mstruct,centerLoc(1),centerLoc(2));
[x1,y1]= mfwdtran(mstruct,lat1,lon1);
[x10, y10] = mfwdtran(mstruct,[lat10(1),lat10(2),lat10(1)-d,lat10(2)-d],[lon10(1),lon10(2),lon10(1),lon10(2)]);
Plotting:
% Plot bullseye
t = linspace(0,2*pi);
for i = 0:50e3:maxRadius
plot(xc+i.*cos(t),yc+i.*sin(t),'-','color',lineColor)
if i>0
text(xc+i-15e3, yc-15e3, [num2str(i/1e3) 'km'],'color',lineColor,'HorizontalAlignment','center');
end
end
% Plot lines
plot([xc xc],[yc-i-10e3 yc+i+10e3],'-','color',lineColor)
plot([xc-i-10e3 xc+i+10e3],[yc yc],'-','color',lineColor)
plot(x10,y10,'-','color','y');%plotting ats
plot(x10-d,y10,'-','color','y');%only for first batch
hold on ;
Why are you doing all that work with the bullseye yourself? Why not call polarplot(), or polar() if you have an older MATLAB ?
I haven't used polar plot so I made it by myself
I doesn't really matter, does it?
You are suffering from a misalignment of the polar plot with your other plots. It would be easier to figure out that problem if we were not having to debug the polar logic and the other logic at the same time.
Note: polar() is the older routine. It constructs the background plot and labels and so on, all in cartesian axes. If you were to overlay data on it, in the same axes, then the overlay would be specified in cartesian coordinates.
polarplot() is the newer routine that is more flexible in matters such as plotting only sectors and adjusting the label formats. It does not use cartesian coordinates at the user level. If you were to overlay data on it, in the same axes, then the overlay would be specified in polar coordinates.
Oh... alright!
I'll use polar instead of the bullseye, but how do I do the other thing?
You use inpolygon() like A. Sawas showed.
Make sure you get x and y right. Latitude corresponds to y, not to x.
I'm still having the issue that even the cordinates which are outside the bound (in polygon cordinates) are being shown...
I didn't quite understand how to solve this.
Using the details you provided try this code:
X = [x10,fliplr(x10)-d];
Y = [y10,fliplr(y10)];
[in,on] = inpolygon(x1,y1,X,Y);
% (in) contains the indices of the points inside the parallelogram
% (on) contains the indices of the points on the parallelogram
% do some plots
%plot(X,Y) % parallelogram
plot(x1(in),y1(in),'r+') % points inside
plot(x1(~in),y1(~in),'bo') % points outside
This worked!!
Thanks a lot Sawas. :')

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products

Asked:

on 6 Apr 2019

Commented:

on 8 Apr 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!