任意角度的图像画法(椭圆、矩形、菱形)

玖、如何畫橢圓

馮丁樹


 

某一特定點離兩固定點間之距離和為一定時之軌跡為橢圓。事實上圓的圖形亦可作為橢圓之特殊形,因為只要橢圖之長軸與短軸相等時,即可以作圓。首先仍然需要宣告下面之指令:

  •   axis equal;

 繪圖參數

本程式中之繪線,仍採用line的功能,逐點連線繪製。但是連線時係以直線表示,因此必須在適當的點數下所繪製的圖才能近似橢圓形,故點數也相當重要。在ellipse之函數當中,其輸入之變數分別如下:

-ra 圓之長軸半徑,可為列矩陣,若為列矩陣時,代表可同時繪製許多圓。

-rb 圓之矩軸半徑,可為列矩陣,如ra

-ang 水平軸之傾斜角,以弧度表示。可為列矩陣,但rarbang三者之矩陣數應相同。

-x0,y0 圓心之座標,可為矩陣,若為矩陣時,代表可同時繪製許多不同圓心位置之橢圓。

-C 圓線之顏色

-Nb 圓時所用之點數。

 

基本上橢圓圓周之構成可用三角函數計算:

 

  • %  x=ra*cosθ*cosα-rb*sinθ*sinα+xpos;

  • %  y=ra*cosθ*sinα-rb*sinθ*cosα+xpos;

 

2π>θ≧0

 

其中角度θ則應自零至360度。而其區間應為θ/ Nb

ra為單一項,x0,y0為向量矩陣,則會繪製向量矩陣數之

x0,y0為單一項,ra為向量矩陣,則會繪製同一橢圓心之不同半徑向量矩陣數之

x0,y0ra均為同大小之矩陣向量,則會繪製數目相同之圓。

x0,y0ra均為不同大小之矩陣,則會繪製總數為兩矩陣數目之乘積。

 

基本應用說明如下:

  • ELLIPSE(ra,rb,ang,x0,y0,C)

會增加線顏色之選項CC可為字串如('r','b',...) RGB 值。若不給C值則會自動依序配顏色。C亦可為矩陣向量。

  • ELLIPSE(ra,rb,ang,x0,y0,C,Nb)

Nb 說明所用繪製之點數,其預設值為300點。若有多個橢圓,Nb可用矩陣分別設定。  

  • h=ELLIPSE(...)

h為該圓之握把代號。

例一:繪製一個傾斜某一角度之

  • ellipse(1,2,pi/8,1,1,'r')

 

 

1  具有傾斜角度之橢圓

 

 

 

例二:繪製多個同心

  • clf

  • ellipse([1:6],[0.5:0.1:1.0])

 

2. 具有同心之橢圓

 

例三:繪製原子模型,即旋轉長軸由0度作4590135度旋轉。

  • clf

  • axis equal

  • ellipse([1 1 1 1],[.5 0.5 0.5 0.5], [0 45 90 135] *pi/180)

 

3. 橢圓型之轉動

 

例四、橢圓之程式亦可作菱型的改變,只要改變最後一項之Nb值即可。

  • clf

  • ellipse(1,.5, [0 45 90 135] *pi/180,0,0,[],4)

 

 

4. 菱形角度之變化

 

例五、正方型亦可作轉角之變化,此程式之rarb令其相等。

 

  • clf

  • ellipse(1,1, [0 30 60 90] *pi/180,0,0,[],4)

5 方形角度之變化

 

例六、水滴石穿。半徑及高度y均變化時。

  • clf

  • ra=1./[0.1:0.1:2];

  • ellipse(ra,0.5*ra, 0, 0,[1:length(ra)]*.6, [])

 

6. 水滴石穿之情況

 

橢圓之函數程式

 

  • function h=ellipse(ra,rb,ang,x0,y0,C,Nb)

  • % Ellipse adds ellipses to the current plot

  • %

  • % ELLIPSE(ra,rb,ang,x0,y0) adds an ellipse with semimajor axis of ra,

  • % a semimajor axis of radius rb, a semimajor axis of ang, centered at

  • % the point x0,y0.

  • %

  • % The length of ra, rb, and ang should be the same.

  • % If ra is a vector of length L and x0,y0 scalars, L ellipses

  • % are added at point x0,y0.

  • % If ra is a scalar and x0,y0 vectors of length M, M ellipse are with the same

  • % radii are added at the points x0,y0.

  • % If ra, x0, y0 are vectors of the same length L=M, M ellipses are added.

  • % If ra is a vector of length L and x0, y0 are  vectors of length

  • % M~=L, L*M ellipses are added, at each point x0,y0, L ellipses of radius ra.

  • %

  • % ELLIPSE(ra,rb,ang,x0,y0,C)

  • % adds ellipses of color C. C may be a string ('r','b',...) or the RGB value.

  • % If no color is specified, it makes automatic use of the colors specified by

  • % the axes ColorOrder property. For several circles C may be a vector.

  • %

  • % ELLIPSE(ra,rb,ang,x0,y0,C,Nb), Nb specifies the number of points

  • % used to draw the ellipse. The default value is 300. Nb may be used

  • % for each ellipse individually.

  • %

  • % h=ELLIPSE(...) returns the handles to the ellipses.

  • %

  • % as a sample of how ellipse works, the following produces a red ellipse

  • % tipped up at a 45 deg axis from the x axis

  • % ellipse(1,2,pi/8,1,1,'r')

  • %

  • % note that if ra=rb, ELLIPSE plots a circle

  • %

  • % rewritten by Din-sue Fon. Dept. of Bio-Industrial Mechatronics Engineering,

  • % National Taiwan University March 10,2001

  • % dsfong@ccms.ntu.edu.tw

  • % % rewritten by Din-sue Fon. Dept. of Bio-Industrial Mechatronics Engineering,

  • % National Taiwan University March 10,2001

  • % dsfong@ccms.ntu.edu.tw

  • % written by D.G. Long, Brigham Young University, based on the

  • % CIRCLES.m original

  • % written by Peter Blattner, Institute of Microtechnology, University of

  • % Neuchatel, Switzerland, blattner@imt.unine.ch

  •   % Check the number of input arguments

  • switch nargin

  • case 0

  •    ra=1;rb=1;ang=0;x0=0;y0=0;C=[];Nb=300;

  • case 1

  •    rb=ra;ang=0;x0=0;y0=0;C=[];Nb=300;

  • case 2

  •    ang=0;x0=0;y0=0;C=[];Nb=300;

  • case 3

  •    x0=0;y0=0;C=[];Nb=300;

  • case 4

  •    y0=zeros(1,length(x0));C=[];Nb=300;

  • case 5

  •    C=[];Nb=300;

  • case 6

  •    Nb=300;

  • end

  •  

  •  % set up the default values

  •  if isempty(ra),ra=1;end;

  • if isempty(rb),rb=1;end;

  • if isempty(ang),ang=0;end;

  • if isempty(x0),x0=0;end;

  • if isempty(y0),y0=0;end;

  • if isempty(Nb),Nb=300;end;

  • if isempty(C),C=get(gca,'colororder');end;

  •  % work on the variable sizes

  •  x0=x0(:);

  • y0=y0(:);

  • ra=ra(:);

  • rb=rb(:);

  • ang=ang(:);

  • Nb=Nb(:);

  •  if isstr(C),C=C(:);end;

  • if length(x0)~=length(y0),

  •    if length(y0)==1,

  •       y0=ones(1,length(x0))*y0;

  •    elseif length(x0)==1,

  •       x0=ones(1,length(y0))*x0;

  •    else

  •       error('The lengths of x0 and y0 must be identical');

  •    end;

  • end; if length(ra)~=length(rb),

  •    if length(ra)==1,

  •       ra=ones(1,length(rb))*ra;

  •    elseif length(x0)==1,

  •       rb=ones(1,length(ra))*rb;

  •    else

  •       error('The lengths of ra and rb must be identical');

  •    end;

  • end;   % how many inscribed elllipses are plotted

  •  if length(ra)~=length(x0)

  •   maxk=length(ra)*length(x0);

  • else

  •   maxk=length(ra);

  • end;

  •     % drawing loop

  • route=0;

  • if length(x0)==1, route=1; end

  • if length(ra)==1, route=2; end

  • if length(x0)==length(ra), route=3; end

  • if length(ang)>1 & length(ra)==1,

  •    route=4;maxk=length(ang);

  • end

  •  for k=1:maxk

  •   switch route

  •   case 1

  •     xpos=x0;

  •     ypos=y0;

  •     radm=ra(k);

  •     radn=rb(k);

  •     if length(ang)==1, an=ang; else an=ang(k); end;

  •   case 2

  •     xpos=x0(k);

  •     ypos=y0(k);

  •     radm=ra;

  •     radn=rb;

  •     if length(ang)==1, an=ang; else an=ang(k); end;

  •   case 3

  •     xpos=x0(k);

  •     ypos=y0(k);

  •     radm=ra(k);

  •     radn=rb(k);

  •     if length(ang)==1, an=ang; else an=ang(k); end;

  •   case 4

  •     xpos=x0;

  •     ypos=y0;

  •     radm=ra;

  •     radn=rb;

  •     an=ang(k);

  •   otherwise

  •     rada=ra(fix((k-1)/size(x0,1))+1);

  •     radb=rb(fix((k-1)/size(x0,1))+1);

  •     an=ang(fix((k-1)/size(x0,1))+1);

  •     xpos=x0(rem(k-1,size(x0,1))+1);

  •     ypos=y0(rem(k-1,size(y0,1))+1);

  •   end;

  •    co=cos(an);

  •   si=sin(an);

  •   the=linspace(0,2*pi,Nb(rem(k-1,size(Nb,1))+1,:)+1);

  • %  x=radm*cos(the)*co-si*radn*sin(the)+xpos;

  • %  y=radm*cos(the)*si+co*radn*sin(the)+ypos;

  •   h(k)=line(radm*cos(the)*co-si*radn*sin(the)+xpos,radm*cos(the)*si+co*radn*sin(the)+ypos);

  •   set(h(k),'color',C(rem(k-1,size(C,1))+1,:));

  •  end;

  • axis equal;grid on;

  摘自: http://www.ecaa.ntu.edu.tw/weifang/matlab/matlabintro9.htm

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值