用MATLAB运行koch曲线

用MATLAB运行koch曲线,第1张

把以下程序存为koch.m%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%KOCH: Plots 'Koch Curve' Fractal

%

% koch(n) plots the 'Koch Curve' Fractal after n iterations

% e.g koch(4) plots the Koch Curve after 4 iterations.

% (be patient for n>8, depending on Computer speed)

%

% The 'kline' local function generates the Koch Curve co-ordinates using

% recursive calls, while the 'plotline' local function is used to plot

% the Koch Curve.

%

% Copyright (c) 2000 by Salman Durrani ([email protected])

%--------------------------------------------------------------------

function []=koch(n)

if (n==0)

x=[01]

y=[00]

line(x,y,'Color','b')

axis equal

set(gca,'Visible','off')

else

levelcontrol=10^n

L=levelcontrol/(3^n)

l=ceil(L)

kline(0,0,levelcontrol,0,l)

axis equal

set(gca,'Visible','off'戚锋)

set(gcf,'Name','Koch Curve')

end

%--------------------------------------------------------------------

function kline(x1,y1,x5,y5,limit)

length=sqrt((x5-x1)^2+(y5-y1)^2)

if(length>limit)

x2=(2*x1+x5)/3

y2=(2*y1+y5)/3

x3=(x1+x5)/2-(y5-y1)/(2.0*sqrt(3.0))

y3=(y1+y5)/2+(x5-x1)/(2.0*sqrt(3.0))

x4=(2*x5+x1)/3

y4=(2*y5+y1)/3

% recursive calls

kline(x1,y1,x2,y2,limit)

kline(x2,y2,x3,y3,limit)

kline(x3,y3,x4,y4,limit)

kline(x4,y4,x5,y5,limit)

else

plotline(x1,y1,x5,y5)

end

%--------------------------------------------------------------------

function plotline(a1,b1,a2,b2)

x=[a1a2]

y=[b1b2]

line(x,y)

%--------------------------------------------------------------------%%%%%%%%%%%%然后在命橘仔核令提示圆掘符处运行koch(5)%%%%%%%%%%%%%%%%%%%%%

把下面饥掘的程序保存为plotkoch.m,然后在command windows中输入plotkoch(8)即可,其中8是迭代次数,你可以换成别的整数。

function plotkoch(n)

[x,y]=koch(n)

fill(x,y,'w')

text(0,0,{'\centerline{The area surrounded by Koch Snow Curve is $\displaystyle\frac{2\sqrt {3}{n}^{2}}{5}$.}',...

'\centerline{($n$ is the side length of the original triangle.)}',...

'\centerline{The dimension of the curve is $\log{_34}$.}'},...

'interpreter'桐樱,'latex','horizontalalignment','center')

axis equal off

function [x,y]=koch(n)

if n==1

t=linspace(0,2*pi,4)

x=cos(t)

y=sin(t)

else

p=1/局肢丛3h=p*sqrt(3)/2

[x,y]=koch(n-1)

xx=x(1)yy=y(1)

dx=diff(x)dy=diff(y)

ax=(1-p)*x(1:end-1)+p*x(2:end)

ay=(1-p)*y(1:end-1)+p*y(2:end)

bx=p*x(1:end-1)+(1-p)*x(2:end)

by=p*y(1:end-1)+(1-p)*y(2:end)

ox=(x(1:end-1)+x(2:end))/2+h*dy

oy=(y(1:end-1)+y(2:end))/2-h*dx

x=[reshape([x(1:end-1)axoxbx],1,[]),x(end)]

y=[reshape([y(1:end-1)ayoyby],1,[]),y(end)]

end


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12566376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存