[Beginner] Constructing a rectangle object and detecting points inside

23 visualizaciones (últimos 30 días)
Hello,
On a 2D environnement, I would like to create a rectangle object.
This being done, I would like to be able to detect from a vector of points (x,y), which one are inside the rectangle area and which one are outside.
Could someone help me please? :D

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 17 de Abr. de 2021
Independently, I'd suggest the rectangle and inpolygon functions. However, to find the points inside the rectangle, I'd use patch instead of rectangle, since it gives you access to the x and y values of the shape.
x=4*rand(1,10)+1;
y=2*rand(1,10)+2;
% view data
scatter(x,y)
hold on
p = patch([2,4,4,2],[2 2 4 4],nan);
axis([1 6 1 5])
% find data points inside rectangle
in=inpolygon(x,y,p.Vertices(:,1),p.Vertices(:,2));
% replot data inside shape, this time filled in
scatter(x(in),y(in),'filled')
hold off

Más respuestas (0)

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by