-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek6.py
More file actions
104 lines (86 loc) · 3.13 KB
/
Copy pathweek6.py
File metadata and controls
104 lines (86 loc) · 3.13 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import random
from math import cos, pi, sin
from PIL import Image
from Geometry import *
from Lsystem import Lsystem
size = 500
image = Image.new("RGB", (size, size), color="white")
draw = ImageDraw.Draw(image)
def chaosGame(n, r, iterations):
points = []
radius = size // 2 - 10
# https://stackoverflow.com/questions/876853/generating-color-ranges-in-python
colors = []
for i in range(n):
h = '%06X' % random.randint(0, 0xFFFFFF)
colors.append(tuple(int(h[i:i + 2], 16) for i in (0, 2, 4)))
for i in range(n):
x = size // 2 + radius * cos(2 * pi * i / n)
y = size // 2 + radius * sin(2 * pi * i / n)
points.append(Point(x, y))
x = random.randint(0, size - 10)
y = random.randint(0, size - 10)
point = Point(x, y)
indefOfPrevousChosenPoint = 1
for i in range(iterations):
indexOfChosenPoint = random.randint(0, n - 1)
while (indefOfPrevousChosenPoint == indexOfChosenPoint):
# while(abs(indefOfPrevousChosenPoint - indexOfChosenPoint) == 1 or abs(indefOfPrevousChosenPoint - indexOfChosenPoint) == 4 ):
indexOfChosenPoint = random.randint(0, n - 1)
chosenOne = points[indexOfChosenPoint]
point.x = int((chosenOne.x + point.x) * r)
point.y = int((chosenOne.y + point.y) * r)
if i > 100:
image.putpixel((point.x, point.y), colors[indexOfChosenPoint])
indefOfPrevousChosenPoint = indexOfChosenPoint
image.save("images6\\chaosGamePenta" + str(n) + ".png")
def feigenbaum(x0, zoom=((2.4, 4), (0, 1))):
zoomR = zoom[0][1] - zoom[0][0]
zoomX = zoom[1][1] - zoom[1][0]
for i in range(size):
r = zoom[0][0] + i * zoomR / size
for x in calculate(x0, r):
j = x / (zoomX / size)
image.putpixel((int(i), int(j)), (0, 0, 0))
image.save("images6\\feigenbaum.png")
def calculate(x, r):
result = []
xn = x
for i in range(500):
xn = r * xn * (1 - xn)
if i >= 100:
result.append(xn)
return result
koch = Lsystem("F--F--F", {"F": "F+F--F+F"},
{"F": "forward(3)",
"+": "right(60)",
"-": "left(60)"})
tree = Lsystem("A", {"A": "F[+A]-A",
"F": "FF"},
{"A": "forward(5)",
"F": "forward(5)",
"+": "right(45)",
"[": "push()",
"]": "pop()",
"-": "left(45)"})
branch = Lsystem("A", {"A": "F-[[A]+A]+F[+FA]-A",
"F": "FF"},
{"A": "forward(1)",
"F": "forward(1)",
"+": "right(25)",
"[": "push()",
"]": "pop()",
"-": "left(25)"})
krishna = Lsystem("-X--X", {"X": "XFX--XFX"},
{"F": "forward(4)",
"-": "left(45)"})
root = Lsystem("F", {"F": "―FF[+F][-F]"},
{"F": "forward(1)",
"+": "right(45)",
"[": "push()",
"]": "pop()",
"-": "left(45)"})
chaosGame(5, 1 / 2, 1000000)
# root.draw(7, "images6\\root")
# feigenbaum(1/2)
image.show()