CPTR 124 Fundamentals of Programming
In this lab you will implement an additional function in your
geometry
module and test it in a graphical application.
- Teams
You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.
- What to do
Copy your
geometry
module to a new folder (directory). You will be modifying the file, so you will want to keep the original copy in a safe place. In this assignment you will implement theintersection
function in yourgeometry
module.Add the file
vizgeom.py
to the same folder as yourgeometry.py
module. This graphical application uses yourgeometry
module and the standardtkinter
module. It allows the user to place two pairs of points within a graphical window. The program then draws the lines that pass through point pairs and prints the equations for each line. It also computes and displays the intersection point of the two lines.You should not modify the
vizgeom.py
program. -
intersection
FunctionThe
intersection
function expects four floating-point parameters. The first parameter represents the slope of the first line (inf
, if the first line is vertical), the second parameter is the y-intercept of the first line (x-intercept if the line is vertical). The third and fourth parameters represent the slope and intercept of a second line. The function should return a tuple representing the x and y components of the point of intersection of line #1 and line #2. The function should returnNone
if the lines do not have a single point of intersection; calling code can then check for a valid intersection point.How do you compute the point of intersection of two lines?
- If the two lines have the same slope, there is
no single point of intersection. The function
returns
None
in that case. - If neither line is vertical,
we have a pair of equations that look like
Since both righthand expressions equal y, if we set them equal to each other and solve for x, we get
Once you have x you can plug it into either of the original equations to compute y. This (x,y) point is the point of intersection of the two lines.
- If one of the lines is vertical, you cannot use the
above formula. Attempting to perform
arithmetic with
inf
will not produce a useful result. Since you know the x-intercept of the vertical line, you can deduce the x coordinate of the intersection directly from the vertical line's intercept. You can plug this x value into the equation for the non-vertical line to compute the intersection point's y coordinate.
Your
intersection
function must contain conditional logic to decide amongst the possibilities listed above. - If the two lines have the same slope, there is
no single point of intersection. The function
returns
- Strategy
The
intersection
function stub provided for you simply returnsNone
. Before you implement theintersection
function ensure that thevizgeom.py
program executes without any run-time exceptions. It should allow the user to add points and move them, and the application should draw line segments between the points. If the program crashes, it means that yourgeometry.py
module contains some lingering subtle errors. If yourgeometry
module did not work properly with thetestgeom.py
program, it has no hope of working withvizgeom.py
. Until you correctly implement theintersection
function, the lines will not extend to the edges of the window. You should, however, be able to interact with the program without it crashing.Keep things simple to begin with, and implement the typical case that does not involve vertical lines. Once you can see the red intersection point of the two line segments, then you should proceed to the case that involves vertical lines. In order for the application to extend the lines to the edges of the window, it must be able to compute the lines' intersection points with the imaginary lines that make up the horizontal edges of the window (y = 300 and y = –300) or the vertical edges of the window (x = 300 and x = –300). It calls your
intersection
function to compute these points. This is why the application cannot render the extended lines completely until you correctly implement theintersection
function.In the course of using the graphical application you may discover problems with one or more of your previous
slope
,intercept
, andlineequation
functions that the handful of tests withtestgeom.py
did not reveal. You should repair any problems you discover. - Program execution
Like the previous programming assignment, the code you write is not in control. The code provided for you is in control, and your code must be able to react to it appropriately.
The graphical application allows the user to interactively supply points for lines that potentially intersect.
The axes range from –300 to +300, so each square on the graph paper is 50 units wide by 50 units high. The location of the points, the equations of the lines, and the intersection point are printed in the console window. You do not need to program this functionality; it is built into
vizgeom.py
.Some useful interface shortcuts include:
- If you wish wish to make a vertical line immediately,
click the mouse somewhere not on the x-axes to make the
initial point of the line and then press the V key to
automatically generate the
other point to make a vertical line.
- If you wish wish to make a horizontal line immediately,
click the mouse somewhere not on the y-axis to make the
initial point of the line and then press the H key
to automatically generate the
other point to make a horizontal line.
- If you wish wish to clear all the points from the graph and make two new lines, press the Z key.
The most common problem is an incorrect and/or incomplete
intersection
function. The code withinvizgeom.py
depends on a correctly writtenintersection
function to draw a line from one side of the window to the other. It is easy to experiment with all kinds of lines via the graphical interface. If you do not see any lines or find that lines are missing in certain situations, focus on correcting yourintersection
function. Pay particular attention to how you handle vertical lines. - If you wish wish to make a vertical line immediately,
click the mouse somewhere not on the x-axes to make the
initial point of the line and then press the V key to
automatically generate the
other point to make a vertical line.
- Check out
Your finished module will be evaluated for correctness and compliance. When approved, you should submit your updated
geometry.py
Python source file to http://eclass.e.southern.edu. Be sure your name and your partner's name are included in comments in the first line of your source file.