Godot 4.2.1
I have a binary tree structure, which I want it to lay out, using this algorithm.
But I’m having some issues, with the implementation. It’s said the formula for the non-root parented nodes is the following:
(π −φ/2+φi/m+φ/(2m),r)
Here is the implementation of the formula:
func _set_non_root_nodes_position(node: Node2D, max_slope: float, radius: float) -> void:
var angular_position = func(max_slope: float, child_index: int, sibling_count:int):
return PI - max_slope / 2 + max_slope * child_index / sibling_count + max_slope / (2 * sibling_count) #Calculating the angle
if node.depth > 1:
var sibling_total = _count_children(node)
if sibling_total == 0:
return
var circle_radius = radius * 0.5 # r
if node.left_child:
var angle_left = angular_position.call(max_slope, 0, sibling_total)
node.left_child.position = Vector2(circle_radius * cos(angle_left), circle_radius * sin(angle_left))
if node.right_child:
var angle_right = angular_position.call(max_slope, 1, sibling_total)
node.right_child.position = Vector2(circle_radius * cos(angle_right), circle_radius * sin(angle_right))
if node.left_child:
_set_non_root_nodes_position(node.left_child, max_slope, radius)
if node.right_child:
_set_non_root_nodes_position(node.right_child, max_slope, radius)
func set_position(node:Node2D):
var first_childs_total: int = _count_nodes(node, 2, false)
var angle_between_children: float = 2*PI/first_childs_total
_set_root_nodes_position(node, 2, angle_between_children) #Set the nodes where v -> node
global_count = 0
_set_non_root_nodes_position(node, max_slope, distance)
But the outcome of my algorithm looks like this:
While this should be the expected result: