2013-03-28 46 views

回答

4

如果您有PDE工具箱,您可以使用pdeellip。否則,你可以這樣寫:

% input ellipse parameters 
theta_grid = linspace(0,2*pi); 
phi = 45*180/pi; 
X0=10; 
Y0=20; 
a=40; 
b=15; 

% the ellipse in x and y coordinates 
ellipse_x_r = X0 + a*cos(theta_grid); 
ellipse_y_r = Y0 + b*sin(theta_grid); 

%Define a rotation matrix 
R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ]; 

%let's rotate the ellipse to some angle phii 
r_ellipse = R * [ellipse_x_r;ellipse_y_r]; 

plot(r_ellipse(1,:),r_ellipse(2,:),'-x') 

enter image description here

這裏的另一個選項,而不是xy座標, 「郵票」 的橢圓形到一個數組:

a=20; 
b=9; 
phi=45; 
[x y] = meshgrid(-50:50,-50:50); 
el=((x-X0)/a).^2+((y-Y0)/b).^2<=1; 
imagesc(imrotate(el,phi)); colormap(bone) 

enter image description here

1

嘿,我認爲解決方案存在問題。 由於旋轉以中心爲0定義,導致一些較爲奇怪的結果(如果您看一下它,繪製的橢圓的中心不是(10,20)),旋轉正在應用。

我認爲正確的答案應該是:

theta_grid = linspace(0,2*pi); 
phi = 45*180/pi; 
X0=10; 
Y0=20; 
a=40; 
b=15; 

% the ellipse in x and y coordinates centered at 0 
ellipse_x_r = a*cos(theta_grid); 
ellipse_y_r = b*sin(theta_grid); 

% Define a rotation matrix 
R = [ cos(phi) sin(phi); -sin(phi) cos(phi) ]; 
n = length(ellipse_x_r); 

% let's rotate the ellipse to some angle phii and then translate it to (X0, Y0) 
r_ellipse = R * [ellipse_x_r; ellipse_y_r] + repmat([X0; Y0], [1, n]); 

plot(r_ellipse(1,:),r_ellipse(2,:),'-x') 

感謝您的解決方案,雖然我無法找到如何做到這一點的任何地方,這是最有幫助的帖子,我發現。

乾杯!
Pablo

編輯:heh,SO的代碼格式化程序存在一個錯誤。 '評論裏面的內容不是字符串=)