在维基百科上,椭圆文章有一个简单的JavaScript代码来绘制椭圆。
它使用参数方程式:
x(theta) = a0 + ax*sin(theta) + bx*cos(theta)
y(theta) = b0 + ay*sin(theta) + by*cos(theta)
在哪里
(a0,b0) is the center of the ellipse
(ax,ay) vector representing the major axis
(bx,by) vector representing the minor axis
我将代码翻译成了MATLAB函数:
calculateEllipse.m
function [X,Y] = calculateEllipse(x, y, a, b, angle, steps)
narginchk(5, 6);
if nargin<6, steps = 36; end
beta = -angle * (pi / 180);
sinbeta = sin(beta);
cosbeta = cos(beta);
alpha = linspace(0, 360, steps)' .* (pi / 180);
sinalpha = sin(alpha);
cosalpha = cos(alpha);
X = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta);
Y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta);
if nargout==1, X = [X Y]; end
end
并附有一个测试示例:
%
%
%
p = calculateEllipse(0, 0, 20, 10, 50)
plot(p(:,1), p(:,2), '.-'), axis equal