Return two lists x, y of point coordinates of the Koch snowflake. Parameters ---------- order : int The recursion depth. scale : float The extent of the snowflake (edge length of the base triangle).
(order, scale=10)
| 17 | |
| 18 | |
| 19 | def koch_snowflake(order, scale=10): |
| 20 | """ |
| 21 | Return two lists x, y of point coordinates of the Koch snowflake. |
| 22 | |
| 23 | Parameters |
| 24 | ---------- |
| 25 | order : int |
| 26 | The recursion depth. |
| 27 | scale : float |
| 28 | The extent of the snowflake (edge length of the base triangle). |
| 29 | """ |
| 30 | def _koch_snowflake_complex(order): |
| 31 | if order == 0: |
| 32 | # initial triangle |
| 33 | angles = np.array([0, 120, 240]) + 90 |
| 34 | return scale / np.sqrt(3) * np.exp(np.deg2rad(angles) * 1j) |
| 35 | else: |
| 36 | ZR = 0.5 - 0.5j * np.sqrt(3) / 3 |
| 37 | |
| 38 | p1 = _koch_snowflake_complex(order - 1) # start points |
| 39 | p2 = np.roll(p1, shift=-1) # end points |
| 40 | dp = p2 - p1 # connection vectors |
| 41 | |
| 42 | new_points = np.empty(len(p1) * 4, dtype=np.complex128) |
| 43 | new_points[::4] = p1 |
| 44 | new_points[1::4] = p1 + dp / 3 |
| 45 | new_points[2::4] = p1 + dp * ZR |
| 46 | new_points[3::4] = p1 + dp / 3 * 2 |
| 47 | return new_points |
| 48 | |
| 49 | points = _koch_snowflake_complex(order) |
| 50 | x, y = points.real, points.imag |
| 51 | return x, y |
| 52 | |
| 53 | |
| 54 | # %% |
no test coverage detected
searching dependent graphs…