问题
Can anyone tell me why this code always has a line on the screen and also how to stop it?
Slight problem with this is that every time this happens, I always get a line on my canvas no matter what I try to do. Any ideas on how to prevent this?
Now this is probably too much code, but I have no idea how else to show the failing code. I have tried a variety of ways but none seem to work.
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)
Maybe the getcolour is failing so here is the code for it:
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)
回答1:
I've reworked your code to get rid of the line and fix other issues that I saw:
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()
Despite its title, I don't picture this drawing a Mandelbrot but it should at least scan correctly.
UPDATE
Now that you've provided getcolour()
, the Mandelbrot nature of this becomes clear. Below is the complete reworked code and some output:
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 (reduced in size, still not finished running)
Neither the prettiest nor the fastest implementation but it is a Mandelbrot.
Now that you've seen your code works on my system, you need to review your environment (Python version, Windows or Unix, etc.) to see what's different. The above was done with Python 3.6.0 on Max OSX 10.11
来源:https://stackoverflow.com/questions/46483288/how-to-stop-the-python-turtle-from-drawing