Equilateral Area
Create a Python program that defines the function equilateral_area(edge_length: float) -> float
that computes the area of an equilateral triangle. The parameter edge_length specifies the length of the triangle's edges. The function should return the area of the triangle. The area of an equilateral triangle and be computed with the equation:
\[a = \frac{\sqrt{3}}{4} \cdot l ^ 2\]
Where a is the triangle area, and l is the length of an edge of the triangle.
Test Cases
import test
def equilateral_area(edge_length: float) -> float:
# Put your code here.
def main() -> None:
1.0), 0.4330127018922193)
test.equal(equilateral_area(# Put more test cases here.
return None
main()
Sierpinski
Create a Python program that defines the function sierpinski_area(edge_length: float) -> float
that computes the area of a Sierpinski triangle of order one. A Sierpinski Triangle of order one is an equilateral triangle with a triangular hole. The vertices of the hole triangle are the midpoints of the Sierpinski triangle's edges. For example:
Copy and paste your solution to the previous exercise into this one and use it to write the sierpinski_area
function.
Test Cases
import test
def equilateral_area(edge_length: float) -> float:
# Put your code here.
def sierpinski_area(edge_length: float) -> float:
# Put your code that calls equilateral_area here.
def main() -> None:
# Test equilateral
1.0), 0.4330127018922193)
test.equal(equilateral_area(# put more test cases here.
# Test sierpinski
1.0), 0.3247595264191645)
test.equal(sierpinski_area(# Put more test cases here.
return None
main()
Trees
When working with functions, abstraction is fundamental. You will soon find yourself in a position where you start thinking about problems in functions. Breaking a complex problem into smaller and simpler problems makes your code a lot easier to read and write. Intricate drawings are often made of more simple designs and are a great example of functional thinking.
Details
Create a Python program that defines the function draw_tree(x: float, y: float) -> None
that uses the turtle module to draw a tree. The x and y location is the center of the triangle’s base that makes up the tree’s canopy. The tree should look like the tree in the example below.
Notice that a tree is defined by a brown square and a filled green triangle. To simplify the draw_tree
function, also define the function draw_triangle(x: float, y: float) -> None
, which draws a filled, green isosceles triangle. The triangle can be any height, but the base length should be the same as the height.
Don’t rewrite code! Functions make it easier to reuse code. Copy the fill_square
function from the Pixel Art activity to make drawing the tree even easier.
Example
Hint
Drawing the trunk of the tree is straightforward. Just draw a filled square.
The tree's canopy triangle fits inside a square since its width is the same as its height.
Drawing the tree's canopy is easier if you use the
turtle.setposition
function.The lower-left and lower-right corners of the triangle are the same as the lower-left and lower-right corners of the square.
The upper corner of the triangle is the midpoint between the upper-left and upper-right corners of the square. The x-coordinate is the same as the base point, and the y-coordinate is the same as the square’s upper points.
Challenge
Modify your program so that you can specify the size of the tree. We will define the size of the tree to be the width of the tree. Use this new function to draw yourself a forest of trees where the farther away trees are smaller.