-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_rect.cpp
116 lines (105 loc) · 2.56 KB
/
game_rect.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "game_rect.h"
#include <cassert>
#include <iostream>
#include <sstream>
game_rect::game_rect(
const game_coordinat& top_left,
const game_coordinat& bottom_right
) : m_top_left{top_left},
m_bottom_right{bottom_right}
{
}
game_coordinat get_center(const game_rect& r) noexcept
{
return game_coordinat(
(r.get_tl().get_x() + r.get_br().get_x()) / 2,
(r.get_tl().get_y() + r.get_br().get_y()) / 2
);
}
int get_height(const game_rect& r) noexcept
{
return r.get_br().get_y() - r.get_tl().get_y();
}
int get_width(const game_rect& r) noexcept
{
return r.get_br().get_x() - r.get_tl().get_x();
}
bool is_in(const game_coordinat& pos, const game_rect& r) noexcept
{
return pos.get_x() >= r.get_tl().get_x()
&& pos.get_x() <= r.get_br().get_x()
&& pos.get_y() >= r.get_tl().get_y()
&& pos.get_y() <= r.get_br().get_y()
;
}
void test_game_rect()
{
#ifndef NDEBUG
// get_width and get_height
{
// 012345678901
// 0 ............
// 1 ..+---|---+.
// 2 ..| | |.
// 3 ..----X----.
// 4 ..| | |.
// 5 ..+---|---+.
// 6 ............
const game_rect r(game_coordinat(2, 1), game_coordinat(10, 5));
assert(get_width(r) == 8); // The bottom-left is excluded?
assert(get_height(r) == 4); // The bottom-left is excluded?
}
// get_center
{
// 012345678901
// 0 ............
// 1 ..+---|---+.
// 2 ..| | |.
// 3 ..----X----.
// 4 ..| | |.
// 5 ..+---|---+.
// 6 ............
const game_rect r(game_coordinat(2, 1), game_coordinat(10, 5));
const auto c{get_center(r)};
assert(c.get_x() == 6);
assert(c.get_y() == 3);
}
// is_in
{
const game_rect r(
game_coordinat(10, 20),
game_coordinat(100, 200)
);
assert(is_in(game_coordinat(50, 60), r));
assert(!is_in(game_coordinat(0, 60), r));
assert(!is_in(game_coordinat(1000, 60), r));
assert(!is_in(game_coordinat(50, 0), r));
assert(!is_in(game_coordinat(60, 600), r));
}
// operator==
{
const game_rect a;
const game_rect b;
const game_rect c(game_coordinat(1.1, 2.2), game_coordinat(3.3, 4.4));
assert(a == b);
assert(!(a == c));
}
// operator<<
{
std::stringstream s;
s << game_rect();
assert(!s.str().empty());
}
#endif // DEBUG
}
bool operator==(const game_rect& lhs, const game_rect& rhs) noexcept
{
return lhs.get_tl() == rhs.get_tl()
&& lhs.get_br() == rhs.get_br()
;
}
std::ostream& operator<<(std::ostream& os, const game_rect& r) noexcept
{
os << "(" << r.get_tl() << "-" << r.get_br() << ")";
return os;
}