Skip to content

Commit 2ec06f9

Browse files
committed
node connect using bezier curve
1 parent c3d6411 commit 2ec06f9

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
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+
"bezier",
34
"lightsalmon",
45
"treeutils"
56
]

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Process
32

43
- Understand the concept
@@ -12,24 +11,27 @@
1211
- Initialize the canvas dimensions
1312
- Calculate required width & height to draw the tree structure
1413

15-
1614
- create getRequiredHeightAndWidth function in treeUtils.js
1715
- calculate the height of canvas
18-
- calculate the width of canvas
19-
16+
- calculate the width of canvas
2017

2118
- calculate center points
2219

23-
- Create recursivelyDrawNotes function in drawBinaryTree.js
24-
- Find x & y - position for circle
25-
20+
- Create recursivelyDrawNotes function in drawBinaryTree.js
21+
- Find x & y - position for circle
2622

2723
- Create drawNode function in treeUtils.js
28-
- Create draw circle, draw border & write value in circle
24+
- Create draw circle, draw border & write value in circle
2925

30-
- recursively Left node created in drawBinaryTree.js
31-
- recursively Right node created in drawBinaryTree.js
26+
- recursively Left node created in drawBinaryTree.js
27+
- recursively Right node created in drawBinaryTree.js
3228

3329
- connectEdges function created in treeUtils.js
3430
- coordinates create for curve
35-
- Create draw curve
31+
- Create draw curve
32+
33+
- Understand algorithm behind bezier curve, convex hull
34+
- https://javascript.info/bezier-curve
35+
36+
- Calculate coordinates for bezier curve
37+
- Node connect using Bezier curve

drawBinaryTree.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,37 @@ const node3 = new BinaryTreeNode(3);
108108
root.setRight(node3);
109109

110110
const node4 = new BinaryTreeNode(4);
111-
node3.setLeft(node4);
111+
node2.setLeft(node4);
112+
113+
const node5 = new BinaryTreeNode(5);
114+
node2.setRight(node5);
115+
116+
const node6 = new BinaryTreeNode(6);
117+
node3.setLeft(node6);
112118

113119
const node7 = new BinaryTreeNode(7);
114120
node3.setRight(node7);
115121

116-
const node5 = new BinaryTreeNode(5);
117-
node2.setLeft(node5);
122+
const node8 = new BinaryTreeNode(8);
123+
node4.setLeft(node8);
118124

119-
const node6 = new BinaryTreeNode(6);
120-
node2.setRight(node6);
125+
const node9 = new BinaryTreeNode(9);
126+
node4.setRight(node9);
127+
128+
const node10 = new BinaryTreeNode(10);
129+
node5.setLeft(node10);
130+
131+
const node11 = new BinaryTreeNode(11);
132+
node5.setRight(node11);
133+
134+
const node12 = new BinaryTreeNode(12);
135+
node6.setRight(node12);
136+
137+
const node13 = new BinaryTreeNode(13);
138+
node7.setLeft(node13);
139+
140+
const node14 = new BinaryTreeNode(14);
141+
node9.setLeft(node14);
121142

122143
console.log(root);
123144

treeutils.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ export function connectEdges(canvasElement, xCoordinates, yCoordinates) {
4848
const { xStart, xEnd } = xCoordinates;
4949
const { yStart, yEnd } = yCoordinates;
5050

51+
const xHalf = (xStart + xEnd) / 2;
52+
const yHalf = (yStart + yEnd) / 2;
53+
5154
const start = { x: xStart, y: yStart };
55+
const cPoint1 = { x: xHalf, y: yHalf };
56+
const cPoint2 = { x: xEnd, y: yHalf };
5257
const end = { x: xEnd, y: yEnd };
5358

5459
// draw curve
@@ -57,6 +62,16 @@ export function connectEdges(canvasElement, xCoordinates, yCoordinates) {
5762
context.beginPath();
5863
context.strokeStyle = "brown";
5964
context.moveTo(start.x, start.y);
60-
context.lineTo(end.x, end.y);
65+
66+
context.bezierCurveTo(
67+
cPoint1.x,
68+
cPoint1.y,
69+
cPoint2.x,
70+
cPoint2.y,
71+
end.x,
72+
end.y
73+
);
74+
// context.lineTo(end.x, end.y);
75+
6176
context.stroke();
6277
}

0 commit comments

Comments
 (0)