-
-
Save FanFnaf4/0a826f1f9567d9e768d480298a9f7b52 to your computer and use it in GitHub Desktop.
Mandelbrot set generated using python turtle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import math | |
def mandelbrot(z , c , n=20): | |
if abs(z) > 10 ** 12: | |
return float("nan") | |
elif n > 0: | |
return mandelbrot(z ** 2 + c, c, n - 1) | |
else: | |
return z ** 2 + c | |
# screen size (in pixels) | |
screenx, screeny = 800, 600 | |
# complex plane limits | |
complexPlaneX, complexPlaneY = (-2.0, 2.0), (-1.0, 2.0) | |
# discretization step | |
step = 3 | |
# turtle config | |
turtle.tracer(0, 0) | |
turtle.setup(screenx, screeny) | |
turtle.bgcolor("#3B3638") | |
turtle.pensize(2) | |
screen = turtle.Screen() | |
screen.title("Mandelbrot Fractal (discretization step = %d)" % (int(step))) | |
mTurtle = turtle.Turtle() | |
mTurtle.penup() | |
mTurtle.shape("turtle") | |
# px * pixelToX = x in complex plane coordinates | |
pixelToX, pixelToY = (complexPlaneX[1] - complexPlaneX[0])/screenx, (complexPlaneY[1] - complexPlaneY[0])/screeny | |
# plot | |
for px in range(-int(screenx/2), int(screenx/2), int(step)): | |
for py in range(-int(screeny/2), int(screeny/2), int(step)): | |
x, y = px * pixelToX, py * pixelToY | |
m = mandelbrot(0, x + 1j * y) | |
if not math.isnan(m.real): | |
color = [abs(math.sin(m.imag)) for i in range(3)] | |
mTurtle.color(color) | |
mTurtle.dot(step + 0.5, color) | |
mTurtle.goto(px, py) | |
turtle.update() | |
turtle.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment