fork download
  1. #include "Point.h"
  2. //#include "Rect.h"
  3.  
  4. //#include "Common/String/Conversion.h"
  5. //#include "Common/System/Comparison.h"
  6.  
  7. //#include SFML_HEADER("SFML/System/Vector2.hpp")
  8. //#include QT_HEADER("QtCore/QPoint")
  9.  
  10. Point::Point(const std::string &str)
  11. {
  12. this->FromString(str);
  13. }
  14.  
  15. //Copy constructor.
  16. Point::Point(const Point &point)
  17. {
  18. *this = point;
  19. }
  20.  
  21. /*
  22. //Keeps the point within 'rect'.
  23. void Point::KeepWithin(const Rect &rect)
  24. {
  25.   this->x = ::KeepWithin(rect.Left(), this->x, rect.Right());
  26.   this->y = ::KeepWithin(rect.Top(), this->y, rect.Bottom());
  27. }
  28.  
  29. //Keeps the point outside of 'rect'.
  30. void Point::KeepWithout(const Rect &rect)
  31. {
  32.   this->x = ::KeepWithout(rect.Left(), this->x, rect.Right());
  33.   this->y = ::KeepWithout(rect.Top(), this->y, rect.Bottom());
  34. }
  35.  
  36. //Snaps the point to the nearest edge of 'rect', regardless of
  37. //whether the point is inside or outside the rectangle.
  38. void Point::SnapToEdge(const Rect &rect)
  39. {
  40.   this->KeepWithin(rect);
  41.   this->KeepWithout(rect);
  42. }
  43.  
  44. //STATIC:
  45. //Returns 'true' if 'value.x' is greater than 'min.x' and less than 'max.x', and the same for the 'y' member.
  46. bool Point::IsWithin(const Point &min, const Point &value, const Point &max)
  47. {
  48.   return ((min.x <= value.x && value.x < max.x)
  49.   && (min.y <= value.y && value.y < max.y));
  50. }
  51.  
  52. bool Point::IsWithin(const Point &value, const Point &max)
  53. {
  54.   return Point::IsWithin(Point(0,0), value, max);
  55. }
  56.  
  57. //STATIC:
  58. //Keeps 'value.x' between 'min.x' and 'max.x', with 'loops.x' being the number of times it had to loop around.
  59. //Does the same with the 'y' member. Negative loops return a negative number.
  60. Point Point::LoopInRange(const Point &min, const Point &value, const Point &max, Point &loops)
  61. {
  62.   //int *loopX = (loops? &loops->x : nullptr);
  63.   //int *loopY = (loops? &loops->y : nullptr);
  64.  
  65.   Point result;
  66.   result.x = ::LoopInRange(min.x, value.x, max.x, &loops.x);
  67.   result.y = ::LoopInRange(min.y, value.y, max.y, &loops.y);
  68.  
  69.   return result;
  70. }
  71.  
  72. //STATIC:
  73. Point Point::LoopInRange(const Point &value, const Point &max, Point &loops)
  74. {
  75.   return Point::LoopInRange(Point(0,0), value, max, loops);
  76. }*/
  77.  
  78. bool Point::operator==(const Point &other) const
  79. {
  80. return (this->x == other.x && this->y == other.y);
  81. }
  82.  
  83. bool Point::operator!=(const Point &other) const
  84. {
  85. return !(*this == other);
  86. }
  87.  
  88. Point &Point::operator=(const Point &other)
  89. {
  90. if(this == &other) return *this; //Self-assignment protection.
  91.  
  92. this->x = other.x;
  93. this->y = other.y;
  94.  
  95. return *this;
  96. }
  97.  
  98. Point &Point::operator+=(const Point &other)
  99. {
  100. this->x += other.x;
  101. this->y += other.y;
  102.  
  103. return *this;
  104. }
  105.  
  106. Point &Point::operator-=(const Point &other)
  107. {
  108. this->x -= other.x;
  109. this->y -= other.y;
  110.  
  111. return *this;
  112. }
  113.  
  114. Point &Point::operator*=(const Point &other)
  115. {
  116. this->x *= other.x;
  117. this->y *= other.y;
  118.  
  119. return *this;
  120. }
  121.  
  122. Point &Point::operator/=(const Point &other)
  123. {
  124. this->x /= other.x;
  125. this->y /= other.y;
  126.  
  127. return *this;
  128. }
  129.  
  130. Point &Point::operator%=(const Point &other)
  131. {
  132. this->x %= other.x;
  133. this->y %= other.y;
  134.  
  135. return *this;
  136. }
  137.  
  138. Point Point::operator+(const Point &other) const
  139. {
  140. Point point(*this);
  141. point += other;
  142.  
  143. return point;
  144. }
  145.  
  146. Point Point::operator-(const Point &other) const
  147. {
  148. Point point(*this);
  149. point -= other;
  150.  
  151. return point;
  152. }
  153.  
  154. Point Point::operator*(const Point &other) const
  155. {
  156. Point point(*this);
  157. point *= other;
  158.  
  159. return point;
  160. }
  161.  
  162. Point Point::operator/(const Point &other) const
  163. {
  164. Point point(*this);
  165. point /= other;
  166.  
  167. return point;
  168. }
  169.  
  170. Point Point::operator%(const Point &other) const
  171. {
  172. Point point(*this);
  173. point %= other;
  174.  
  175. return point;
  176. }
  177.  
  178. //Additive-inverse operator.
  179. Point Point::operator-() const
  180. {
  181. return Point(-(this->x), -(this->y));
  182. }
  183.  
  184. /*
  185. //Packs the Point into a Uint32, with 16 bits for x, and 16 for y. Since both x and y can be negative,
  186. //this leaves just 15 bits for the numeral component, meaning (-32768 to 32768) in both x and y.
  187. uint32_t Point::ToUint32() const
  188. {
  189.   uint32_t data = 0;
  190.   data |= (((int16_t)this->x) & 0x0000FFFF);
  191.   data |= (((int16_t)this->x) & 0x0000FFFF) << 15;
  192.  
  193.   return data;
  194. }
  195.  
  196. void Point::FromUint32(uint32_t data)
  197. {
  198.   this->x = (int16_t)((data & 0x0000FFFF) );
  199.   this->y = (int16_t)((data & 0xFFFF0000) >> 15);
  200. }
  201.  
  202. std::string Point::ToString() const
  203. {
  204.   return PointToString(*this);
  205. }
  206.  
  207. void Point::FromString(const std::string &str)
  208. {
  209.   *this = StringToPoint(str);
  210. }
  211.  
  212. SFML_ONLY
  213. (
  214.   sf::Vector2f Point::ToSfmlVector2f() const
  215.   {
  216.   return sf::Vector2f(float(this->x), float(this->y));
  217.   }
  218.  
  219.   void Point::FromSfmlVector2f(sf::Vector2f vector)
  220.   {
  221.   this->x = value_type(vector.x);
  222.   this->y = value_type(vector.y);
  223.   }
  224. )
  225.  
  226. QT_ONLY
  227. (
  228.   QPoint Point::ToQPoint() const
  229.   {
  230.   return QPoint(this->x, this->y);
  231.   }
  232.  
  233.   void Point::FromQPoint(QPoint point)
  234.   {
  235.   this->x = point.x();
  236.   this->y = point.y();
  237.   }
  238. )*/
  239.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty