原文http://blog.csdn.net/fyzhao/archive/2007/01/18/1486592.aspx
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 程序实现及其运行结果如下:
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.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()