Skip to content

Commit c3d6411

Browse files
committed
measure realtime coordinates & implement node rendering
1 parent ffb10ff commit c3d6411

File tree

4 files changed

+193
-36
lines changed

4 files changed

+193
-36
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"cSpell.words": [
3+
"lightsalmon",
34
"treeutils"
45
]
56
}

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
# Process
3+
4+
- Understand the concept
5+
- create binary node tree properties in BinaryTreeNode.js
6+
- Set default_config in treeUtils.js
7+
- Create drawBinaryTree.js
8+
9+
- Initialize canvas via query selector
10+
- creation function drawBinaryTree & pass root canvasElement as argument
11+
- Find maxWidth & maxHeight for window
12+
- Initialize the canvas dimensions
13+
- Calculate required width & height to draw the tree structure
14+
15+
16+
- create getRequiredHeightAndWidth function in treeUtils.js
17+
- calculate the height of canvas
18+
- calculate the width of canvas
19+
20+
21+
- calculate center points
22+
23+
- Create recursivelyDrawNotes function in drawBinaryTree.js
24+
- Find x & y - position for circle
25+
26+
27+
- Create drawNode function in treeUtils.js
28+
- Create draw circle, draw border & write value in circle
29+
30+
- recursively Left node created in drawBinaryTree.js
31+
- recursively Right node created in drawBinaryTree.js
32+
33+
- connectEdges function created in treeUtils.js
34+
- coordinates create for curve
35+
- Create draw curve

drawBinaryTree.js

Lines changed: 102 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,124 @@
1-
import { BinaryTreeNode } from "./BinaryTreeNode.js"
2-
import { getRequiredHeightAndWidth } from "./treeutils.js";
3-
1+
import { BinaryTreeNode } from "./BinaryTreeNode.js";
2+
import {
3+
DEFAULT_CONFIG,
4+
connectEdges,
5+
drawNode,
6+
getRequiredHeightAndWidth,
7+
} from "./treeutils.js";
48

59
const canvas = document.querySelector("canvas");
610

711
function drawBinaryTree(root, canvasElement) {
8-
9-
const maxWidth = window.innerWidth; // taking max width of window
10-
const maxHeight = window.innerHeight; // taking max height of window
12+
const maxWidth = window.innerWidth; // taking max width of window
13+
const maxHeight = window.innerHeight; // taking max height of window
14+
15+
// Initialize the canvas dimensions
16+
canvasElement.width = maxWidth;
17+
canvasElement.height = maxHeight;
18+
19+
// calculate required width & height to draw the tree structure
20+
const { requiredCanvasHeight, requiredCanvasWidth } =
21+
getRequiredHeightAndWidth(root);
1122

12-
// Initialize the canvas dimensions
13-
canvasElement.width = maxWidth;
14-
canvasElement.height = maxHeight;
23+
// calculate center Points
1524

16-
// calculate required width & height to draw the tree structure
17-
const { requiredCanvasHeight, requiredCanvasWidth } =
18-
getRequiredHeightAndWidth(root);
25+
const windowWidthCenter = maxWidth / 2;
26+
const requiredWidthCenter = requiredCanvasWidth / 2;
1927

28+
const xStart = windowWidthCenter - requiredWidthCenter;
29+
const xEnd = windowWidthCenter + requiredWidthCenter;
2030

21-
// calculate center Points
22-
23-
const windowWidthCenter = maxWidth / 2;
24-
const requiredWidthCenter = requiredCanvasWidth / 2;
31+
const horizontalConfig = { xStart, xEnd };
2532

26-
const xStart = windowWidthCenter - requiredWidthCenter;
27-
const xEnd = windowWidthCenter + requiredWidthCenter;
33+
// Draw
2834

29-
const horizontalConfig = { xStart, xEnd }
30-
31-
// Draw
35+
recursivelyDrawNotes(root, canvasElement, 0.5, horizontalConfig);
3236
}
3337

38+
function recursivelyDrawNotes(
39+
root,
40+
canvasElement,
41+
currentLine,
42+
horizontalConfig
43+
) {
44+
const { xStart, xEnd } = horizontalConfig;
45+
46+
const xPos = (xStart + xEnd) / 2; // Find x-position for circle
47+
const yPos = currentLine * DEFAULT_CONFIG.nodeHeightSpacing; // Find y-position for circle
48+
49+
drawNode(root.value, canvasElement, xPos, yPos); // draw the circle
50+
51+
if (root.left !== null) {
52+
// recursively Left node created
53+
const leftNodeHorizontalConfig = { xStart, xEnd: xPos };
54+
recursivelyDrawNotes(
55+
root.left,
56+
canvasElement,
57+
currentLine + 1,
58+
leftNodeHorizontalConfig
59+
);
60+
61+
connectEdges(
62+
canvasElement,
63+
{
64+
xStart: xPos,
65+
xEnd: (xStart + xPos) / 2,
66+
},
67+
{
68+
yStart: yPos + DEFAULT_CONFIG.radius,
69+
yEnd:
70+
(currentLine + 1) * DEFAULT_CONFIG.nodeHeightSpacing -
71+
DEFAULT_CONFIG.radius,
72+
}
73+
);
74+
}
75+
76+
if (root.right !== null) {
77+
// recursively right node created
78+
const rightNodeHorizontalConfig = { xStart: xPos, xEnd };
79+
recursivelyDrawNotes(
80+
root.right,
81+
canvasElement,
82+
currentLine + 1,
83+
rightNodeHorizontalConfig
84+
);
85+
86+
connectEdges(
87+
canvasElement,
88+
{
89+
xStart: xPos,
90+
xEnd: (xPos + xEnd) / 2,
91+
},
92+
{
93+
yStart: yPos + DEFAULT_CONFIG.radius,
94+
yEnd:
95+
(currentLine + 1) * DEFAULT_CONFIG.nodeHeightSpacing -
96+
DEFAULT_CONFIG.radius,
97+
}
98+
);
99+
}
100+
}
34101

35-
const root = new BinaryTreeNode(1); // creating first node
102+
const root = new BinaryTreeNode(1); // creating first node
36103

37104
const node2 = new BinaryTreeNode(2);
38105
root.setLeft(node2);
39106

40107
const node3 = new BinaryTreeNode(3);
41108
root.setRight(node3);
42109

43-
console.log(root);
110+
const node4 = new BinaryTreeNode(4);
111+
node3.setLeft(node4);
44112

113+
const node7 = new BinaryTreeNode(7);
114+
node3.setRight(node7);
115+
116+
const node5 = new BinaryTreeNode(5);
117+
node2.setLeft(node5);
118+
119+
const node6 = new BinaryTreeNode(6);
120+
node2.setRight(node6);
121+
122+
console.log(root);
45123

46-
drawBinaryTree(root, canvas)
124+
drawBinaryTree(root, canvas);

treeutils.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,62 @@
11
export const DEFAULT_CONFIG = {
2-
radius: 20,
3-
nodeWidthSpacing: 25, // width spacing
4-
nodeHeightSpacing: 100, // line height
5-
nodeFontSize: 10 // font size
6-
}
2+
radius: 20,
3+
nodeWidthSpacing: 25, // width spacing
4+
nodeHeightSpacing: 100, // line height
5+
nodeFontSize: 10, // font size
6+
};
77

88
export function getRequiredHeightAndWidth(root) {
99
const heightOfTree = root.getHeight(); // calculate the height of canvas
1010
const maxLeafNodes = Math.pow(2, heightOfTree); // calculate the width of canvas
1111

1212
const requiredCanvasHeight = heightOfTree * DEFAULT_CONFIG.nodeHeightSpacing; // calculate the height of canvas
13-
const requiredCanvasWidth = maxLeafNodes * DEFAULT_CONFIG.nodeWidthSpacing; // calculate the width of canvas
14-
15-
return {
16-
requiredCanvasHeight,
17-
requiredCanvasWidth
18-
}
19-
}
13+
const requiredCanvasWidth = maxLeafNodes * DEFAULT_CONFIG.nodeWidthSpacing; // calculate the width of canvas
14+
15+
return {
16+
requiredCanvasHeight,
17+
requiredCanvasWidth,
18+
};
19+
}
20+
21+
export function drawNode(value, canvasElement, x, y) {
22+
const context = canvasElement.getContext("2d"); // tool to draw
23+
24+
// draw circle
25+
26+
context.beginPath();
27+
context.arc(x, y, DEFAULT_CONFIG.radius, 0, 2 * Math.PI, false);
28+
context.fillStyle = "lightsalmon";
29+
context.fill();
30+
31+
// draw circle border
32+
33+
context.beginPath();
34+
context.arc(x, y, DEFAULT_CONFIG.radius, 0, 2 * Math.PI, false);
35+
context.strokeStyle = "brown";
36+
context.stroke();
37+
38+
// write value
39+
40+
context.font = `${DEFAULT_CONFIG.nodeFontSize}pt serif`;
41+
context.fillStyle = "brown";
42+
context.textAlign = "center";
43+
context.fillText(value, x, y + DEFAULT_CONFIG.nodeFontSize / 2);
44+
}
45+
46+
// Connecting edges of nodes
47+
export function connectEdges(canvasElement, xCoordinates, yCoordinates) {
48+
const { xStart, xEnd } = xCoordinates;
49+
const { yStart, yEnd } = yCoordinates;
50+
51+
const start = { x: xStart, y: yStart };
52+
const end = { x: xEnd, y: yEnd };
53+
54+
// draw curve
55+
56+
const context = canvasElement.getContext("2d");
57+
context.beginPath();
58+
context.strokeStyle = "brown";
59+
context.moveTo(start.x, start.y);
60+
context.lineTo(end.x, end.y);
61+
context.stroke();
62+
}

0 commit comments

Comments
 (0)