Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Point.hxx
Go to the documentation of this file.
1 #ifndef POINT_HXX_
2 #define POINT_HXX_
3 
4 #include "Settings.hxx"
5 #include <cmath>
6 
7 class Point
8 {
9 public:
10  constexpr Point() : x(0), y(0), z(0), height(0), rawHeight(0){};
11  constexpr Point(int x, int y) : x(x), y(y), z(0), height(0), rawHeight(0){};
12  constexpr Point(int x, int y, int z) : x(x), y(y), z(z), height(0), rawHeight(0){};
13  constexpr Point(int x, int y, int z, int height) : x(x), y(y), z(z), height(height), rawHeight(0){};
14  constexpr Point(int x, int y, int z, int height, double rawHeight) : x(x), y(y), z(z), height(height), rawHeight(rawHeight){};
15 
17  int x;
18 
20  int y;
21 
23  int z;
24 
26  int height;
27 
29  double rawHeight;
30 
31  //TODO: need to add z axis in the future, currently it is not supported.
32  bool operator==(const Point &p) const { return x == p.x && y == p.y /*&& z == p.z*/; }
33  bool operator!=(const Point &p) const { return !(*this == p); }
34 
35  static constexpr Point INVALID() { return {-1, -1, -1, -1}; }
36 
37  bool isWithinMapBoundaries() const
38  {
39  return (x >= 0 && x < Settings::instance().mapSize) && (y >= 0 && y < Settings::instance().mapSize);
40  }
41 
47  bool isNeighborOf(Point coordinate) const
48  {
49  return isWithinMapBoundaries() && std::max(std::abs(coordinate.x - x), std::abs(coordinate.y - y)) <= 1;
50  }
51 
57  bool isDirectNeighborOf(Point coordinate) const
58  {
59  return ((x == coordinate.x) && (std::abs(y - coordinate.y) <= 1)) ||
60  ((y == coordinate.y) && (std::abs(x - coordinate.x) <= 1));
61  }
62 
68  int manhattanDistanceTo(Point target) const { return abs(x - target.x) + abs(y - target.y); }
69 
75  int distanceTo(Point target) const { return sqrt((x - target.x) * (x - target.x) + (y - target.y) * (y - target.y)); }
76 
80  int toIndex() const { return x * Settings::instance().mapSize + y; };
81 };
82 
83 namespace std
84 {
85 template <> struct hash<Point>
86 {
87  const size_t operator()(const Point &p) const { return std::hash<int>()(p.x) ^ std::hash<int>()(p.y); }
88 };
89 } // namespace std
90 
91 #endif
Point::Point
constexpr Point()
Definition: Point.hxx:10
Point::isNeighborOf
bool isNeighborOf(Point coordinate) const
Checks if a given point is a neighbor of this point.
Definition: Point.hxx:47
Point::manhattanDistanceTo
int manhattanDistanceTo(Point target) const
Calculate the manhattan distance between this point and a given point.
Definition: Point.hxx:68
Point::toIndex
int toIndex() const
calculates the index (stride) that can be used to access in Map to access mapNodes vector.
Definition: Point.hxx:80
Point::Point
constexpr Point(int x, int y)
Definition: Point.hxx:11
std::hash< Point >::operator()
const size_t operator()(const Point &p) const
Definition: Point.hxx:87
Point::operator==
bool operator==(const Point &p) const
Definition: Point.hxx:32
Point::Point
constexpr Point(int x, int y, int z)
Definition: Point.hxx:12
Point::z
int z
The z coordinate.
Definition: Point.hxx:23
Point::operator!=
bool operator!=(const Point &p) const
Definition: Point.hxx:33
Point::y
int y
The y coordinate.
Definition: Point.hxx:20
SettingsData::mapSize
int mapSize
the size of the map
Definition: Settings.hxx:34
Point::x
int x
The x coordinate.
Definition: Point.hxx:14
Point::isWithinMapBoundaries
bool isWithinMapBoundaries() const
Definition: Point.hxx:37
Point::INVALID
static constexpr Point INVALID()
Definition: Point.hxx:35
Settings.hxx
Point::Point
constexpr Point(int x, int y, int z, int height)
Definition: Point.hxx:13
Point::height
int height
The height level.
Definition: Point.hxx:26
std
Definition: Point.hxx:83
Point
Definition: Point.hxx:7
Singleton< Settings >::instance
static Settings & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
Point::distanceTo
int distanceTo(Point target) const
Calculate the direct distance between this point and a given point.
Definition: Point.hxx:75
Point::isDirectNeighborOf
bool isDirectNeighborOf(Point coordinate) const
Check if a given point is a direct neighbor (cardinal direction) of this point.
Definition: Point.hxx:57
Point::rawHeight
double rawHeight
The raw height.
Definition: Point.hxx:29