Processing math: 100%

CPSC150A
Scientific Computing

Activity 14

Function Scope

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=34×l2

Where a is the area of the triangle and l is the length of an edge of the triangle.

Test Cases

Create your own! Use this template:

import test

def equilateral_area(edge_length: float) -> float:
    # Put your code here

def main() -> None:
    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 mid points 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

Create your own! Use this template:

import test

def sierpinski_area(edge_length: float) -> float:
    # Put your code here

def main() -> None:
    test.equal(sierpinski_area(?), ?)
    return None

main()

Trees

When working with functions, abstraction is key. You will soon find yourself in a position where you start thinking about problems in functions. Being able to break a complex problem, like an advanced drawing, into smaller and simpler problems which can be solved with a function can make your code a lot easier to write, and a lot easier to read.

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 specified is the center of the base of the triangle making up the tree. 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 length of the base should be the same as the height.

Example

  • Drawing the trunk of the tree is simple, just draw a filled square.
  • Drawing the tree's canopy is easier if you use the turtle.setposition function. To help figure out the coordinates of the triangle corners, make a drawing of the triangle inside of the square. The triangle fits inside of a square since its width is the same as its height.

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 trees that are farther away are smaller.