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
Write a function called equilateral_area(edge_length)
that computes the area of an equilateral triangle with the specified
edge length.
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:
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.
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.
draw_triangle
function.
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.
Using a for loop, draw a full forest of trees. You should use a for loop to draw the following collection of trees.
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?
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.
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 |
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.
convert_seconds_to_years(seconds)
, which returns
a floating point value representing how many years that number
of seconds represents.
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.