Repository navigation
Expand file tree
/
Copy pathPoint.cpp
More file actions
126 lines (98 loc) · 2.26 KB
/
Copy pathPoint.cpp
File metadata and controls
126 lines (98 loc) · 2.26 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include "Point.h"
#include <string>
#include <cmath>
//constructors
Point::Point() {
x = 0;
y = 0;
}
Point::Point(double x_, double y_){
x = x_;
y = y_;
}
Point::Point(double z_) {
x = y = z_;
}
Point::~Point() {
// deallocate the memory that was previously reserved for object
}
//copy constructor
Point::Point(const Point& sauce) {
X(sauce.X());
Y(sauce.Y());
}
//operator overloading
Point Point::operator-() const{
Point temp = Point(x*-1, y*-1);
return temp;
}
Point Point::operator* (double factor) const {
Point temp = Point(x*factor, y*factor);
return temp;
}
Point Point::operator+ (const Point& p) const{ // Add Coordinates
Point temp = Point(x + p.X(), y + p.Y());
return temp;
}
bool Point::operator== (const Point& p) const {
if (p.X() == x && p.Y() == y) {
return true;
}
return false;
}
Point& Point::operator= (const Point& source) {
x = source.X();
y = source.Y();
return *this;
}
Point& Point::operator*= (double factor) {
x *= factor;
y *= factor;
return *this;
}
/*
std::ostream& operator<< (std::ostream& os, const Point& source) {
os << "(" << source.x << ", " << source.y << ")" << std::endl;
return os;
}
*/
//selectors
double Point::X() const {
return x;
}
double Point::Y() const {
return y;
}
std::string Point::ToString() const{
return ("( " + std::to_string(x) + ", " + std::to_string(y) + " )\n");
}
//mutators
void Point::X(double x_){
x = x_;
}
void Point::Y(double y_){
y = y_;
}
double Point::Distance() const {
double a = std::abs(x);
double b = std::abs(y);
return (std::sqrt(pow(a,2) + pow(b,2)));
}
//altered signature to pass-by-reference in order to avoid copy constructor call and reduce overhead. Made argument const as per instructions so function cannot alter state of referenced value.
double Point::Distance(const Point& p) const{
double a, b;
// p.x = 55; uncomment to test read-only nature of const Point& reference.
if (p.x > x) {
a = p.x - x;
}
else { a = x - p.x; }
if (p.y > y) {
b = p.y - y;
}
else { b= y - p.y; }
std::cout << "a: " << std::to_string(a) << " b: " << std::to_string(b) << std::endl;
std::cout << "a^2 = " << pow(a, 2) << std::endl;
std::cout << "b^2 = " << pow(b, 2) << std::endl;
return (std::sqrt(std::pow(a, 2) + std::pow(b,2)));
}