-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek8.py
More file actions
172 lines (134 loc) · 5.59 KB
/
Copy pathweek8.py
File metadata and controls
172 lines (134 loc) · 5.59 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import copy
import math
import numpy as np
from PIL import Image, ImageDraw
from Geometry import Polygon, Point, PolygonGroup
size = 1000
image = Image.new("RGB", (size, size), color="white")
draw = ImageDraw.Draw(image)
def rotation(angle):
cos = math.cos(math.radians(-angle))
sin = math.sin(math.radians(-angle))
matrix = np.matrix([[cos, -sin, 0],
[sin, cos, 0],
[0, 0, 1]])
return matrix
def scaling(x, y):
matrix = np.matrix([[x, 0, 0],
[0, y, 0],
[0, 0, 1]])
return matrix
def translation(x, y):
matrix = np.matrix([[1, 0, x],
[0, 1, y],
[0, 0, 1]])
return matrix
def shear(k):
matrix = np.matrix([[1, k, 0],
[0, 1, 0],
[0, 0, 1]])
return matrix
def combine(transformations):
result = transformations[0]
for i in range(1, len(transformations)):
result = np.dot(result, transformations[i])
return result
def square(size):
square1 = Polygon(
[Point(-size, size), Point(size, size), Point(size, -size), Point(-size, -size), Point(-size, size)])
return square1
def squares():
polygon = square(20)
transformations = combine([rotation(20), scaling(1.1, 1.1), translation(5, 10)])
polygon.repeatTransformation(10, transformations, image, size)
image.save("images8\\squares.png")
def tail():
polygon = square(50)
transformations = combine([shear(1.3), rotation(10), scaling(0.9, 0.9), translation(-50, 50)])
polygon.repeatTransformation(25, transformations, image, size)
image.save("images8\\tail.png")
def fan():
polygon = square(200)
transformations = combine(rotation(10), scaling(1.1, 0.8))
polygon.repeatTransformation(50, transformations, image, size)
image.save("images8\\fan.png")
def mergeGroups(groups):
merged = PolygonGroup()
for group in groups:
merged.polygons.extend(group.polygons)
return merged
def mrcm(n, polygon, transformations):
parents = PolygonGroup([polygon])
for i in range(n):
children = []
for transformation in transformations:
newGroup = copy.deepcopy(parents)
newGroup.applyTransformation(transformation)
children.append(newGroup)
parents = mergeGroups(children)
for i in range(0, len(parents.polygons)):
# nutne preskalovanie pre transformacie zadane maticou
# parents.polygons[i].applyTransformation(scaling(size, size))
# parents.polygons[i].applyTransformation(translation(-size // 2, -size // 2))
parents.polygons[i].draw(image, size)
return parents
def sierpinskyTriangle(size, angle):
transformations = [combine([translation(0, size // 2), scaling(0.5, 0.5)]),
combine([rotation(angle), translation(0, size // 2), scaling(0.5, 0.5)]),
combine([rotation(-angle), translation(0, size // 2), scaling(0.5, 0.5)])]
mrcm(7, square(size), transformations)
image.save("images8\\sierpinskyTriangle" + str(angle) + ".png")
def star():
transformations = [np.matrix([[0.255, 0.0, 0.3726],
[0.0, 0.255, 0.6714],
[0, 0, 1]]),
np.matrix([[0.255, 0.0, 0.1146],
[0.0, 0.255, 0.2232],
[0, 0, 1]]),
np.matrix([[0.255, 0.0, 0.6306],
[0.0, 0.255, 0.2232],
[0, 0, 1]]),
np.matrix([[0.370, -0.642, 0.6356],
[0.642, 0.370, -0.0061],
[0, 0, 1]])]
mrcm(8, square(1), transformations)
image.save("images8\\star.png")
def fern():
transformations = [np.matrix([[0.849, 0.037, 0.075],
[-0.037, 0.849, 0.183],
[0, 0, 1]]),
np.matrix([[0.197, -0.226, 0.4],
[0.226, 0.197, 0.049],
[0, 0, 1]]),
np.matrix([[-0.15, 0.283, 0.575],
[0.26, 0.237, 0.084],
[0, 0, 1]]),
np.matrix([[0, 0, 0.5],
[0, 0.16, 0],
[0, 0, 1]])]
mrcm(9, square(1), transformations)
image.save("images8\\fern.png")
def orionStar(size):
line = Polygon([Point(size // 2, size), Point(size // 2, size // 2)])
transformations = [combine([scaling(0.5, 2)]),
combine([rotation(90), translation(0, size // 2), scaling(0.5, 2)]),
combine([rotation(-90), translation(0, -size // 2), scaling(0.5, 2)]),
combine([rotation(180), translation(0, -size // 2), scaling(0.5, 2)])]
mrcm(8, line, transformations)
image.save("images8\\orionStar.png")
def fractal(size):
transformations = [combine([rotation(45), translation(size, size), scaling(0.5, 0.5)]),
combine([rotation(-45), translation(size, size), scaling(0.5, 0.5)]),
combine([rotation(135), translation(size, size), scaling(0.5, 0.5)]),
combine([rotation(-135), translation(size, size), scaling(0.5, 0.5)])]
mrcm(8, square(size), transformations)
image.save("images8\\fractal.png")
# squares()
# tail()
# fan()
# sierpinskyTriangle(500, 150)
# star()
# fern()
# orionStar(50)
fractal(200)
image.show()