CPTR 124 Fundamentals of Programming
In this lab you will implement an additional function in your
geometrymodule and test it in a graphical application.
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
geometrymodule 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 the
intersectionfunction in your
Add the file
vizgeom.pyto the same folder as your
geometry.pymodule. This graphical application uses your
geometrymodule and the standard
tkintermodule. 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
intersectionfunction 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 return
Noneif 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
Nonein 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
infwill 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.
intersectionfunction 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
intersectionfunction stub provided for you simply returns
None. Before you implement the
intersectionfunction ensure that the
vizgeom.pyprogram 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 your
geometry.pymodule contains some lingering subtle errors. If your
geometrymodule did not work properly with the
testgeom.pyprogram, it has no hope of working with
vizgeom.py. Until you correctly implement the
intersectionfunction, 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
intersectionfunction to compute these points. This is why the application cannot render the extended lines completely until you correctly implement the
In the course of using the graphical application you may discover problems with one or more of your previous
lineequationfunctions that the handful of tests with
testgeom.pydid 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
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
intersectionfunction. The code within
vizgeom.pydepends on a correctly written
intersectionfunction 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 your
intersectionfunction. 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.pyPython 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.