有人能告诉我为什么这段代码总是在屏幕上有一条线,还有如何阻止它?
这方面的一个小问题是,每次发生这种情况时,无论我尝试做什么,我总是在画布上画一条线。关于如何防止这种情况的任何想法?
现在这可能是代码太多了,但我不知道如何显示失败的代码。我尝试了各种各样的方法,但似乎都没有。
def drawpixel(x, y, colour):
turtle.goto(x, y)
turtle.dot(1, colour)
def main():
screen = turtle.Screen()
screen.title('Mandelbrot set drawer')
screen.bgcolor('#22ccaa')
turtle.hideturtle()
turtle.speed(0)
turtle.penup()
turtle.tracer(-10000, 0)
turtle.speed(0)
width = int(turtle.numinput('Screen size', 'What width?', 600, 100, 20000))
height = int(turtle.numinput('Screen size', 'What height?', 600, 100, 10000))
turtle.setworldcoordinates((-width)//2, (-height)//2, (width)//2, (height)//2)
radius = 2
turtle.goto((-width)//2, (-height)//2)
x, y = ((-width)//2, (-height)//2)
while y<((height)//2 +1):
while x!=((width)//2 +1):
newx = x/(width//2)*radius
newy = y/(width//2)*radius
mpos = newx + newy*1j
drawpixel(x, y, getcolour(mpos))
x += 1
y += 1
turtle.goto((-width)//2, y)
也许getcolour失败所以这里是代码:
def iterc(c):
iters = 0
z = 0+0j
while iters < 16 and math.hypot(z.real, z.imag)<2:
z = z*z+c
iters += 1
return iters
def getcolour(pos):
x = iterc(pos)
colournum = int(x*6.25)
colour = 'gray{}'.format(colournum)
答案 0 :(得分:1)
我已经重新编写了代码以摆脱界限并解决我看到的其他问题:
def main():
screen = turtle.Screen()
screen.title('Mandelbrot Set')
screen.bgcolor('#22ccaa')
width = int(screen.numinput('Screen size', 'What width?', 600, 100, 20000))
height = int(screen.numinput('Screen size', 'What height?', 600, 100, 10000))
screen.setworldcoordinates(-width // 2, -height // 2, width // 2, height // 2)
screen.tracer(0, 0)
radius = 2
turtle.penup()
turtle.hideturtle()
turtle.speed('fastest')
x, y = -width // 2, -height // 2
turtle.goto(x, y)
while y < (height // 2):
while x < (width // 2):
newx = x / (width // 2) * radius
newy = y / (width // 2) * radius
mpos = newx + newy * 1j
drawpixel(x, y, getcolour(mpos))
x += 1
x, y = -width // 2, y + 1
screen.update()
尽管有标题,但我并没有将这幅画描绘成Mandelbrot,但它至少应该扫描。
<强>更新强>
现在你提供了getcolour()
,Mandelbrot的性质变得清晰了。下面是完整的返工代码和一些输出:
import math
import turtle
def iterc(c):
iters = 0
z = 0 + 0j
while iters < 16 and math.hypot(z.real, z.imag) < 2:
z = z * z + c
iters += 1
return iters
def getcolour(pos):
x = iterc(pos)
colournum = int(x * 6.25)
return 'gray{}'.format(colournum)
def drawpixel(x, y, colour):
turtle.goto(x, y)
turtle.dot(1, colour)
def main():
screen = turtle.Screen()
screen.title('Mandelbrot Set')
screen.bgcolor('#22ccaa')
width = int(screen.numinput('Screen size', 'What width?', 600, 100, 20000))
height = int(screen.numinput('Screen size', 'What height?', 600, 100, 10000))
screen.setworldcoordinates(-width // 2, -height // 2, width // 2, height // 2)
screen.tracer(0, 0)
radius = 2
turtle.penup()
turtle.hideturtle()
turtle.speed('fastest')
x, y = -width // 2, -height // 2
turtle.goto(x, y)
while y < (height // 2):
while x < (width // 2):
newx = x / (width // 2) * radius
newy = y / (width // 2) * radius
mpos = newx + newy * 1j
drawpixel(x, y, getcolour(mpos))
x += 1
x, y = -width // 2, y + 1
screen.update()
main()
OUTPUT(尺寸减小,仍未完成运行)
既不是最漂亮也不是最快的实施,但它是Mandelbrot。
现在您已经看到您的代码在我的系统上运行,您需要查看您的环境(Python版本,Windows或Unix等),看看它们有什么不同。以上是在Max OSX 10.11上使用Python 3.6.0完成的。