Chapter 22
Image Synthesis
The Mandelbrot set is an interesting mathematical object that owes much
of its popularity to the computer programs that create images of it.
Listing 22.1: Mandelbrot Set
1 # mandelbrot.py
2 from image import ImagePPM
3
4 def lerp(frac, low, high):
5 return low + frac
*
(high - low)
6
7 def testpoint(c, maxreps):
8 z = 0
9 reps = 0
10 while abs(z) < 2 and reps < maxreps:
11 z = z
**
2 + c
12 reps += 1
13 frac = reps / maxreps
14 return (0, 0, int(lerp(frac, 0, 255)))
15
16 def mandelbrot(xint, yint, size, maxreps):
17 width, height = size
18 img = ImagePPM.new(size)
19 for i in range(width):
20 for j in range(height):
21 a = lerp(i / width, xint[0], ...
Get A Concise Introduction to Programming in Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.