How do I identify points within a user defined shape?

3 vues (au cours des 30 derniers jours)
Mariam Shahab
Mariam Shahab le 11 Oct 2022
Commenté : David Hill le 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:
% 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!

Réponses (1)

David Hill
David Hill le 11 Oct 2022
Look at polyshape and isinterior
  2 commentaires
Mariam Shahab
Mariam Shahab le 13 Oct 2022
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 le 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)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by