Point2D.h
Go to the documentation of this file.
1 /*
2  * Point2D.h
3  *
4  * Created on: 24.07.2014
5  * Author: moritzl
6  */
7
8 #ifndef POINT2D_H_
9 #define POINT2D_H_
10
11 #include <vector>
12 #include <cinttypes>
13 #include <cassert>
14 #include <cmath>
15 #include <cstdint>
16 #include "../Globals.h"
17
18 namespace NetworKit {
19
20 //template<class T> class Point;
21 //
22 //template<class T>
23 //std::ostream& operator <<(std::ostream& out, Point<T>& point);
24
25
26
31 template<class T>
32 class Point2D {
33 protected:
34  T x;
35  T y;
37
38 public:
39  Point2D() {
40  x = 0;
41  y = 0;
42  indice = 0;
43  }
44  Point2D(T x, T y) {
45  this->x = x;
46  this->y = y;
47  this->indice = 0;
48  }
49  Point2D(T x, T y, index indice) {
50  this->x = x;
51  this->y = y;
52  this->indice = indice;
53  }
54
55  virtual ~Point2D() {}
56
57  count getDimensions() { return 2; }
58
59  T distance(const Point2D<T>& p) const;
60  T squaredDistance(const Point2D<T>& p) const;
61
62  Point2D& operator+=(const Point2D<T>& p);
63  Point2D& operator-=(const Point2D<T>& p);
64  Point2D& scale(const T factor);
65
66  Point2D operator-(const Point2D<T>& other);
67  Point2D operator+(const Point2D<T>& other);
68
69  T length() const;
70  T squaredLength() const;
71
72  T& operator[](const index i);
73  T getX() const;
74  T getY() const;
75  index getIndex() const;
76 };
77
78 template<class T>
79 T Point2D<T>::length() const {
80  return sqrt(x*x+y*y);
81 }
82
83 template<class T>
85  return x*x + y*y;
86 }
87
88 template<class T>
90  T diffx = p.x - x;
91  T diffy = p.y - y;
92  return diffx*diffx + diffy*diffy;
93 }
94
95 template<class T>
96 T Point2D<T>::distance(const Point2D<T>& p) const {
97  return sqrt(squaredDistance(p));
98 }
99
100 template<class T>
102  this->x += p.x;
103  this->y += p.y;
104  return *this;
105 }
106
107 template<class T>
109  this->x -= p.x;
110  this->y -= p.y;
111  return *this;
112 }
113
114 template<class T>
116  return Point2D(x - other.x, y - other.y);
117 }
118
119 template<class T>
121  return Point2D(x + other.x, y + other.y);
122 }
123
124
125 template<class T>
126 Point2D<T>& Point2D<T>::scale(const T factor) {
127  x *= factor;
128  y *= factor;
129  return *this;
130 }
131
132 template<class T>
134  assert(i >= 0 && i < 2);
135  if (i == 0) return x;
136  else return y;
137 }
138
139 template<class T>
140 inline T Point2D<T>::getX() const {
141  return x;
142 }
143
144 template<class T>
145 inline T Point2D<T>::getY() const {
146  return y;
147 }
148
149 template<class T>
150 inline index Point2D<T>::getIndex() const {
151  return indice;
152 }
153
154 } /* namespace NetworKit */
155 #endif /* POINT2D_H_ */
Point2D()
Definition: Point2D.h:39
Point2D operator+(const Point2D< T > &other)
Definition: Point2D.h:120
T squaredLength() const
Definition: Point2D.h:84
T getX() const
Definition: Point2D.h:140
uint64_t index
Typedefs.
Definition: Globals.h:20
Points in any dimension of templated type.
Definition: Point2D.h:32
T distance(const Point2D< T > &p) const
Definition: Point2D.h:96
T squaredDistance(const Point2D< T > &p) const
Definition: Point2D.h:89
Point2D(T x, T y, index indice)
Definition: Point2D.h:49
T & operator[](const index i)
Definition: Point2D.h:133
Point2D operator-(const Point2D< T > &other)
Definition: Point2D.h:115
count getDimensions()
Definition: Point2D.h:57
T y
Definition: Point2D.h:35
Point2D & scale(const T factor)
Definition: Point2D.h:126
uint64_t count
Definition: Globals.h:21
Point2D & operator+=(const Point2D< T > &p)
Definition: Point2D.h:101
T length() const
Definition: Point2D.h:79
Point2D & operator-=(const Point2D< T > &p)
Definition: Point2D.h:108
index indice
Definition: Point2D.h:36
Point2D(T x, T y)
Definition: Point2D.h:44
T x
Definition: Point2D.h:34
T getY() const
Definition: Point2D.h:145
virtual ~Point2D()
Definition: Point2D.h:55
index getIndex() const
Definition: Point2D.h:150