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 treedrawing function drawPhyloTree which resembles drawPhyloTree but takes one additional input: the angle of the branches to be drawn.
drawPhyloTreeTree angle, scale
Consider the following example tree:
myTree
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 is drawn at a horizontal distance of units from the leaves. And the root node with a height of is drawn 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 drawPhyloTree function, you did this by applying a correction factor, which assumed you were drawing with a 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 whereas this leaf has a height of This says that the segment from the root to the leaf labeled C should traverse 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 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 Because the root is at we need to draw a segment leading to this subtree, which has a horizontal distance of units. So we now divide 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 # angle in degrees
angleInRadians math.radiansangle
math.cosangleInRadians
math.cosangleInRadians
So we want to have the turtle move units for the subtree labeled C and units for the other.
Here's an example of the output of drawPhyloTree on our test dataset.
drawPhyloTreemyTree
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
