MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / iteration_step

Function iteration_step

fractals/koch_snowflake.py:53–74  ·  view source on GitHub ↗

Loops through each pair of adjacent vectors. Each line between two adjacent vectors is divided into 4 segments by adding 3 additional vectors in-between the original two vectors. The vector in the middle is constructed through a 60 degree rotation so it is bent outwards. >>> ite

(vectors: list[np.ndarray])

Source from the content-addressed store, hash-verified

51
52
53def iteration_step(vectors: list[np.ndarray]) -> list[np.ndarray]:
54 """
55 Loops through each pair of adjacent vectors. Each line between two adjacent
56 vectors is divided into 4 segments by adding 3 additional vectors in-between
57 the original two vectors. The vector in the middle is constructed through a
58 60 degree rotation so it is bent outwards.
59 >>> iteration_step([np.array([0, 0]), np.array([1, 0])])
60 [array([0, 0]), array([0.33333333, 0. ]), array([0.5 , \
610.28867513]), array([0.66666667, 0. ]), array([1, 0])]
62 """
63 new_vectors = []
64 for i, start_vector in enumerate(vectors[:-1]):
65 end_vector = vectors[i + 1]
66 new_vectors.append(start_vector)
67 difference_vector = end_vector - start_vector
68 new_vectors.append(start_vector + difference_vector / 3)
69 new_vectors.append(
70 start_vector + difference_vector / 3 + rotate(difference_vector / 3, 60)
71 )
72 new_vectors.append(start_vector + difference_vector * 2 / 3)
73 new_vectors.append(vectors[-1])
74 return new_vectors
75
76
77def rotate(vector: np.ndarray, angle_in_degrees: float) -> np.ndarray:

Callers 1

iterateFunction · 0.85

Calls 2

rotateFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected