-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.cpp
70 lines (53 loc) · 1.38 KB
/
Ball.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
#include <math.h>
#include <cstdlib>
#include "Ball.hpp"
#include "State.hpp"
Ball::Ball()
{}
Ball::Ball(int x, int y):
x(x), y(y)
{}
Ball Ball::operator+ (const Ball &sec) const {
return Ball(x + sec.x, y + sec.y);
}
void Ball::operator+=(const Ball &sec) {
this->x += sec.x;
this->y += sec.y;
}
bool Ball::operator== (const Ball &sec) const {
return (x == sec.x) && (y == sec.y);
}
bool Ball::operator!= (const Ball &sec) const {
return !(*this == sec);
}
Ball Ball::operator- () const {
return Ball(-x, -y);
}
Ball Ball::operator- (const Ball &sec) const {
return Ball(x - sec.x, y - sec.y);
}
bool Ball::operator< (const Ball &sec) const {
return (x != sec.x) ? (x < sec.x) : (y < sec.y);
}
double Ball::degree() const {
return atan2(y, x) / M_PI * 180;
}
const bool Ball::is_valid_step() const {
return std::abs(x) + std::abs(y) == 1;
}
const bool Ball::is_valid_position() const {
return 0 <= x < WIDTH && 0 <= y < HEIGHT;
}
std::ostream &operator<< (std::ostream &os, const Ball &B) {
os << '(' << B.x << ", " << B.y << ')';
return os;
}
bool Ball::InSegment(const Ball &spot, const std::vector <Ball> &objects) {
return InSegment(spot, objects, Ball(0, objects.size()));
}
bool Ball::InSegment(const Ball &spot, const std::vector <Ball> &objects, const Ball &interval) {
for(size_t i = interval.x; i < interval.y; ++i)
if(spot == objects[i])
return 1;
return 0;
}