# How do I identify points within a user defined shape?

2 views (last 30 days)
Mariam Shahab on 11 Oct 2022
Commented: David Hill on 13 Oct 2022
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:
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!

David Hill on 11 Oct 2022
Look at polyshape and isinterior
##### 2 CommentsShowHide 1 older comment
David Hill on 13 Oct 2022
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)