-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinate.cc
80 lines (62 loc) · 2.04 KB
/
coordinate.cc
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
#include "tools.cc"
#ifndef _KROB_COORD
#define _KROB_COORD
// Your average 2D Cartesian coordinate.
class coordinate {
public:
coordinate() {x = 0; y = 0; }
coordinate(double xin, double yin) {x = xin; y = yin; }
double x;
double y;
// Euclidean distance.
double distance(const coordinate & other) const;
double sq_distance(const coordinate & other) const;
// clamp_min and clamp_max clamps values less than the
// coordinate values on other, or greater than, depending
// on _min or _max.
void clamp_min(const coordinate & other);
void clamp_max(const coordinate & other);
bool cisnan() { return (isnan(x) || isnan(y)); }
// Operator!
coordinate operator- (const coordinate p) const;
coordinate operator+ (const coordinate p) const;
coordinate operator/ (const coordinate p) const;
coordinate operator* (const coordinate p) const;
bool operator== (const coordinate p) const;
bool operator!= (const coordinate p) const;
};
double coordinate::distance(const coordinate & other) const {
return(euc_distance(x, y, other.x, other.y));
}
double coordinate::sq_distance(const coordinate & other) const {
return(euc_sq_distance(x, y, other.x, other.y));
}
void coordinate::clamp_min(const coordinate & other) {
// These are equivalent to if (x < other.x) x = other.x (and similar
// for .y).
x = max(x, other.x);
y = max(y, other.y);
}
void coordinate::clamp_max(const coordinate & other) {
x = min(x, other.x);
y = min(y, other.y);
}
coordinate coordinate::operator- (const coordinate p) const {
return(coordinate(x - p.x, y - p.y));
}
coordinate coordinate::operator+ (const coordinate p) const {
return(coordinate(x + p.x, y + p.y));
}
coordinate coordinate::operator/ (const coordinate p) const {
return (coordinate(x/p.x, y/p.y));
}
coordinate coordinate::operator* (const coordinate p) const {
return(coordinate(x * p.x, y * p.y));
}
bool coordinate::operator== (const coordinate p) const {
return(x == p.x && y == p.y);
}
bool coordinate::operator!= (const coordinate p) const {
return(x != p.x || y != p.y);
}
#endif