-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathkoch.py
69 lines (52 loc) · 2.1 KB
/
koch.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
# Koch snowflake adalah kurva fraktal dan salah satu fraktal paling awal yang
# telah dijelaskan. Kepingan salju Koch dapat dibangun secara berulang, dalam
# urutan tahapan. Tahap pertama adalah segitiga sama sisi, dan masing-masing
# tahap berturut-turut dibentuk dengan menambahkan tikungan ke luar ke setiap sisi
# tahap sebelumnya, membuat segitiga sama sisi yang lebih kecil.
from __future__ import annotations
import matplotlib.pyplot as plt # type: ignore
import numpy as np
VECTOR_1 = np.array([0, 0])
VECTOR_2 = np.array([0.5, 0.8660254])
VECTOR_3 = np.array([1, 0])
INITIAL_VECTORS = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1]
def iterate(initial_error: list[np.ndarray], steps: int) -> list[np.ndarray]:
vectors = initial_error
for _ in range(steps):
vectors = iteration_step(vectors)
return vectors
def iteration_step(vectors: list[np.ndarray]) -> list[np.ndarray]:
new_vectors = []
for i, start_vector in enumerate(vectors[:-1]):
end_vector = vectors[i + 1]
new_vectors.append(start_vector)
difference_vector = end_vector - start_vector
new_vectors.append(
start_vector + difference_vector / 3 + rotate(difference_vector / 3, 60)
)
new_vectors.append(start_vector + difference_vector * 2 / 3)
new_vectors.append(vectors[-1])
return new_vectors
def rotate(vector: np.ndarray, angle_in_degrees: float) -> np.ndarray:
"""
>>> import numpy
>>> rotate(numpy.array([1, 0]), 60)
array([0.5 , 0.8660254])
>>> rotate(numpy.array([1, 0]), 90)
array([6.123234e-17, 1.000000e+00])
"""
theta = np.radians(angle_in_degrees)
c, s = np.cos(theta), np.sin(theta)
rotation_matrix = np.array(((c, -s), (s, c)))
return np.dot(rotation_matrix, vector)
def plot(vectors: list[np.ndarray]) -> None:
axes = plt.gca()
axes.set_aspect("equal")
x_coordinates, y_coordinates = zip(*vectors)
plt.plot(x_coordinates, y_coordinates)
plt.show()
if __name__ == "__main__":
import doctest
doctest.testmod()
processed_vector = iterate(INITIAL_VECTORS, 5)
plot(processed_vector)