-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinates.cpp
More file actions
37 lines (32 loc) · 1.16 KB
/
Copy pathcoordinates.cpp
File metadata and controls
37 lines (32 loc) · 1.16 KB
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
#include "coordinates.h"
constexpr CoordType Coordinates::kInvalidValue;
constexpr CoordType Coordinates::kMaxPossibleValue;
constexpr std::array<CoordType, 4> Coordinates::kXShift;
constexpr std::array<CoordType, 4> Coordinates::kYShift;
Coordinates::Coordinates(size_t x, size_t y)
{
assert(x < kMaxPossibleValue && y < kMaxPossibleValue);
this->x = static_cast<CoordType>(x);
this->y = static_cast<CoordType>(y);
}
Coordinates Coordinates::shift(Direction direction, CoordType distance) const
{
return Coordinates(x + xShift(direction) * distance,
y + yShift(direction) * distance);
}
CoordinatesDiff Coordinates::diff(const Coordinates &other) const
{
auto xdiff = other.x - this->x;
auto ydiff = other.y - this->y;
Direction xdir = xdiff > 0 ? Direction::East : Direction::West;
Direction ydir = ydiff > 0 ? Direction::South : Direction::North;
xdiff = abs(xdiff);
ydiff = abs(ydiff);
return CoordinatesDiff(xdiff, ydiff, xdir, ydir);
}
CoordinatesDiff::CoordinatesDiff(CoordType xdiff, CoordType ydiff, Direction xdir, Direction ydir)
: xdiff(xdiff)
, ydiff(ydiff)
, xdir(xdir)
, ydir(ydir)
{}