CPTR 124 Fundamentals of Programming


In this lab you will rewrite your code from the Visual Geometry lab so that it uses objects rather than the primitive C++ numeric types.


  1. Ensure that you have the necessary graphics files

    Refer to the original Visual Geometry assignment to ensure you have the graphics files (sgl.hpp, sgl.h, etc.) you need.

  2. Create a new project

    Create a new project and add the following three new files

    • obj-geom.h This file contains declarations for two classes and several free functions. You will not modify this file.

    • obj-geom.cpp This file contains the implementations of the methods and free functions declared in the header file. This is the file you will modify.

    • objvisgeom.cpp This file contains client code re-implementing the visual geometry application using your Point and Line objects. You will not modify this file.

  3. Implement the methods, constructors, and free functions declared in obj-geom.h

    Edit the obj-geom.cpp file to implement the methods, constructors, and free functions for the Point and Line classes:

    • The Point class represents the notion of a mathematical point:
      • Mathematical points have x and y double-precision floating-point components. We will consider a point object to be a "primitive" type; that is, the x and y components are public. Clients can manipulate freely Point objects as if they are integers or floating-point numbers.

      • The Point class has a constructor that allows a a client to create a point object by supplying a pair of double-precision floating-point values representing the x and y coordinates of the point.

      • The Point class has a to_string method that returns a convenient, human-readable representation of a point object as a std::string; for example, (1.5, 0.2).

      • Provide a free function named distance that accepts two Point objects as arguments and returns a double-precision floating-point number as a result. The returned quantity represents the distance between the two points passed by the client.

      • Overload operator<< so that it prints a point object in the usual form (it even can use the Point::to_string method to do the work).

      The following client code fragment shows a client can make and use Point objects:

      Point pt1{2, 5}, pt2{10, 5}; std::cout << distance(pt1, pt2) << " is the distance between " << pt1 << " and " << pt2 << '\n';
      this code fragment should print
      8 is the distance between (2, 5) and (10, 5)

    • The Line class represents the notion of a mathematical line:
      • A mathematical line has a slope and intercept component, with both being double-precision floating-point values. As before, a vertical line will have a slope of INFINITY, and we will interpret its intercept as an x intercept. For non-vertical lines, we interpret the intercept as the line's y intercept.

        We will consider a line object to be immutable; that is, a client cannot change the state of a line object once created. This means the slope and intercept components of an existing line object are not adjustable.

      • The Line class has two overloaded constructors. One constructor allows a client to create a line object by supplying two Point objects through which the line is to pass; the other constructor accepts a slope and intercept value.

      • The Line class has a method named slope that returns the Line object's slope. This allows clients to see a line object's slope without being able to change it.

      • The Line class has a method named intercept that returns the Line object's intercept. This allows clients to see a line object's intercept without being able to change it.

      • The Line class has a method named is_vertical that returns true if the line object is vertical; otherwise, it returns false.

      • The Line class has a to_string method that returns a convenient, human-readable representation of a line object as a std::string; it uses the form y = mx + b form for nonvertical lines or x = b form for vertical lines.

      • Provide a free function named intersection that accepts two Line objects as arguments. The function should return a Point object that represents the point of intersection between the two line objects passed to it. If the two have no single point of intersection—that is, the lines are parallel to each other (no intersection point) or the two lines are the same (an infinite number of points in common)—the function should return the point which its x and y components both set to INFINITY.

      • Overload operator<< so that it prints a line object in the usual form, either the y = mx + b form for vertical lines or x = b form for horizontal lines. It can use Line::to_string to do most of the heavy lifting.

      The following client code fragment shows a client can make and use Line objects:

      Point pt1{2, 5}, pt2{10, 3}; Line ln{pt1, pt2}; std::cout << "The line " << ln << " passes through " << pt1 << " and " << pt2 << '\n';
      this code fragment should print
      The line y = -0.25x + 5.5 passes through (2, 5) and (10, 3)

  4. Program execution

    The graphical application allows the user to interactively supply points for lines that potentially intersect. It works exactly like the Visual Geometry application from an earlier assignment. The obj-geom.cpp file contains method and function stubs. They contain all the code to allow the project to compile, but the application will have limited functionality. Be sure that you can build and run the program with the original obj-geom.cpp before you begin modifying obj-geom.cpp. You should fill in the stubs a little at a time but always keep a buildable, runnable program.

  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 is included in a comment at the top of each source file.