MCPcopy Create free account
hub / github.com/Vishal-raj-1/Awesome-JavaScript-Projects / tree

Function tree

assets/js/fractalTree_js/fractalTree.js:14–42  ·  view source on GitHub ↗
(x1, y1, branch, len, a)

Source from the content-addressed store, hash-verified

12
13//function to make these fractals
14const tree = (x1, y1, branch, len, a) => {
15 if (branch > 0) {
16 //making new points
17 let x2 = x1 + Math.sin(a * (Math.PI / 180)) * len;
18 let y2 = y1 - Math.cos(a * (Math.PI / 180)) * len;
19
20 //drawing lines
21 c.moveTo(x1, y1);
22 c.lineTo(x2, y2);
23 c.strokeStyle = "#fff";
24 c.lineWidth = 1;
25 c.stroke();
26
27 // draw fruit
28 if (branch == 1) {
29 c.fillStyle = "tomato"
30 c.strokeStyle = "tomato";
31 c.beginPath();
32 c.arc(x2, y2, 4, 0, 2 * Math.PI);
33 c.fill();
34 c.lineWidth = 0.1;
35 c.stroke();
36 }
37
38 //making recursions
39 tree(x2, y2, branch - 1, len * 0.67, a + angle);
40 tree(x2, y2, branch - 1, len * 0.67, a - angle);
41 }
42}
43
44//Main function
45const init = () => {

Callers 1

initFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected