Loading [MathJax]/jax/output/HTML-CSS/jax.js

CPSC120A
Fundamentals of Computer Science I

Lab 12

Scope

Practice 1

Write a function called equilateral_area(edge_length) that computes the area of an equilateral triangle with the specified edge length.

area=34×length2


Practice 2

Copy and paste your solution to the previous exercise into this one, and use it when writing a function called sierpinski_area(edge_length) 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:


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 function draw_tree(pen, x_location, y_location, width) in a file called forest.py. This function should take as a parameter a turtle object (pen) which can be used to draw a tree to the turtle window. The base of the tree should be centered around the point (x_location,y_location) and the largest set of branches should have a width as specified. Your tree should look like the tree in the picture in the example below.

Notice that a tree is defined by a brown square, and four green triangles which overlap each other. For this, define two additional functions in the same file. The first should be called draw_square(pen, x_location, y_location, side_length) which draws a filled in brown square square centered around the specified point. The second should be called draw_triangle(pen, x_location, y_location, side_length), which creates an equilateral triangle with the specified side length centered around the specified point.

It should also be noted that each triangle is 10% smaller than the triangle below.

Test your function by calling the function multiple times with different parameters. Make sure your code follows the course's code conventions.

Example Output

Turtle Drawing of a Tree

Hint

Challenge

Using a for loop, draw a full forest of trees. You should use a for loop to draw the following collection of trees.

Turtle Drawing of a Forest

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.