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

13 ビュー (過去 30 日間)
Yakubu Galadima
Yakubu Galadima 2021 年 8 月 19 日
コメント済み: Yakubu Galadima 2021 年 8 月 19 日
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.
  1 件のコメント
Yakubu Galadima
Yakubu Galadima 2021 年 8 月 19 日
Thank you so much Wan. Your suggestion did solved my problem. Once again, many thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by