point inside an area

조회 수: 6 (최근 30일)
Muhammad Abdullah
Muhammad Abdullah 2024년 9월 23일
댓글: Voss 2024년 9월 23일
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.

채택된 답변

Voss
Voss 2024년 9월 23일
편집: Voss 2024년 9월 23일
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
Muhammad Abdullah
Muhammad Abdullah 2024년 9월 23일
Thankyou very much sir, got it now...
Voss
Voss 2024년 9월 23일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Vector Volume Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by