CoordT.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef COORDT_H
  2. #define COORDT_H
  3. //STL
  4. #include <ostream>
  5. //core
  6. #include "core/vector/VectorT.h"
  7. namespace NICE {
  8. /**
  9. * A simple 2D coordinate.
  10. *
  11. * @author Frank Mattern
  12. */
  13. template<class T>
  14. class CoordT {
  15. public:
  16. T x;
  17. T y;
  18. public:
  19. /**
  20. * Create an zero coordinate.
  21. */
  22. CoordT() {
  23. x = 0;
  24. y = 0;
  25. }
  26. /**
  27. * Create a coordinate with given parameters.
  28. * @param _x
  29. * @param _y
  30. */
  31. CoordT(const T _x, const T _y) {
  32. x = _x;
  33. y = _y;
  34. }
  35. /**
  36. * Copy-constructor.
  37. * @param other Original coordinate.
  38. */
  39. CoordT(const CoordT<T>& other) {
  40. x = other.x;
  41. y = other.y;
  42. }
  43. ~CoordT() {
  44. }
  45. //! Assignment operator
  46. /*! \param ex class to copy
  47. * \return a reference to this class
  48. */
  49. CoordT<T>& operator=(const CoordT<T>& ex)
  50. {
  51. x=ex.x;
  52. y=ex.y;
  53. return *this;
  54. }
  55. //! Compare operator
  56. /*! \param ex class to compare
  57. * \return true if class content is equal
  58. */
  59. bool operator==(const CoordT<T>& ex) const
  60. {
  61. if(ex.x==x && ex.y == y)
  62. return true;
  63. else
  64. return false;
  65. }
  66. CoordT<T> &operator+=(const CoordT<T>& ex)
  67. {
  68. x += ex.x;
  69. y += ex.y;
  70. return *this;
  71. }
  72. CoordT<T> &operator-=(const CoordT<T>& ex)
  73. {
  74. x -= ex.x;
  75. y -= ex.y;
  76. return *this;
  77. }
  78. CoordT<T> &operator*=(const T& v)
  79. {
  80. x *= v;
  81. y *= v;
  82. return *this;
  83. }
  84. CoordT<T> &operator/=(const T& v)
  85. {
  86. x /= v;
  87. y /= v;
  88. return *this;
  89. }
  90. CoordT<T> operator+(const CoordT<T>& ex) const
  91. {
  92. CoordT<T> result(x,y);
  93. result+=ex;
  94. return result;
  95. }
  96. CoordT<T> operator-(const CoordT<T>& ex) const
  97. {
  98. CoordT<T> result(x,y);
  99. result-=ex;
  100. return result;
  101. }
  102. CoordT<T> operator*(const T& v) const
  103. {
  104. CoordT<T> result(x,y);
  105. result*=v;
  106. return result;
  107. }
  108. CoordT<T> operator/(const T& v) const
  109. {
  110. CoordT<T> result(x,y);
  111. result/=v;
  112. return result;
  113. }
  114. NICE::VectorT<T> convertToNiceVector () const
  115. {
  116. //allocate memory
  117. NICE::VectorT<T> vec ( 2 /*size*/, 0 /* initial values*/ );
  118. //copy data
  119. vec[0] = this->x;
  120. vec[1] = this->y;
  121. return vec;
  122. }
  123. };
  124. template<class T>
  125. inline std::ostream& operator<< (std::ostream& out, const CoordT<T>& coord) {
  126. out << coord.x << " " << coord.y;
  127. return out;
  128. }
  129. typedef CoordT<int> Coord;
  130. } // namespace
  131. #endif // COORDT_H