Extra#
Mandelbrot#
Tijdens deze opdracht ga je een programma schrijven om de punten in en rond de mandelbrotverzameling weer te geven en te verkennen.
# laat deze importregel staan...
from png import *
#
# een testfunctie...
#
def test_fun():
""" algorithmic image-creation one pixel at a time...
this is a test function: it should output
an image named test.png in the same directory
"""
im = PNGImage(300, 200) # maak een afbeelding met width=300, height = 200
# Geneste lussen!
for r in range(200): # lust over de rijen met lusvariabele r
for c in range(300): # lust over de kolommen met c
if c == r:
im.plot_point(c, r, (255, 0, 0))
# else:
# im.plot_point( c, r, (255,0,0))
im.save_file()
#
# zet je functies hieronder neer:
#
def mult(c, n):
"""Mult uses only a loop and addition
to multiply c by the positive integer n
"""
result = 0
for i in range(n):
result += c
return result
assert mult(3, 5) == 15
assert mult(6, 7) == 42
assert mult(1.5, 28) == 42.0
def update(c, n):
"""Update starts with z = 0 and runs z = z**2 + c
for a total of n times. It returns the final z.
"""
z = 0
for i in range(n):
z = z ** 2 + c
return z
assert update(1, 3) == 5
assert update(-1, 3) == -1
assert update(-1, 10) == 0
def in_mset(c, n):
"""in_mset accepts
c for the update step of z = z**2+c
n, the maximum number of times to run that step
Then, it returns
False as soon as abs(z) gets larger than 2
True if abs(z) never gets larger than 2 (for n iterations)
"""
z = 0
for i in range(n):
z = z ** 2 + c
if abs(z) > 2:
return False
#return z
return True
c = 0 + 0j
assert in_mset(c, 25) == True
c = 3 + 4j
assert in_mset(c, 25) == False
c = 0.3 + -0.5j
assert in_mset(c, 25) == True
c = -0.7 + 0.3j
assert in_mset(c, 25) == False
c = 0.42 + 0.2j
assert in_mset(c, 25) == True
assert in_mset(c, 50) == False
def we_want_this_pixel(col, row):
"""This function returns True if we want to show
the pixel at col, row and False otherwise.
"""
if col % 10 == 0 and row % 10 == 0:
# if col % 10 == 0 or row % 10 == 0:
return True
else:
return False
def test():
"""This function demonstrates how
to create and save a PNG image.
"""
width = 300
height = 200
image = PNGImage(width, height)
# maak een lus om wat pixels te tekenen
for col in range(width):
for row in range(height):
if we_want_this_pixel(col, row):
image.plot_point(col, row)
# we hebben door alle pixels gelust; nu schrijven we het bestand
image.save_file()
# test()
def scale(pix, pix_max, float_min, float_max):
"""scale accepts
pix, the CURRENT pixel column (or row)
pix_max, the total # of pixel columns
float_min, the min floating-point value
float_max, the max floating-point value
scale returns the floating-point value
that corresponds to pix
"""
return float_min + (pix/pix_max * (float_max-float_min))
assert scale(100, 200, -2.0, 1.0) == -0.5
assert scale(100, 200, -1.5, 1.5) == 0.0
assert scale(100, 300, -2.0, 1.0) == -1.0
assert scale(25, 300, -2.0, 1.0) == -1.75
# print(scale(299, 300, -2.0, 1.0)) # == 0.99
def mset():
"""Creates a 300x200 image of the Mandelbrot set
"""
NUM_ITER = 50 # aantal updates
XMIN = -2.0 # de kleinste waarde voor de reƫle coƶrdinaat
XMAX = 1.0 # de grootste waarde voor de reƫle coƶrdinaat
YMIN = -1.0 # de kleinste waarde voor de imaginaire coƶrdinaat
YMAX = 1.0 # de grootste waarde voor de imaginaire coƶrdinaat
width = 300
height = 200
image = PNGImage(width, height)
# maak een lus om wat pixels te tekenen
for col in range(width):
for row in range(height):
# Gebruik scale twee keer:
# ƩƩn keer om het reƫle deel van c te bepalen (x)
x = scale(col, width, XMIN, XMAX)
# Ć©Ć©n keer om het imaginaire deel van c te bepalen (y)
y = scale(row, height, YMIN, YMAX)
# DAARNA ken je c toe, kies je n en test je:
c = x + y*1j
#n = 25
if in_mset(c, NUM_ITER ):
image.plot_point(col, row, (255, 175, 0) )
else:
image.plot_point(col, row, (0, 0, 0))
# we hebben door alle pixels gelust; nu schrijven we het bestand
image.save_file()
# mset()