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

21 次查看(过去 30 天)
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

采纳的回答

Wan Ji
Wan Ji 2021-8-19
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Scatter Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by