Skip to content

Instantly share code, notes, and snippets.

@tarnacious
Created July 20, 2012 16:22
Show Gist options
  • Select an option

  • Save tarnacious/3151666 to your computer and use it in GitHub Desktop.

Select an option

Save tarnacious/3151666 to your computer and use it in GitHub Desktop.
Mandelbrot
***
***
* *********
**************
***************
******************
***** ******************
***************************
**************************************
***************************
***** ******************
******************
***************
**************
* *********
***
***
import sys
def plot_function(z, c):
return (z * z) + c
def has_escaped(z):
mag = (z.real * z.real) + (z.imag * z.imag)
return (mag > 10000)
def plot_point(x, y):
c = complex(x,y)
z = complex(0,0)
for i in range(200):
if has_escaped(z):
return False
z = plot_function(z, c)
return True
def draw(width, height, x1, y1, x2, y2):
step_x = abs(x1 - x2) / width
step_y = abs(y1 - y2) / height
for y in range(height):
for x in range(width):
point_x = x1 + (x * step_x)
point_y = y1 - (y * step_y)
character = '*' if plot_point(point_x, point_y) else ' '
sys.stdout.write(character)
print("")
if __name__ == '__main__':
draw(50, 20, -2.0, 1.0, 1.0, -1.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment