MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Position2D.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_TYPES_POSITION2D_HPP_
24 #define SRC_TYPES_POSITION2D_HPP_
25 
26 #include <types/Action2D.hpp>
27 
28 namespace mic {
29 namespace types {
30 
35 class Position2D {
36 public:
37 
39  long x;
40 
42  long y;
43 
47  Position2D() : x(0), y(0) { };
48 
52  Position2D(long x_, long y_) : x(x_), y(y_) { };
53 
61  Position2D(size_t min_x_, size_t max_x_, size_t min_y_, size_t max_y_) {
62  rand(min_x_, max_x_, min_y_, max_y_);
63  }
64 
72  void rand(size_t min_x_, size_t max_x_, size_t min_y_, size_t max_y_) {
73  // Initialize device and generator.
74  std::random_device rd;
75  std::mt19937_64 rng_mt19937_64(rd());
76 
77  // Initialize uniform integer distribution for x
78  std::uniform_int_distribution<size_t> x_dist(min_x_, max_x_);
79 
80  // Select a random action.
81  x = x_dist(rng_mt19937_64);
82 
83  // Initialize uniform integer distribution for x
84  std::uniform_int_distribution<size_t> y_dist(min_y_, max_y_);
85 
86  // Select a random action.
87  y = y_dist(rng_mt19937_64);
88  }
89 
90 
96  void set(long x_, long y_) {
97  x = x_;
98  y = y_;
99  }
100 
107  mic::types::Position2D new_pos;
108  new_pos.x = this->x + ac_.dx;
109  new_pos.y = this->y + ac_.dy;
110  return new_pos;
111  }
112 
119  mic::types::Position2D new_pos;
120  if ((pos_.x == this->x) && (pos_.y == this->y))
121  return true;
122  else
123  return false;
124  }
125 
132  x += ac_.dx;
133  y += ac_.dy;
134  return true;
135  }
136 
147  bool move(Action2DInterface ac_, size_t width_, size_t height_, bool circular_world_ = true) {
148  if (circular_world_) {
149  // Execute the "circular" move.
150  x = (x + width_ + ac_.dx) % width_;
151  y = (y + height_ + ac_.dy) % height_;
152  return true;
153  } else {
154  // Execute the "truncated move.
155  if (((x+ac_.dx) < 0) || ((x+ac_.dx) >= (long)width_))
156  return false;
157 
158  if (((y+ac_.dy) < 0) || ((y+ac_.dy) >= (long)height_))
159  return false;
160 
161  x += ac_.dx;
162  y += ac_.dy;
163 
164  return true;
165  }//: else
166  }
167 
174  friend std::ostream& operator<<(std::ostream& os_, const Position2D& pos_)
175  {
176  os_ << "[x,y]: [" << pos_.x << ',' << pos_.y << "]";
177  return os_;
178  }
179 };
180 
181 
182 
183 } /* namespace types */
184 } /* namespace mic */
185 
186 #endif /* SRC_TYPES_POSITION2D_HPP_ */
Class representing position in 2-D space.
Definition: Position2D.hpp:35
void set(long x_, long y_)
Definition: Position2D.hpp:96
int dy
Increment according to y axis.
Definition: Action2D.hpp:59
long x
The x coordinate.
Definition: Position2D.hpp:39
mic::types::Position2D operator+(Action2DInterface ac_)
Definition: Position2D.hpp:106
int dx
Increment according to x axis.
Definition: Action2D.hpp:56
bool operator==(mic::types::Position2D pos_)
Definition: Position2D.hpp:118
friend std::ostream & operator<<(std::ostream &os_, const Position2D &pos_)
Definition: Position2D.hpp:174
long y
The y coordinate.
Definition: Position2D.hpp:42
Position2D(size_t min_x_, size_t max_x_, size_t min_y_, size_t max_y_)
Definition: Position2D.hpp:61
void rand(size_t min_x_, size_t max_x_, size_t min_y_, size_t max_y_)
Definition: Position2D.hpp:72
Interface class representing an action in 2-D space.
Definition: Action2D.hpp:52
bool move(Action2DInterface ac_, size_t width_, size_t height_, bool circular_world_=true)
Definition: Position2D.hpp:147
bool move(Action2DInterface ac_)
Definition: Position2D.hpp:131
Position2D(long x_, long y_)
Definition: Position2D.hpp:52