-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoints.hpp
139 lines (105 loc) · 2.8 KB
/
points.hpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef POINTS_HPP
#define POINTS_HPP
/**
* @file /home/ryan/uml/robotics/checkers/points.hpp
* @author Ryan Domigan <ryan_domigan@[email protected]>
* Created on Mar 30, 2014
*
* Two and three dimentional points
*/
#include <iostream>
template<class Num>
class Pair {
public:
Num x, y;
Pair() = default;
template<class N>
Pair(const Pair<N>& p) : x(p.x), y(p.y) {}
Pair(Num i_x, Num i_y) : x(i_x), y(i_y) {}
Num& operator[](int i) {
if(i == 0) return x;
return y;
}
const Num& operator[](int i) const {
if(i == 0) return x;
return y;
}
Pair operator+(const Pair& p) const {
return Pair(p.x + x, p.y + y); }
Pair operator-(const Pair& p) const {
return Pair(x - p.x, y - p.y); }
template<class Numeric>
Pair operator/(Numeric i) const {
return Pair(x / i, y / i); }
/* scalar product */
Num dot(const Pair& p) const { return (x * p.x) + (y * p.y); }
bool operator<(const Pair& p) const {
return (x < p.x) && (y < p.y); }
bool operator>(const Pair& p) const {
return (x > p.x) && (y > p.y); }
bool operator<=(const Pair& p) const {
return (x <= p.x) && (y <= p.y); }
bool operator>=(const Pair& p) const {
return (x >= p.x) && (y >= p.y); }
bool operator==(const Pair& p) const {
return (x == p.x) && (y == p.y);
}
bool operator!=(const Pair& p) const { return !(*this == p); }
Num& row() { return x; }
Num& column() { return y; }
Num row() const { return x; }
Num column() const { return y; }
Pair<Num> transpose() const {
std::swap(x,y);
return *this;
}
Pair<Num>& transpose() {
std::swap(x,y);
return *this;
}
};
template<class Num>
std::ostream& operator<<(std::ostream &out, const Pair<Num>& p) {
return out << "(" << p.x << "," << p.y << ")";
}
typedef Pair<int> iPair;
typedef Pair<double> dPair;
typedef Pair<float> fPair;
template<class Num>
struct Triplet : public Pair<Num> {
typedef Pair<Num> P;
Num z;
Triplet() = default;
Triplet(P p, Num z) : P(p), z(z) {}
Triplet(Num x, Num y, Num z) : P(x, y), z(z) {}
Triplet& operator-() {
P::x = -P::x;
P::y = -P::y;
z = -z;
return *this;
}
Triplet operator+(const Triplet& input) const {
return Triplet(P::x + input.x, P::y + input.y, input.z + z);
}
Triplet operator-(const Triplet& input) const {
return Triplet(P::x - input.x, P::y - input.y, z - input.z);
}
Triplet& operator+=(const Triplet& in) {
P::x += in.x;
P::y += in.y;
z += in.z;
return *this;
}
Triplet& operator=(const Triplet& in) {
P::x = in.x;
P::y = in.y;
z = in.z;
return *this;
}
};
typedef Triplet<double> dTriplet;
template<class Num>
std::ostream& operator<<(std::ostream &out, const Triplet<Num>& p) {
return out << "(" << p.x << "," << p.y << "," << p.z << ")";
}
#endif