# CPSC120BFundamentals of Computer Science I

## Assignment 2

Loops

The first electronic programmable computer, ENIAC, was created for performing ballistic calculations for the United States Army. ENIAC was the size of a school bus and weighed 30 tons. A lot has changed over the years, to the point where pocket sized computers (Cell Phones) now have more computational power than the ENIAC. For this assignment, you will use your modern day computer and the turtle module to perform similar calculations.

### Details

Using the Turtle module, your program should draw a series of 3 concentric circles on the right hand side of the screen. These circles will represent the target that your ballistic simulation will be aiming for.

Your program is going to launch projectiles from the lower left corner of the window. To accomplish this, your program should prompt the user to enter both an initial x component of velocity, and an initial y component of velocity.

Given this information, your program should draw some fixed number of lines segments that will approximate the ballistic trajectory of the launched projectile. Each line segment can be calculated by using the projectile's current velocity to compute the new location of the projectile (assuming the projectile is traveling in a straight line). Once you compute the new position, you then need to compute the new velocities in the x and y directions. You can do this by simply subtracing the acceleration due to gravity (9.81 m/s2) from the y component of velocity, and adding the acceleration due to wind (Which we will assume for now is 1 m/s2).

Keep in mind that your program does not need to stop when the user hits the target, nor does your program need to stop when the turtle leaves the screen. You simply need to draw some fixed number of line segments to represent the projectile path.

### "Hacker" Prompt

Each week, additional exercises related to the assignment will be provided at the end. These exercises are typically more challenging than the regular assignment. Bonus points will be provided to students who complete any of the "Hacker" level assignments.

1. Continued Play: Right now, your program stops after the first projectile. For this hacker prompt, you should restructure your program such that, after the first projectile, it asks again for another set of x and y velocities. This should continue for some fixed number of steps.

2. Air Resistance: The code above assumes the projectiles are fired in a vacuum. While space combat seems fun, it's not very useful for Earth based projectiles. Add air resistance to the simulation. Air resistance is an acceleration that is always opposite the direction of the velocity. Thus the x and y components of the air resistance must be recalculated every time velocity is updated.

3. The Monkey and the Hunter: The monkey and the unter is a classic physics question. If a hunter aims a dart gun directly at a monkey hanging on a branch and the monkey lets go at the same instant the hunter fires, will the monkey be hit? Simulate this by creating two projectiles. One projectile, the monkey, with an initial velocity of 0 in both the x and y direction. The other projectile, the dart, with an initial velocity directly toward the initial location of the monkey.

The assignment will be graded on the following requirements according to the course’s programming assignment rubric.

Functionality (75%): A functional program will:

• draw 3 concentric circles,
• align these circles in the lower right hand corner of the screen,
• ask the user for 2 integer input values for x and y velocity,
• draw a fixed number of line segments representing the ballistic trajectory of the projectile,
• start the path of the projectile from the lower left hand corner of the screen, and
• account for gravitational and wind forces in the computation of the ballistic trajectory.

Style (25%): A program with good style will:

• include a header comment signifying the authors of the file,
• avoid magic numbers (literal primitive numbers),
• use meaningful names for variables,
• have statements that are small (80 characters or less including leading space) and do one thing, and
• have spaces after commas in argument lists and spaces on both sides of binary operators (=, +, -, *, etc.).

Oscar Reutersvärd is Swedish artist most famous for creating geometric drawings that are simple to draw in 2 dimensions but represent things that are impossible to create in 3 dimensions. Loops are great for creating repetitive geometric designs like Reutersvärd did before the invention of the computer.

### Details

Create a Python program that uses the turtle module to Reutersvärd's Impossible Triangle like the following:

• The program should draw solid cubes like the ones pictured above. That is, the cubes should be oriented so the corner of the cube that appears closest is centered in the drawing of the cube. The cubes can be any color you want.
• The cubes should be arranged in an equilateral triangle with four cubes on a side.
• The cubes should be spaced out so that the negative space inside the triangle produces a star.
• The triangle of cubes should overlap in a way that is impossible as pictured above. That is, the cubes should overlap in 2 dimensions is a way that is not possible in 3 dimensions.
• ### Extra

Reutersvärd is just one of several artists producing Impossible Figures. Do an image search for Impossible Figures and pick another cool impossible object to draw with turtle.

The assignment will be graded on the following requirements according to the course’s programming assignment rubric.

Functionality (75%): A functional program will:

• draw a solid cube as pictured above.
• draw multiple cubes using a loop.
• draw multiple cubes using a loop spaced out as pictured above.
• draw three lines of cubes that make a triangle.
• draw an impossible triangle of cubes.

Style (25%): A program with good style will:

• include a header comment signifying the authors of the file,
• avoid magic numbers (literal primitive numbers),
• use meaningful names for variables,
• have statements that are small (80 characters or less including leading space) and do one thing, and
• have spaces after commas in argument lists and spaces on both sides of binary operators (=, +, -, *, etc.).

A binary clock uses an array of lights to represent time. In this assignment you will create a binary timer.

### Details

Read about series and parallel circuits and then create a circuit with 4 LED lights in a row. Create a Python program that uses the LEDs to display the number of seconds, in binary, since the script began running. After the timer reaches 15, the largest integer that can be displayed with 4 bits. The timer should reset back to 0.

The assignment will be graded on the following requirements according to the course’s programming assignment rubric.

Functionality (75%): A functional program will:

• have a circuit with four LEDs that can be controlled with Raspberry gPIo.
• display a binary number using the LED lights.
• display different binary numbers 1 second apart.
• display the numbers 0 - 15 in sequence using the LEDs.
• start over at zero after displaying 15 with the LEDs.

Style (25%): A program with good style will:

• include a header comment signifying the authors of the file,
• avoid magic numbers (literal primitive numbers),
• use meaningful names for variables,
• have statements that are small (80 characters or less including leading space) and do one thing, and
• have spaces after commas in argument lists and spaces on both sides of binary operators (=, +, -, *, etc.).

## Submission

Submit your program as a .py file on the course Inquire page before class on Wednesday September 14th.