MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / getKochSnowflake

Function getKochSnowflake

Recursive/KochSnowflake.manual-test.js:10–30  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

8 * @returns The canvas of the rendered Koch snowflake.
9 */
10function 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.

Callers 1

Calls 3

iterateFunction · 0.90
drawToCanvasFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected