Rendering an ellipse in polar coordinates that is not circumscribing the origin
21 views (last 30 days)
Show older comments
I need to render ellipses in a polar coordinate plot. I found on the Wikipedia entry for Ellipse the equation for the general polar form, which I implemented as a Matlab function. It is listed below. The function works well until values of r0 and theta0 cause the ellipse to no longer circumscribe the origin. At that point, equation Q generates complex numbers, which does not render properly using Matlab's polar plotting function. Is there an equation for the general polar form of an ellipse that does not have this restriction?
Thanks,
Mac
function [radii, thetas] = PolarEllipse(a, b, r0, theta0, phi, steps)
% This code only works when the polar coordinate (r0,theta0) falls within
% the ellipse. Otherwise, complex numbers are generated in the equation
% for Q.
thetas = -pi:pi/steps:pi;
P = r0 * (((b^2 - a^2) * cos(thetas + theta0 - 2 * phi)) + ...
((a^2 + b^2) * cos(thetas - theta0)));
R = (b^2 - a^2) * cos(2 * thetas - 2 * phi) + a^2 + b^2;
Q = sqrt(2) * a * b .* sqrt(R - (2 * r0^2 .* sin(thetas - theta0).^ 2));
radii = (P + Q) ./ R;
0 Comments
Answers (1)
Roger Stafford
on 24 Jul 2013
Just generate the ellipse parametrically in cartesian coordinates and transform these to polar coordinates. That way it won't matter where the origin is - inside or outside the ellipse.
t = linspace(-pi,pi,n); % n values of the parameter
X = a*cos(t); % The ellipse in standard form
Y = b*sin(t);
x = r0*cos(theta0)+X*cos(phi)-Y*sin(phi); % Rotate & translate
y = r0*sin(theta0)+X*sin(phi)+Y*cos(phi);
radii = sqrt(x.^2+y.^2); % Transform to polar coordinates
thetas = atan2(y,x);
The quantities 'radii' and 'thetas' are your polar coordinates.
See Also
Categories
Find more on Polar Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!