Rayman的专栏

欢迎参观我的blog: http://virtualcastleoffyzhao.spaces.live.com

原创 递归算法(1) Cantor三分集收藏

新一篇: LS文法构图算法(2) Koch雪花 | 旧一篇: LS文法构图算法(1) 简介

        Cantor三分集的构造如下图所示,一条线段ab被均分为三段,保留其两边的两段,中间一段去掉,然后把得到的每一段再继续进行划分,如此反复。

 

        Cantor三分集的绘制十分简单,是一种最简单的分形实例,它的算法如下:

cx = ax + ( bx – ax ) / 3

cy = ay + h

dx = bx – ( bx – ax ) / 3

dy = by + h

ay = ay – h

by = by – h

其中h为两层之间的距离。

        Cantor三分集的python程序实现及其运行结果如下:

#!/apps/bin/python
from Tkinter import *

class Cantor(Frame):
    
    limit 
= 1


    
def __init__(self, master=None):
        Frame.
__init__
(self, master)
        self.grid()
        self.createWidgets()

    
def
 createWidgets(self):
        
#self.quitButton = Button(self, text="QUIT", command=self.quit)

        #self.quitButton.pack(side=BOTTOM, fill=BOTH)
        self.draw = Canvas(self, width=800, height=500)
        self.draw.pack(side
=
LEFT)
        self.drawCanvas(
100,100,700,100
)

    
def
 drawCanvas(self,ax,ay,bx,by):
        self.draw.create_line(ax,ay,bx,by)
        
if ((bx-ax)>
self.limit):
            cx 
= ax + (bx - ax) / 3
;
            cy 
= ay + 50
;
            dx 
= bx - (bx - ax) / 3
;
            dy 
= by + 50
;
            ay 
= ay + 50
;
            by 
= by + 50
;
            self.drawCanvas(ax,ay,cx,cy)
            self.drawCanvas(dx,dy,bx,by)
            



app 
=
 Cantor()
app.master.title(
"Cantor (recursive)"
)
app.mainloop()

发表于 @ 2007年01月18日 13:07:00|评论(loading...)|编辑

新一篇: LS文法构图算法(2) Koch雪花 | 旧一篇: LS文法构图算法(1) 简介

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © Rayman