Borrar filtros
Borrar filtros

How to find points inside a region defined by a rotated ellipse

21 visualizaciones (últimos 30 días)
Hello. I have a rotated ellipse defined by a center [x0,y0], the major and minor axes r1 and r2 as well as the angle of rotation, theta. My goal is to determine whether a given point [x y] lie within the regoin defined by this ellipse. Thanks

Respuesta aceptada

Wan Ji
Wan Ji el 19 de Ag. de 2021
Here is an example of telling how to calculate whether a given point [x y] lie within the regoin defined by this ellipse.
function main
x = rand(1000,1); % the x coordinates of 1000 points
y = rand(1000,1); % the y coordinates of 1000 points
xc = 0.4; % centre x coordinate of the ellipse
yc = 0.5; % centre y coordinate of the ellipse
a = 0.3; % major semi-axis
b = 0.1; % minor semi-axis
theta = 54 * pi / 180; % the orientation of ellipse 54 degree
p = inellipse(x, y, xc, yc, a, b, theta) ;
% plot the ellipse boundary
plot_ellipse(xc, yc, a, b, theta); hold on
% scatter the points in the ellipse, markered in red
scatter(x(p), y(p), 10, 'r', 'filled');
% scatter the points out of the ellipse, markered in blue
scatter(x(~p), y(~p), 10, 'b', 'filled');
legend('Ellipse boundary','In ellipse', 'Out ellipse')
axis equal
end
function p = inellipse(x, y, xc, yc, a, b, theta)
% return a logical array, indices of the points in the ellipse
xr = x - xc;
yr = y - yc;
x0 = cos(theta)*xr + sin(theta)*yr;
y0 = -sin(theta)*xr + cos(theta)*yr;
p = x0.^2 / a^2 + y0.^2 / b^2 < 1;
end
function plot_ellipse(xc, yc, a, b, theta)
% plot an ellipse
alpha = linspace(0,2*pi,101);
x0 = a*cos(alpha);
y0 = b*sin(alpha);
x = cos(theta)*x0 - sin(theta)*y0 + xc; % rotate and translate
y = sin(theta)*x0 + cos(theta)*y0 + yc;
plot(x,y,'k--')
end
To this example, you will obtain a figure which validates this method.
  1 comentario
Yakubu Galadima
Yakubu Galadima el 19 de Ag. de 2021
Thank you so much Wan. Your suggestion did solved my problem. Once again, many thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Scatter Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by