plot ellipse with given degree range

Hi:
I follow the way to plot ellipse in posted in this thread:
it works pretty good when I plot a full ellipse, however, when I try to plot ellipse with given range, it seems a little bit different than what I expect, example is below:
t = linspace(0,0.25*pi,100);
theta = deg2rad(0);
a=2;
b=1;
x0 = 0;
y0 = 0;
x = x0 + a*cos(t)*cos(theta) - b*sin(t)*sin(theta);
y = y0 + b*sin(t)*cos(theta) + a*cos(t)*sin(theta);
figure;
plot(x,y);
axis equal;
atand(y(end)/x(end))
the angle calculated by y(end)/x(end) is 26 degree, however, I give 0.25*pi in the theta range, it is expected to be 45 degree.
is there any mistake with my understanding?
thanks!
Yu

Answers (3)

t = linspace(0,45,100); theta=0; %degrees
a=2; b=1;
x0 = 0; y0 = 0;
N=1000;
p=translate( scale(nsidedpoly(N),[a,b]) , x0,y0);
V=interp1(linspace(-90,+270,N) ,flipud(p.Vertices),t);
plot(p,'FaceColor','none','EdgeColor','b'); hold on
plot(V(:,1), V(:,2),'r.'); hold off; axis equal
you would be right just in case of a circle (a = b) but in general for an ellipse with a different from b , the angle made at the end point with the origin is not the parametric angle (t) used to construct the curve
think at the ellipse as a circle being distorted (anamorphosis factor = b/a) in one direction : as the distorsion is applied only in one direction these angles cannot match
%% circle : a = b
alpha = 0.25*pi;
t = linspace(0,alpha,100);
theta = deg2rad(0);
a=1;
b=1;
x0 = 0;
y0 = 0;
x = x0 + a*cos(t)*cos(theta) - b*sin(t)*sin(theta);
y = y0 + b*sin(t)*cos(theta) + a*cos(t)*sin(theta);
figure;
plot(x,y);
hold on
plot([0 cos(alpha)],[0 sin(alpha)],'--r');
axis equal;
atand(y(end)/x(end))
ans = 45
%% ellipse : a =/= b
alpha = 0.25*pi;
t = linspace(0,alpha,100);
theta = deg2rad(0);
a=2;
b=1;
x0 = 0;
y0 = 0;
x = x0 + a*cos(t)*cos(theta) - b*sin(t)*sin(theta);
y = y0 + b*sin(t)*cos(theta) + a*cos(t)*sin(theta);
figure;
plot(x,y);
hold on
plot([0 cos(alpha)],[0 sin(alpha)],'--r');
axis equal;
atand(y(end)/x(end))
ans = 26.5651

4 Comments

Yu Li
Yu Li on 14 Apr 2025
Moved: Image Analyst on 14 Apr 2025
Hi:
thank you, I think I know why now.
the new question is - is there anyway to generate ellipse curve with given degree range, such as from 0 to 45 degree?
Thanks!
Yu
sure
%% Solution #1 : compute the full elipse and keep what is inside the range (red dots)
alpha = [0 0.25]*pi; % lower & upper limit to plot
t = linspace(0,2*pi,300);
theta = deg2rad(0);
a=2;
b=1;
x0 = 0;
y0 = 0;
x = x0 + a*cos(t)*cos(theta) - b*sin(t)*sin(theta);
y = y0 + b*sin(t)*cos(theta) + a*cos(t)*sin(theta);
myangle = atan2(y,x);
ind = (myangle>=alpha(1) & myangle<=alpha(2)); % plot segment for angle between 0 and alpha
figure;
plot(x,y); hold on
grid on
plot(x(ind),y(ind),'or');
plot([0 a*cos(alpha(1))],[0 a*sin(alpha(1))],'--r');
plot([0 a*cos(alpha(2))],[0 a*sin(alpha(2))],'--r');
axis equal;
%% Solution #2 : compute the limits with trogonometric equations
alpha = [0.1 0.25]*pi; % lower & upper limit to plot
theta = deg2rad(0);
a=2;
b=1;
x0 = 0;
y0 = 0;
t1 = atan2(a*sin(alpha(1)),b*cos(alpha(1)));
t2 = atan2(a*sin(alpha(2)),b*cos(alpha(2)));
t = linspace(t1,t2,100);
x = x0 + a*cos(t)*cos(theta) - b*sin(t)*sin(theta);
y = y0 + b*sin(t)*cos(theta) + a*cos(t)*sin(theta);
figure;
plot(x,y); hold on
grid on
plot([0 a*cos(alpha(1))],[0 a*sin(alpha(1))],'--r');
plot([0 a*cos(alpha(2))],[0 a*sin(alpha(2))],'--r');
axis equal;
Do you want the major and minor axes aligned with the axes? If not, let me know because I have code to rotate the ellipse axes by a specified angle.
hello @Yu Li
problem solved ?

Sign in to comment.

Using this FEX package,
t = linspace(0,45,100); theta=0; %degrees
a=2; b=1;
x0 = 0; y0 = 0;
xy=ellipticalFit.xysim([x0,y0],[a,b],theta, t);
plot(xy(1,:), xy(2,:),'.'); axis padded

Categories

Find more on Graphics in Help Center and File Exchange

Asked:

on 14 Apr 2025

Commented:

on 4 Jul 2025

Community Treasure Hunt

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

Start Hunting!