-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPoint.cpp
More file actions
93 lines (80 loc) · 2.43 KB
/
Point.cpp
File metadata and controls
93 lines (80 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//******************************************************************************
// Point.cpp
// Polymorphism
//
// Created by Karlina Beringer on July 8, 2014.
//
// This source file contains the Point class definition.
// Point is included in Polymorphism through composition.
//******************************************************************************
#include "Point.h"
// Default constructor creates a Point at the graph origin.
Point::Point()
{
X_coordinate = 0.0;
Y_coordinate = 0.0;
}
// Normal constructor creates a custom Point object.
Point::Point( double X, double Y )
{
X_coordinate = X;
Y_coordinate = Y;
}
// Copy constructor creates a clone of a Point object.
Point::Point( Point & point )
{
X_coordinate = point.X_coordinate;
Y_coordinate = point.Y_coordinate;
}
// Setter changes the value of the X_coordinate.
void Point::setX( double X )
{
X_coordinate = X;
}
// Setter changes the value of the Y_coordinate.
void Point::setY( double Y )
{
Y_coordinate = Y;
}
// Getter returns the value of the X_coordinate.
double Point::getX()
{
return X_coordinate;
}
// Getter returns the value of the Y_coordinate.
double Point::getY()
{
return Y_coordinate;
}
// Getter computes the distance between this and another Point.
// Distance = Square_Root[ ((X2 - X1)^2) + ((X2 - X1)^2) ]
double Point::getDistance( Point & point )
{
return sqrt( pow(X_coordinate - point.X_coordinate, 2) +
pow(Y_coordinate - point.Y_coordinate, 2));
}
// Getter computes the slope between this and another Point.
// Slope = (Y2 - Y1) / (X2 - X1)
double Point::getSlope( Point & point )
{
if (X_coordinate == point.X_coordinate) return INFINITY;
return (Y_coordinate - point.Y_coordinate)/
(X_coordinate - point.X_coordinate);
}
// Getter prints the coordinate pair of Point in the form ( X, Y ).
// If no ostream parameter is supplied, default will be cout.
void Point::print( ostream & output )
{
output << "(" << X_coordinate << "," << Y_coordinate << ")";
}
// Friend function behaves like Points's print method.
// Overloads the ostream operator.
// Friend is NOT a member of Point, but has access to its members.
ostream & operator << ( ostream & output, Point & point )
{
point.print( output );
return output;
}
//******************************************************************************
// End of File
//******************************************************************************