问题
I've found this answer that is coded in C# to plot nodes in a radial tree and gained a lot of information. The problem is that I am having a hard time reading C# and can't exactly figure out the entirety of what's going on. The goal is to try and render a radial tree like the image below, into a game and dynamically show and hide the points based on progress. The majority of radial trees I see use networkx
or matplotlib
, but I don't want to bloat the game with packages when I only need to plot them using the game engines x and y positions.I then also tried to look at the algorithm frome the referenced answer and got even more info but not everything seems clear to me as I don't know all the math talk in terms of python.
My tree is based on anytree and printed out, my nodes come out like so in a list, each one being a Node object with data inside.
for pre, fill, node in RenderTree(Node('1')):
if Depth > 0:
list_of_p_c.append((node.name, node.data['Parent']))
else:
list_of_p_c.append((1, 0))
Depth += 1
print list_of_p_c
>> [(1, 0), ("'1.1'", '1'), ("'1.2'", '1'), ("'1.2.1'", '1.2'), ("'1.2.2'", '1.2'), ("'1.2.3'", '1.2'),
("'1.2.3.1'", '1.2.3'), ("'1.2.3.2'", '1.2.3'), ("'1.2.3.2.1'", '1.2.3.2'), ("'1.2.3.2.2'", '1.2.3.2'),
("'1.3'", '1')]
---------------------------------------------------
EDIT 2
def RadialPositions(node, alfa=0, beta=2*math.pi, delta=0, RadialPoints=RadialPoint):
if node.is_root:
# (x,y)
node.positions = (0,0)
print "This Node is root, positions are {} ".format(node.positions)
def addpoint():
x = 0
y = 0
ParentPoint = None
depthOfNode = node.depth
theta = alfa
radius = math.pi + (delta * depthOfNode)
number_chuldrend_of_node = len(node.children)
# print "There are " + str(number_chuldrend_of_node) + " children " + "radius is " + "%s and depth is %s" % (radius, depthOfNode)
for child_node in node.children:
print "Children: %s" % len(child_node.children)
leaves = len(child_node.leaves)
def convert_theta(x):
print "Beta is %s" % beta, "Alpha is %s" % alfa
return x * (beta - alfa)
mi = theta + convert_theta(leaves)
x = radius * math.cos((theta + mi) / 2.0)
y = radius * math.sin((theta + mi) / 2.0)
child_node.positions = (x, y)
print child_node
if (len(child_node.children) > 0):
child_node.positions = (x, y)
RadialPositions(child_node, theta, mi)
theta = mi
I have been successful in giving each node a position and then recursively giving the children a point as well. Currently though the x and y postions are in the correct depth, but they also come out in a straight line. I have put the results below. When up to the depth of 4, the line also seems to put some points on the opposite side. I'm most likely missing something at this point but don't know what it is. The translation from C# to Python seem to have worked well but I don't understand some points of the previous answer.
来源:https://stackoverflow.com/questions/60394111/how-to-plot-images-as-a-radial-tree-only-using-math