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 = \frac{\sqrt{3}}{4} \times length ^ 2 \]


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

  • You have already written code that draws an equilateral triangle in a post lab. Look back at that code for help writing the draw_triangle function.
  • Your draw_tree function should consist basically of one call to draw_square and four calls to draw_triangle. You can use a for loop to make drawing the four triangles easier.
  • You can use variable reassignment to keep track of the width of the next triangle to be drawn. If at loop iteration 0 your width was 100, you want the width of the next triangle to be 90 at loop iteration 1. You can compute this by multiplying the current width by .9, and assigning the value back into your variable storing current width.

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.