CPTR 124 Fundamentals of Programming


In this lab you will write a program consisting of multiple functions that dabble in analytic geometry.


  1. 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.

  2. What to do

    Complete the given C++ program below that requests four double-precision floating point numbers from the user. These four values, in order, represent the x1, y1, x2, and y2 components of the two geometric points (x1, y1) and (x2, y2).

    The program should then do four things:

    1. Compute and display the distance between the points.
    2. Compute and display the slope of the line that passes through the two points.
    3. Compute and display the y-intercept of the line that passes through the two points. If the line is a vertical line, compute the x-intercept instead.
    4. Print, in slope-intercept form, the equation of the line that passes through the two points.

    Implement the above four tasks by four separate functions as described below:

    • Distance

      The distance between two points (x1, y1) and (x2, y2), is given in the following formula:

      Distance formula

      Write a function named distance that accepts four double-precision floating point arguments x1, y1, x2, and y2 representing two geometric points. It should return the double-precision floating point value representing the distance between the two points.

      The distance function's prototype is

              double distance(double x1, double y1, double x2, double y2);
                   

    • Slope

      The slope of the line passing through two points (x1, y1) and (x2, y2), is the difference of the y values divided by the difference of the x values.

      Write a function named slope that accepts four double-precision floating point arguments and returns the double-precision floating point value representing the slope of the line passing through the two points represented by the arguments.

      The slope function's prototype is

              double slope(double x1, double y1, double x2, double y2);
                   

      Watch out for vertical lines! Return the predefined constant HUGE_VAL to represent infinity if the line is vertical.

      Note: The function's behavior is undefined for parameters that represent two equal points. When we say the function's behavior is undefined, we mean you do not need to worry about what your function does if the client attempts to compute the slope with two points that are the same.

    • Intercept

      An intercept is a number that represents a location on an axis where a line intersects that axis. The y-intercept of a line is the y value of the point where a line crosses the y-axis. Write a function named intercept that accepts four double-precision floating point arguments representing the coordinates of two points. The function ordinarily returns the double-precision floating point value representing the y-intercept of the line that that passes through the two points defined by the arguments.

      The intercept function's prototype is

              double intercept(double x1, double y1, double x2, double y2);
                   

      Vertical lines do not have a y-intercept, so for vertical lines the intercept function should instead return the x-intercept.

      Note: The function's behavior is undefined for parameters that represent two equal points. Again, this means you need not worry about this possibility in your function.

    • Line equation

      The equation of a line in slope-intercept form is

      y = mx + b
      where m is the line's slope, and b is the line's y intercept.

      Write a function named line_equation that accepts two double-precision floating point arguments representing the slope and y-intercept of a line. It prints the the equation of that line in slope-intercept form. The function returns nothing; its prototype is:

              void line_equation(double m, double b);
                   

      If the first parameter a caller passes to the function is HUGE_VAL (representing infinity), the line is vertical. In this case the second parameter represents the x-intercept instead of a y-intercept. A vertical line has an equation of the form

      x = b
      where b is the x-intercept. Your function should print the equation for vertical lines correctly.

      Note: The function's behavior is undefined for parameters that represent two equal points.

      If your line_equation displays the correct mathematical results and the other three functions are correct, you will receive 9/10 points for this assignment. To receive the full 10/10 score, on the first or second test of your program on or before the due date the equation your function prints should be "pretty," meaning do not print an equation like

         y = -1x + -3
                   
      rather the function should print
         y = -x - 3
                   
      and an equation like
         y = 20.5x + 0 
                   
      should appear as
         y = 20.5x
                   
      This means you will need some conditional statements within your line_equation function to fine tune the exact output. As you begin developing your line_equation function do not worry about pretty output. First concentrate on getting the numbers correct, and after you are sure the displayed results are mathematically sound then proceed to make the output more attractive.

  3. Skeleton Code

    Copy the following program into your project.

    
       //  File geometry.cpp
       
       #include <iostream>
       #include <cmath>
       
       using namespace std;
    
    
       /*  -------------------------------------------------------  */
       /*  Do not touch anything above this line                    */
       
       //////////////////////////////////////////////////////////
       //
       //    ADD YOUR CODE HERE
       //
       //////////////////////////////////////////////////////////
    
       /*  Do not touch anything below this line                    */
       /*  -------------------------------------------------------  */
       
       void print_point(double x, double y)
       {
           cout << "(" << x << "," << y << ")";
       }
       
       void do_distance(double x1, double y1, double x2, double y2)
       {
           cout << "The distance between ";
           print_point(x1, y1);
           cout << " and ";
           print_point(x2, y2);
           cout << " is " << distance(x1, y1, x2, y2) << endl;      
       }
       
       void do_slope(double x1, double y1, double x2, double y2)
       {
           cout << "The slope of the line between ";
           print_point(x1, y1);
           cout << " and ";
           print_point(x2, y2);
           cout << " is ";
           double m = slope(x1, y1, x2, y2);
           if ( m == HUGE_VAL )
               cout << "undefined";
           else
               cout << m;
           cout << endl;
       }
       
       void do_intercept(double x1, double y1, double x2, double y2)
       {
           if ( slope(x1, y1, x2, y2) == HUGE_VAL )
               cout << "The x-";
           else
               cout << "The y-";
           cout << "intercept of the line between ";
           print_point(x1, y1);
           cout << " and ";
           print_point(x2, y2);
           cout << " is " << intercept(x1, y1, x2, y2)
               << endl;
       }
       
       void do_equation(double x1, double y1, double x2, double y2)
       {
           cout << "The equation of the line between ";
           print_point(x1, y1);
           cout << " and ";
           print_point(x2, y2);
           cout << " is ";
           line_equation(slope(x1, y1, x2, y2), intercept(x1, y1, x2, y2));
           cout << endl;
       }
       
       int main()
       {
           for (;;)   //  Infinite loop, use Control-C to quit
           {
               double p_x1, p_y1, p_x2, p_y2;
               cout << "Enter the point coordinates x1, y1, x2, y2 "
                    << "(Control-C quits): ";
               cin >> p_x1 >> p_y1 >> p_x2 >> p_y2;
               do_distance(p_x1, p_y1, p_x2, p_y2);
               do_slope(p_x1, p_y1, p_x2, p_y2);
               do_intercept(p_x1, p_y1, p_x2, p_y2);
               do_equation(p_x1, p_y1, p_x2, p_y2);
               cout << "-----------------" << endl;
           }
       }
    
    

    Add your code to the section specified within the comments. Do not touch any of the code outside of that section. Observe that because I have provided the main function you should NOT write your own main function. This means that, unlike all the previous programming assignments, 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.

             

  4. Strategy

    To keep things simpler to begin with, don't worry about handling vertical lines and don't worry about "pretty" equation output. After everything works under those limited conditions, then implement pretty output and vertical line capability.

  5. Check out

    Your finished program will be evaluated for correctness and compliance. When approved, you should submit your C++ source file to eclass.e.southern.edu. Be sure your name and your partner's name are included in comments at the top of each source file.