CPSC 170 A

- Location
- Trexler 363
- Times
- MWF 3:30 - 5:30

- Office Hours
- M-Th 6 - 7pm
- Office
- Trexler 365B
- chssmith AT roanoke DOT edu

< Back

### Activity 0

Top-down Design

Top-down design is a process of iteratively breaking a problem down into smaller, more manageable pieces. This often allows for more efficient, and more concise code. Today, we will practice top down design with the following problem.

For the following assignment statement, write out on paper what functions you think you will need. We will go over this before we start on the full assignment.

Create a program that uses Turtle to draw a triangle and a random point and tests if the point is inside the triangle. The program should draw a triangle that takes up the majority of the window. You can pick the location of each of the corners, or vertices, of the triangle. The program should also generate and draw a random point in the window. The color of the circle should be determined by whether the point is inside or outside the triangle.

A point is inside a triangle if it is on the interior side of all three edges of the triangle. For example, the point P in the illustration below is on the interior side of the edge from B to C and the edge from C to A, but it is not on the interior side of the edge from A to B.

It is possible to determine which side of an edge a point is on using the equation:

\[ s = (V1_x - V2_x) \times (V1_y - P_y) - (V1_y - V2_y) \times (V1_x - P_x) \]Where V1 and V2 are vertices that define an edge of the triangle, and P is a point. If the vertex V1 is clockwise from V2, then s will be positive if P is on the interior side of the line segment between V1 and V2.