Clear Filters
Clear Filters

point inside an area

42 views (last 30 days)
Muhammad Abdullah
Muhammad Abdullah on 23 Sep 2024 at 3:17
Commented: Voss on 23 Sep 2024 at 14:44
Hello all,
I have two points which are inside an area, when I plot them manually, it shows inside it. I am using inpolygon command so as to store all the points inside the area in an array and also segregate them. The following is the code:
w =0.4953;g = 0.5282;
%% polygon are
t = 0:0.1:20;
left_boundary_w = (t.*(t+2))./(t+1).^2;
left_boundary_g = 2*((t.*(t+3))./((t+1).*(t+2)));
lower_boundary_w = 0:0.01:0.5;
lower_boundary_g = 0.*(lower_boundary_w)-0;
right_boundary_w = 0.5:0.01:1;
right_boundary_g = 4.*(right_boundary_w(1:end))-2;
%% plot
figure
plot(left_boundary_w,left_boundary_g);hold on
plot(lower_boundary_w,lower_boundary_g)
plot(right_boundary_w,right_boundary_g)
plot(w,g,'r+','Linewidth',2')
%% Create the boundary vertices
x_boundary = [left_boundary_w, right_boundary_w, lower_boundary_w];
y_boundary = [left_boundary_g, right_boundary_g, lower_boundary_g];
figure;
plot(x_boundary, y_boundary, 'b');hold on
[in,on] = inpolygon(w, g, x_boundary, y_boundary)
in = logical
0
on = logical
0
if in
inside_points = [in; w, g]
end
When I use x_boundary and y_boundary, it also shows an additional fourth line. inpolygon also doesn't show the point inside the polygon.
Any help is appreciated please.

Accepted Answer

Voss
Voss on 23 Sep 2024 at 3:42
Edited: Voss on 23 Sep 2024 at 4:53
The coordinates in x_boundary and y_boundary don't specify the polygon you mean to specify, because the points are out of order. left_boundary goes from origin to (1,2), right_boundary goes from (0.5,0) to (1,2), and lower_boundary goes from origin to (0.5,0). Putting them together in that order gives you a sequence of points with some jumps (which explains the "additional line" in the second plot).
You need to reverse the order of the points in at least one of those boundary vectors, and put the boundary vectors together in the correct order as well, so that each boundary begins where the previous one ends (i.e., no jumps).
For instance:
w =0.4953;g = 0.5282;
%% polygon are
t = 20:-0.1:0;
left_boundary_w = (t.*(t+2))./(t+1).^2;
left_boundary_g = 2*((t.*(t+3))./((t+1).*(t+2)));
lower_boundary_w = 0:0.01:0.5;
lower_boundary_g = 0.*(lower_boundary_w)-0;
right_boundary_w = 0.5:0.01:1;
right_boundary_g = 4.*(right_boundary_w(1:end))-2;
%% plot
figure
plot(left_boundary_w,left_boundary_g);hold on
plot(lower_boundary_w,lower_boundary_g)
plot(right_boundary_w,right_boundary_g)
plot(w,g,'r+','Linewidth',2')
%% Create the boundary vertices
x_boundary = [left_boundary_w, lower_boundary_w, right_boundary_w];
y_boundary = [left_boundary_g, lower_boundary_g, right_boundary_g];
figure;
plot(x_boundary, y_boundary, 'b');hold on
[in,on] = inpolygon(w, g, x_boundary, y_boundary)
in = logical
1
on = logical
0
  2 Comments
Muhammad Abdullah
Muhammad Abdullah on 23 Sep 2024 at 5:32
Thankyou very much sir, got it now...
Voss
Voss on 23 Sep 2024 at 14:44
You're welcome!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!