我正在Python中使用Zelle图形包来绘制Cantor三元模型(如IFS(迭代函数系统)图)的迭代。 我要绘制的迭代函数系统是:
{R;x/9+1/6,2/3+x/9,x/9+1/3,x/9+5/6}
以下代码是对我在Internet上找到的内容的模仿。代码正确吗?如何使每个线段具有不同的颜色,以及如何在每个阶段标记点?
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(20)
line.draw(win)
cantor_set(win,2*x+3,y+h,h,Len//18)
cantor_set(win,2*x+12,y+h,h,Len//18)
cantor_set(win,2*x+6,y+h,h,Len//18)
cantor_set(win,2*x+15,y+h,h,Len//18)
def cantor_set_starter():
Len = 790
win = GraphWin("Cantor Set", 800, 200)
c = cantor_set(win,5,20,50,790)
win.postscript(file = "can1.eps")
#win.getMouse()
#win.close()
cantor_set_starter()
答案 0 :(得分:0)
编程中有两种主要的错误类型:语法和算法。您的程序同时遭受这两种痛苦。我们可以很容易地解决您的语法错误:
from graphics import *
def cantor_set(win, x, y, h, length):
if length < 2:
return
line = Line(Point(x, y), Point(x + length, y))
line.setWidth(20)
line.draw(win)
cantor_set(win, 2 * x + 3, y + h, h, length // 18)
cantor_set(win, 2 * x + 12, y + h, h, length // 18)
cantor_set(win, 2 * x + 6, y + h, h, length // 18)
cantor_set(win, 2 * x + 15, y + h, h, length // 18)
def cantor_set_starter(length):
cantor_set(win, 5, 20, 50, length)
win = GraphWin("Cantor Set", 800, 200)
cantor_set_starter(790)
win.getMouse()
win.close()
但是由于存在算法错误,因此它不会绘制cantor三元图。为了帮助您解决这些问题,我们需要更多地了解迭代函数系统方程的获得位置以及其他详细信息。如果目标只是绘制Cantor集,我们可以修改程序来这样做,主要是通过扔掉代码并在剩下的地方调整数学来实现:
from graphics import *
def cantor_set(win, x, y, height, length):
if length < 2:
return
line = Line(Point(x, y), Point(x + length, y))
line.setWidth(height / 2)
line.draw(win)
length /= 3
cantor_set(win, x, y + height, height, length)
cantor_set(win, x + 2 * length, y + height, height, length)
win = GraphWin("Cantor Set", 800, 300)
cantor_set(win, 5, 20, 50, 790)
win.getMouse()
win.close()