fractals.koch_snowflake

Description

The Koch snowflake is a fractal curve and one of the earliest fractals to have been described. The Koch snowflake can be built up iteratively, in a sequence of stages. The first stage is an equilateral triangle, and each successive stage is formed by adding outward bends to each side of the previous stage, making smaller equilateral triangles. This can be achieved through the following steps for each line:

  1. divide the line segment into three segments of equal length.

2. draw an equilateral triangle that has the middle segment from step 1 as its base and points outward. 3. remove the line segment that is the base of the triangle from step 2.

(description adapted from https://en.wikipedia.org/wiki/Koch_snowflake ) (for a more detailed explanation and an implementation in the Processing language, see https://natureofcode.com/book/chapter-8-fractals/ #84-the-koch-curve-and-the-arraylist-technique )

Requirements (pip):
  • matplotlib

  • numpy

Attributes

INITIAL_VECTORS

VECTOR_1

VECTOR_2

VECTOR_3

processed_vectors

Functions

iterate(→ list[numpy.ndarray])

Go through the number of iterations determined by the argument "steps".

iteration_step(→ list[numpy.ndarray])

Loops through each pair of adjacent vectors. Each line between two adjacent

plot(→ None)

Utility function to plot the vectors using matplotlib.pyplot

rotate(→ numpy.ndarray)

Standard rotation of a 2D vector with a rotation matrix

Module Contents

fractals.koch_snowflake.iterate(initial_vectors: list[numpy.ndarray], steps: int) list[numpy.ndarray]

Go through the number of iterations determined by the argument “steps”. Be careful with high values (above 5) since the time to calculate increases exponentially. >>> iterate([np.array([0, 0]), np.array([1, 0])], 1) [array([0, 0]), array([0.33333333, 0. ]), array([0.5 , 0.28867513]), array([0.66666667, 0. ]), array([1, 0])]

fractals.koch_snowflake.iteration_step(vectors: list[numpy.ndarray]) list[numpy.ndarray]

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. >>> iteration_step([np.array([0, 0]), np.array([1, 0])]) [array([0, 0]), array([0.33333333, 0. ]), array([0.5 , 0.28867513]), array([0.66666667, 0. ]), array([1, 0])]

fractals.koch_snowflake.plot(vectors: list[numpy.ndarray]) None

Utility function to plot the vectors using matplotlib.pyplot No doctest was implemented since this function does not have a return value

fractals.koch_snowflake.rotate(vector: numpy.ndarray, angle_in_degrees: float) numpy.ndarray

Standard rotation of a 2D vector with a rotation matrix (see https://en.wikipedia.org/wiki/Rotation_matrix ) >>> rotate(np.array([1, 0]), 60) array([0.5 , 0.8660254]) >>> rotate(np.array([1, 0]), 90) array([6.123234e-17, 1.000000e+00])

fractals.koch_snowflake.INITIAL_VECTORS
fractals.koch_snowflake.VECTOR_1
fractals.koch_snowflake.VECTOR_2
fractals.koch_snowflake.VECTOR_3
fractals.koch_snowflake.processed_vectors