Simple code to draw a 2-D ellipse

Sometime you want to add ellipses to your figures or images in Matlab? Rather than update to Matlab 2010 or buy PDE toolbox, you could google around and hopefully you find here: A simple code illustrates how you can do it.


Input parameters of 2-D ellipse

clc, clear, close all 
x0 = 3; y0 = 3; % Center of ellip 
a = 2; b = 1;   % Major and minor radius 
phi = 30;       % Orientation (degree) 
num = 100;      % #points -> smoothness 

draw 'ellip = scaled circle' at origin

theta = linspace(0,2*pi,num);
p(1,:) = a*cos(theta);
p(2,:) = b*sin(theta);
figure(1), hold on 
axis equal
Rotate to derised angle

phi = phi*pi/180; % deg->rad 
% Rotation matrix: 
Q = [cos(phi) -sin(phi)
    sin(phi)  cos(phi)];
p = Q*p;
Move to derised location

p(1,:) = p(1,:) + x0;
p(2,:) = p(2,:) + x0;


Anonymous said...

Why do you show your know how like this? should keep them somewhere :)

Trung Huy Duong said...

Partly because I am not sure where is somewhere, so just place here.
By the way, one might found it helps, other don't- (and I am happy with this :))

Anonymous said...

Yes this is helpful for me

Anonymous said...

This is amazing! I was looking how to move to a specific location for long. Thank you very very much. Only one correction in the p(2,:) for the devised location. It is:
p(1,:) = p(1,:) + x0;
p(2,:) = p(2,:) + y0;
Many thanks!!