-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXY.h
59 lines (49 loc) · 1.37 KB
/
XY.h
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
// Copyright 2016 Carrie Rebhuhn
#ifndef MATH_XY_H_
#define MATH_XY_H_
#include <utility>
namespace easymath {
//! A class for locations. Contains many overloads for vector arithmetic.
class XY : public std::pair<double, double> {
public:
XY(const double &x, const double &y) :
pair<double, double>(x, y), x(x), y(y) {}
XY() {};
double x, y;
//! Vector subtraction
XY operator-(const XY &rhs) const {
return XY(x - rhs.x, y - rhs.y);
}
//! Orders first by x values, then by y values
bool operator<(const XY &rhs) const {
if (x != rhs.x) return x < rhs.x;
return y < rhs.y;
}
//! Checks equality of both elements
bool operator==(const XY &rhs) const {
return x == rhs.x && y == rhs.y &&
first == rhs.first && second == rhs.second;
}
//! Scalar multiplication
XY operator*(double rhs) const {
return XY(x*rhs, y*rhs);
}
//! Dot multiplication
double operator*(const XY &V) const {
return x*V.x + y*V.y;
}
//! Vector addition
XY operator+(const XY &rhs) const {
return XY(x + rhs.x, y + rhs.y);
}
//! Assignment operator
XY& operator=(const XY &rhs) {
x = rhs.x;
y = rhs.y;
first = rhs.first;
second = rhs.second;
return *this;
}
};
} // namespace easymath
#endif // MATH_XY_H_