fractals.koch_snowflake ======================= .. py:module:: fractals.koch_snowflake .. autoapi-nested-parse:: 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 ---------- .. autoapisummary:: 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 Functions --------- .. autoapisummary:: fractals.koch_snowflake.iterate fractals.koch_snowflake.iteration_step fractals.koch_snowflake.plot fractals.koch_snowflake.rotate Module Contents --------------- .. py:function:: 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])] .. py:function:: 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])] .. py:function:: 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 .. py:function:: 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]) .. py:data:: INITIAL_VECTORS .. py:data:: VECTOR_1 .. py:data:: VECTOR_2 .. py:data:: VECTOR_3 .. py:data:: processed_vectors