-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.cpp
102 lines (81 loc) · 1.64 KB
/
point.cpp
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
#include "point.hpp"
#include "fcmp.hpp"
#include <cmath>
#include <iomanip>
#include <sstream>
Point::Point() : x_{}, y_{}
{
}
Point::Point(double x, double y) : x_{x}, y_{y}
{
}
double Point::x() const
{
return x_;
}
double Point::y() const
{
return y_;
}
bool Point::operator==(const Point & other)
{
return fcmp(x_, other.x_) && fcmp(y_, other.y_);
}
bool Point::operator!=(const Point & other)
{
return !operator==(other);
}
Point & Point::operator+=(const Point & other)
{
x_ += other.x_;
y_ += other.y_;
return *this;
}
Point & Point::operator-=(const Point & other)
{
x_ -= other.x_;
y_ -= other.y_;
return *this;
}
Point Point::operator+(const Point & rhs) const
{
Point lhs(*this);
lhs += rhs;
return lhs;
}
Point Point::operator-(const Point & rhs) const
{
Point lhs(*this);
lhs -= rhs;
return lhs;
}
std::string Point::description() const
{
std::stringstream ss;
ss << "Point "
"(" << x() << ", " << y() << ")";
return ss.str();
}
#ifdef UNITTEST_POINT
#include "xassert.hpp"
int main()
{
xassert(fcmp(Point().x(), 0));
xassert(fcmp(Point().y(), 0));
xassert(Point() == Point(0, 0));
xassert(Point() != Point(1, 1));
xassert(fcmp(Point(3, 4).x(), 3));
xassert(fcmp(Point(3, 4).y(), 4));
auto a = Point(1, 2);
a += Point(10, 20);
xassert(a == Point(11, 22));
auto b = Point(10, 20);
b -= Point(1, 2);
xassert(b == Point(9, 18));
a = Point(1, 2);
b = Point(10, 20);
xassert(a + b == Point(11, 22));
xassert(b - a == Point(9, 18));
xassert(a.description() == std::string("Point (1, 2)"));
}
#endif