* Method to render the Koch snowflake to a canvas. * * @param canvasWidth The width of the canvas. * @param steps The number of iterations. * @returns The canvas of the rendered Koch snowflake.
(canvasWidth = 600, steps = 5)
| 8 | * @returns The canvas of the rendered Koch snowflake. |
| 9 | */ |
| 10 | function getKochSnowflake(canvasWidth = 600, steps = 5) { |
| 11 | if (canvasWidth <= 0) { |
| 12 | throw new Error('canvasWidth should be greater than zero') |
| 13 | } |
| 14 | |
| 15 | const offsetX = canvasWidth / 10.0 |
| 16 | const offsetY = canvasWidth / 3.7 |
| 17 | const vector1 = new Vector2(offsetX, offsetY) |
| 18 | const vector2 = new Vector2( |
| 19 | canvasWidth / 2, |
| 20 | Math.sin(Math.PI / 3) * canvasWidth * 0.8 + offsetY |
| 21 | ) |
| 22 | const vector3 = new Vector2(canvasWidth - offsetX, offsetY) |
| 23 | const initialVectors = [] |
| 24 | initialVectors.push(vector1) |
| 25 | initialVectors.push(vector2) |
| 26 | initialVectors.push(vector3) |
| 27 | initialVectors.push(vector1) |
| 28 | const vectors = iterate(initialVectors, steps) |
| 29 | return drawToCanvas(vectors, canvasWidth, canvasWidth) |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Utility-method to render the Koch snowflake to a canvas. |
no test coverage detected