Question: In this problem, you will write a phylogenetic tree - drawing function drawPhyloTree 3 , which resembles drawPhyloTree 2 but takes one additional input: the

In this problem, you will write a phylogenetic tree-drawing function drawPhyloTree3, which resembles drawPhyloTree2 but takes one additional input: the angle of the branches to be drawn.
drawPhyloTree3(Tree, angle, scale)
Consider the following example tree:
myTree =(5,
(3,
("A",(),()),
("B",(),())
),
("C",(),())
)
In drawing this, we have made sure that the internal nodes are the right heightthat is, horizontal distance from the leaves. The node with height 3 is drawn at a horizontal distance of 3 units from the leaves. And the root node with a height of 5 is drawn 5 units from the leaves. The thing that makes this a little tricky is that the turtle is not moving horizontally but rather at an angle. In the drawPhyloTree2 function, you did this by applying a correction factor, which assumed you were drawing with a 30-degree angle. However, now we have the problem of drawing correctly where the angle is specified by the input parameter angle. In this case, we need to use a little bit of trigonometry. We'll explain that now.
Consider our example tree above. Let's say we are at the root of this tree and wish to begin drawing. There are two subtrees at the root. The left subtree contains the leaves "A" and "B". And the right subtree is itself a leaf, labeled "C".
Let's first draw the right subtree. The root has a height of 5, whereas this leaf has a height of 0. This says that the segment from the root to the leaf labeled C should traverse 5 horizontal units. The path of the turtle can be thought of as the hypotenuse of a right triangle.
We need to know the length of the hypotenuse (see illustration). To get it, we divide the horizontal distance (5) by the cosine of the angle parameter. If you feel like you need a basic review of trig, try this.
The other subtree in our example has a height of 3. Because the root is at 5, we need to draw a segment leading to this subtree, which has a horizontal distance of 5-3=2 units. So, we now divide 2 by the cosine of the angle parameter.
In Python, we can use the math package to do trig. Note that we'll have angle in degrees, which is what the turtle needs. The math.cos function wants radians, so we convert first with math.radians. Here's the calculation for our example:
>>> import math
>>> angle =30 # angle in degrees
>>> angleInRadians = math.radians(angle)
>>>5.0/ math.cos(angleInRadians)
5.773502691896257
>>>2.0/ math.cos(angleInRadians)
2.309401076758503
So, we want to have the turtle move 5.8 units for the subtree labeled C and 2.3 units for the other.
Here's an example of the output of drawPhyloTree3 on our test dataset.
>>> drawPhyloTree3(myTree,30,20)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!