< Back

Lab 12: Scope and Functions

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

$ cd ~/cs120/labs
$ mkdir lab12 
$ cd lab12 


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

Twin Paradox

In the year 2047, a set of twins were born. One of these twins was placed in a rocket and launched towards a distant star at a significant percentage of the speed of light. Unlike Superman, however, the rocket was programmed to return home after reaching its destination. Because of the laws of relativity, the twin who was launched into space at close to the speed of light will have aged significantly less than their sibling. The real question is, how much younger are they?

Details

Create the function compute_age_difference(distance_traveled, percentage_of_light) in a file called twin_paradox.py. This function should take as parameters an integer representing a distance traveled in meters, and a floating point value in the range \((0, 1)\) which represents how close to the speed of light the twin traveled. Your function should use the laws of relativity to compute how many years younger the twin is.

Note that we are assuming all acceleration is instant, so we can simplify the equations. The speed of light is \(2.99 \times 10 ^ 7 \mbox{ } \frac{m}{s^2}\), and the number of seconds passed on earth can be computed using time equation below, and the percentage of time passed on the rocket as compared to earch can be computed using dialation equation below. $$ \begin{eqnarray} time &=& 2 \times \frac{distance}{speed\_traveled}\\ \\ dialation &=& \sqrt{1 - \frac{speed\_traveled}{speed\_of\_light ^ 2}} \end{eqnarray} $$

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

Test Cases

Parameters Expected Output
4 light years, 90% speed of light 5.071622487746852
4 light years, 50% speed of light 2.1680934673491077
0 light years, 90% speed of light 0.0
1 light year, 90% speed of light 1.267905621936713
1 light year, 50% speed of light 0.5420233668372769
10 light years, 99% speed of light 17.550499006858566
10 light years, 99.999% speed of light 20.138324055460178

Hint

  • Write a function called convert_light_years_to_meters(light_years), which converts a distance in light years to meters. One light year is \(9.56 \times 10 ^ {15}\) meters.
  • The time equation listed above returns the time that has passed on Earth in seconds. Write a function called convert_seconds_to_years(seconds), which returns a floating point value representing how many years that number of seconds represents.
  • Using the above equations, you simply need to subtract the time that passed on the ship from the time passed on earth.

Challenge

What if, instead of just launcing one of the children into space, these crazy scientists launched both? Likely, they would have tried to send two of the siblings off into space, but at different speeds. Write a new function called compute_relativistic_age_difference, which computes the difference between two siblings who were launched into space at differing speeds.