point inside an area

6 visualizzazioni (ultimi 30 giorni)
Muhammad Abdullah
Muhammad Abdullah il 23 Set 2024
Commentato: Voss il 23 Set 2024
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.

Risposta accettata

Voss
Voss il 23 Set 2024
Modificato: Voss il 23 Set 2024
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 Commenti
Muhammad Abdullah
Muhammad Abdullah il 23 Set 2024
Thankyou very much sir, got it now...
Voss
Voss il 23 Set 2024
You're welcome!

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by