递归算法(1) Cantor三分集

        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()

递归算法(1) Cantor三分集_第1张图片

你可能感兴趣的:(算法,python,command,Class,import,button)