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 the line that passes through the two points. If the line is a vertical line, the x-intercept should be computed instead.
    4. Print, in slope-intercept form, the equation of the line that passes through the two points.

    Each of the above four tasks should be handled by 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.

      Its 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 represeted by the arguments.

      Its 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

      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.

      Its prototype is

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

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

      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);
                   

      Your equation should be "pretty," meaning do not print an equation like

         y = -1x + -3
                   
      rather it should be printed as
         y = -x - 3
                   
      and an equation like
         y = 20.5x + 0 
                   
      should be printed as
         y = 20.5x
                   
      This means you will need some conditional statements within your line_equation function to fine tune the exact output.

      If the first parameter passed to the function is HUGE_VAL (representing infinity), it represents a vertical line. The second parameter then represents the x-intercept instead of a y-intercept.

      A vertical line has 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.

  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.

             

  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. Be sure your name and your partner's name are included in comments at the top of each source file.