CoordT.h 2.1 KB

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