-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpoint.h
73 lines (55 loc) · 1.61 KB
/
tpoint.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#pragma once
struct TPoint
{
public:
#define maxDeviation 5
int16_t x;
int16_t y;
TPoint snap(const uint8_t& grid) {
int16_t new_x = x + grid / 2;
int16_t new_y = y + grid / 2;
TPoint ret;
ret.x = new_x - new_x % grid;
ret.y = new_y - new_y % grid;
return ret;
};
/* something wrong
TPoint within(const int16_t& x0, const int16_t& x1, const int16_t& y0, const int16_t& y1) {
TPoint ret;
ret.x = x;
ret.y = y;
if (ret.x < x0) ret.x = x0;
if (ret.y < y0) ret.y = y0;
if (ret.x > x1) ret.x = x1;
if (ret.y > y1) ret.y = y1;
return ret;
};*/
friend bool operator==(const TPoint& value1, const TPoint& value2) {
return abs(value1.x - value2.x) <= maxDeviation && abs(value1.y - value2.y) <= maxDeviation; // remark: Tpoint == operator has tolerance
}
friend bool operator!=(const TPoint& value1, const TPoint& value2) {
return !(value1 == value2);
}
friend TPoint operator+(const TPoint& value1, const TPoint& value2) {
TPoint sum;
sum.x = value1.x + value2.x;
sum.y = value1.y + value2.y;
return sum;
}
friend TPoint& operator+=(TPoint& value1, const TPoint& value2) {
value1.x += value2.x;
value1.y += value2.y;
return value1;
}
friend TPoint operator-(const TPoint& value1, const TPoint& value2) {
TPoint sub;
sub.x = value1.x - value2.x;
sub.y = value1.y - value2.y;
return sub;
}
friend TPoint& operator-=(TPoint& value1, const TPoint& value2) {
value1.x -= value2.x;
value1.y -= value2.y;
return value1;
}
};