How do I identify points within a user defined shape?

조회 수: 3 (최근 30일)
Mariam Shahab
Mariam Shahab 2022년 10월 11일
댓글: David Hill 2022년 10월 13일
Hi all,
I have the folliwing code:
% genertae random data
%initial parameters
npoints= 300;
xrange = [0 100];
yrange = [0 100];
% generating randomly placed points
points = rand(npoints, 2);
% normalizing the xy locations according to the parameters
points(:,1) = points(:,1)*diff(xrange) + xrange(1);
points(:,2) = points(:,2)*diff(yrange) + yrange(1);
%plot
figure(2), clf
plot(points(:,1),points(:,2), 'b+','linewidth',2, 'markersize',12);
hold on
n=5;
a=zeros(n,1);
b=zeros(n,1);
for i=1:5
[x,y] = ginput(1); % these 5 points can be used to make any kind of shape (preferable small in size) on the plot
h1 = text(x,y,int2str(i), ...
'HorizontalAlignment','center', ...
'Color', [1 0 0], ...
'FontSize',8);
a(i)=x;
b(i)=y;
end
%plotting a line between all points
plot([a ; a(1)],[b ; b(1)], 'b-');
hold off
% references:
% https://www.youtube.com/watch?v=9C-MLBZ6u0Q
% https://www.mathworks.com/matlabcentral/answers/1812505-how-to-show-selected-points-and-connect-first-point-to-last-point?s_tid=prof_contriblnk
I want to find a way to identify all those points that lie within a user defined shape. A few examples of user defined shapes would be like:
I will appreciate if someone can kindly help me out. Thanks!

답변 (1개)

David Hill
David Hill 2022년 10월 11일
Look at polyshape and isinterior
  댓글 수: 2
Mariam Shahab
Mariam Shahab 2022년 10월 13일
Thank you for your help. I was able to use both functions to identify the points. Just one question; how do I plot the identified points using the TFin array?
David Hill
David Hill 2022년 10월 13일
x=10*rand(1,1000);
y=10*rand(1,1000);
p=polyshape([2 5 5 2],[2 2 6 2]);
figure;
hold on;
idx=isinterior(p,x',y');
scatter(x(idx),y(idx),10,'red','filled');
scatter(x(~idx),y(~idx),10,'blue','filled')
plot(p)

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

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by