-
Notifications
You must be signed in to change notification settings - Fork 5
/
fractal.py
71 lines (49 loc) · 1.47 KB
/
fractal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from plotcomplex.plot import gridToImage
def squarePlusOne(z):
return z*z + 1
def squareMinusOne(z):
return z*z - 1
def squarePlusC(c):
def f(z):
return z*z + c
return f
def isSmallForever(z, f):
k = 0
while abs(z) < 2:
z = f(z)
k += 1
if k > 250:
return True
return False
def frange(start, stop, step):
while start < stop:
yield start
start += step
def classify(classifier, xRange=(-2,2), yRange=(-2,2), step=0.1):
xStart, xEnd = xRange
yStart, yEnd = yRange
xSize = int((xEnd - xStart) / step)
ySize = int((yEnd - yStart) / step)
grid = [[0 for j in range(xSize)] for i in range(ySize)]
for i in range(ySize):
b = yEnd - i * step
for j in range(xSize):
a = xStart + j * step
z = a + b * 1j
if classifier(z):
grid[i][j] = 1
return grid
if __name__ == "__main__":
def classifySquarePlusOne(z):
return isSmallForever(z, squarePlusOne)
def classifySquareMinusOne(z):
return isSmallForever(z, squareMinusOne)
#grid = classify(classifySquarePlusOne)
#gridToImage(grid)
#grid = classify(classifySquareMinusOne, step=0.001)
#gridToImage(grid)
def classifySquarePlusC(c):
return isSmallForever(0, squarePlusC(c))
grid = classify(classifySquarePlusC, xRange=(-2,1), yRange=(-1,1), step=0.001)
#grid = classify(classifySquarePlusC, xRange=(0.1,0.2), yRange=(0.55,0.7), step=0.0001)
gridToImage(grid)