Determining point is within polygon or outside.

so far I have coded a profile and a user input to plot the points if inside or outside. any improvement to the code is appreciable

9 Comments

%polygonal coordinates
close all
clear
clc
xv = [0 0 0 20 20 0 10 20 20 20 20 30 30 50 50 20 30 40 50]
yv = [0 0 50 10 0 0 0 0 0 10 50 50 10 10 0 0 0 0 0 ]
% plotting the points as polygonas
figure (1)
plot(xv, yv,'-')
hold on
%square axis
grid on
axis([-10 60 -10 60])
%specifying the N coordinate
% xq = [22 45];
% yq = [25 35];
[X] = ginput(2);
[X] = round(X);
xq = X(1:2);
yq = X(3:4);
%polygon and point plotting
[in,on] = inpolygon(xq,yq,xv,yv);
%Determine the number of points lying inside or on the edge of the polygon area.
numel(xq(in))
%Determine the number of points lying on the edge of the polygon area.
numel(xq(on))
%Determine the number of points lying outside the polygon area (not inside or on the edge).
numel(xq(~in))
%Plot the polygon and the query points. Display the points inside the polygon with a red plus. Display the points outside the polygon with a blue circle.
figure (1)
plot(xv,yv) % polygon
axis([-10 60 -10 60])
hold on
plot(xq(in),yq(in),'r+') % points inside
plot(xq(~in),yq(~in),'bo') % points outside
hold off
Adam Danz
Adam Danz on 25 Jul 2018
Edited: Adam Danz on 25 Jul 2018
Also see
In this updated code, what is 'in' and 'on'? Or is that where you're stuck? If that's where you're stuck, how does this question differ from the previous one?
Guten tag The question is similar I guess Except I don't know how to put this as an updated code in the previous one , that's why made a new one.
In means inside polygon ,on denotes on the polygon line. But I tried with numel(xq(in)) == numel(xq(-on)
numel(xq(in)) > numel(xq(-in)) But it still says points inside the profile
When I run the code you shared, 'in' is not defined anywhere. 'in' has no values. So the code breaks when it tries to execute 'in'.
In on comes as ans and it gets window. I guess it should not be defined? I may be wrong too, kindly show me with an assigned value to it and how it works
'in' and 'on' have no values. If you run the code you provided, it will break when it tries to execute 'in'. I don't know what 'in' and 'on' represent or what their values 'should' be. I'm guessing they are indices and their values are probably [0 1] or [1 0].
If you have a more clearer or specific question maybe I can help. But the biggest problem in your current code is that 'in' and 'on' have no values.
Thanks für the reply. I actually tried using inpolygon for the code ,it didn't worked well as I'm selecting only 2 coords as user input .I think it works with random points. So here im trying to print a statement saying ginputs are within profile or outside profile using inpolygon Function. Actually the present code shows the points in and out in colours. I need to attach a print statement to it .
Kaleesh,m see your other thread where I answered this question. inpolygon() does work well with your code.

Sign in to comment.

Answers (0)

Categories

Products

Asked:

on 25 Jul 2018

Commented:

on 25 Jul 2018

Community Treasure Hunt

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

Start Hunting!