使用Graphics.py绘制cantor集

时间:2018-03-28 14:23:34

标签: python-3.x graphics

我正在尝试使用python中的线条在图形窗口上绘制一个cantor集。 到目前为止我所拥有的是

from graphics import *

def cantor_set(win,x,y,h,Len): 

    if Len < 2: return

    line = Line(Point(x, y), Point(x+Len, y))
    line.setWidth(10)
    line.draw(win)

    cantor_set(win,x,y+h,h,Len//3)
    cantor_set(win,x+Len*(2//3),y+h,h,Len//3)

def cantor_set_starter():
    win = GraphWin("Cantor Set", 500, 250)
    cantor_set(win,5,10,20,490)

cantor_set_starter()

但是运行它会返回

Cantor Set Crash

然后窗口崩溃没有错误,我不知道为什么。

1 个答案:

答案 0 :(得分:0)

我最终用这个

来解决它
from graphics import *

def cantor_set(win,x,y,h,Len): 

    if Len < 2: return

    line = Line(Point(x, y), Point(x+Len, y))
    line.setWidth(10)
    line.draw(win)

    cantor_set(win,x,y+h,h,Len//3)
    cantor_set(win,x+Len*2//3,y+h,h,Len//3)

def cantor_set_starter():
    Len = 490
    win = GraphWin("Cantor Set", 500, 250)
    cantor_set(win,5,10,20,490)
    win.getMouse()
    win.close()

cantor_set_starter()

似乎数学在第二次递归时很糟糕,并且添加了.getmouse()和.close()修复了崩溃