Assignment 1: Points and Lines

Create a class Point and a class Line. The Point class deals with points in a Cartesian coordinate system, the class Line deals with Lines in the plane. Recall that a line is determined either by two points or by a point and the slope or by a point and the y-intercept.

First, let’s focus on the Point class and figure out its fields and methods.

Fields: x and y coordinate of a point, both double values
Methods: display, getX, setX, getY, setY as usual. We also might need a method ‘distance’ which computer the distance of the current point to the origin.

Here is the declaration of the class Point. Please add comments where appropriate before turning in the assignment:

#include <iostream.h>
#include <math.h>

#ifndef POINT
#define POINT

class Point
{     
   private: double x, y;      // x and y are the (x,y) coordinates of a point

   public: Point(void);       // Constructors
   public: Point(double, double);
 
   public: void setX(double);  // Methods
   public: void setY(double);
   public: double getX(void);
   public: double getY(void);
   public: void display(void);       // display method without linefeed
   public: double distance(void);    // distance to origin
};

// all method implementations go here and are left as homework

#endif

After the method implementations have been added, we need a test program to test this class. Assuming we have saved our above class as Point.cpp, a simple test program could look as follows:


#include <iostream.h>
#include "Point.cpp"

int main(void)
{
   Point P, Q(3,3);
   cout << "The following points are defined \n";
   P.display(); cout << "\t";
   Q.display(); cout << "\n";
   cout << "changing coordinates of point P to (-1, -1)\n";
   P.setX(-1); P.setY(-1);
   P.display(); cout << "\n";
   cout  << "Distance of P to origin should be sqrt(2): "
         << P.distance() << "\n";
   return 0;
}

Next, let’s concentrate on the Line class.Recall from your calculus or pre-calculus class that a line is completely defined by:

two points, or
the slope and one point, or
the slope and the y-intercept

Therefore, we have three options for storing enough information to uniquely determine a line - i.e. we will give our class at least three appropriate constructors. Please implement a line class as follows:

Class Name:
Line
Fields:
slope and intercept (double types)
Constructors:
Line(void) to initialize slope and intercept to zero
Line(double, double) to initialize a line using slope and y-intercept
Line(double, Point) to initialize a line using the slope and a point
Line(Point, Point) to initialize a line using two points
Methods:
display(void) displays the line in slope-intercept form
setSlope(double) sets the slope of line to the input value
setIntercept(double) sets the y-intercept of the line to the input value
getSlope(void) returns the slope of the line
getIntercept(void) returns the y-intercept of the line
containsPoint(Point) returns 1 if the line contains the input point, 0 otherwise
isPerpendicularTo(Line) returns 1 if the line is perpendicular to the input line, 0 otherwise
isParallelTo(Line)returns 1 if the line is parallel to the input line, 0 otherwise

Note that the Line class needs to know about the Point class. Therefore, the Line class should start as follows:

#include <iostream.h>	// needed for the display method
#include "Point.cpp"	// so that this class knows about Points
#ifndef LINE
#define LINE

// class declaration and implementation here

#endif

Also create a small test program to test your line class. To summarize: you must turn in four files for this assignment:

Point.cppcontains the Point class
Line.cppcontains the Line class
linetst.cpptest program for Line class

(bgw)