Point.h
1 /* Copyright (C) 2006-2015 Laurentiu-Gheorghe Crisan
2  Copyright (C) 2006-2015 Marc Boris Duerner
3  Copyright (C) 2010 Aloysius Indrayanto
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  As a special exception, you may use this file as part of a free
11  software library without restriction. Specifically, if other files
12  instantiate templates or use macros or inline functions from this
13  file, or you compile this file and link it with other files to
14  produce an executable, this file does not by itself cause the
15  resulting executable to be covered by the GNU General Public
16  License. This exception does not however invalidate any other
17  reasons why the executable file might be covered by the GNU Library
18  General Public License.
19 
20  This library is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  Lesser General Public License for more details.
24 
25  You should have received a copy of the GNU Lesser General Public
26  License along with this library; if not, write to the Free Software
27  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28  02110-1301 USA
29 */
30 
31 #ifndef PT_GFX_POINT_H
32 #define PT_GFX_POINT_H
33 
34 #include <Pt/Gfx/Api.h>
35 #include <Pt/Math.h>
36 #include <Pt/Types.h>
37 
38 namespace Pt {
39 
40 namespace Gfx {
41 
44 template<typename T>
45 class BasicPoint
46 {
47  public:
50  : _x(0), _y(0)
51  {}
52 
54  BasicPoint(T x, T y)
55  : _x(x), _y(y)
56  {}
57 
60  : _x(pt._x), _y(pt._y)
61  { }
62 
64  void set(T x_, T y_)
65  {
66  _x = x_;
67  _y = y_;
68  }
69 
71  void setX(T x_)
72  {_x = x_; }
73 
75  void setY(T y_)
76  {_y = y_; }
77 
79  T x() const
80  { return _x; }
81 
83  T y() const
84  { return _y; }
85 
87  const BasicPoint& addX(T x)
88  {
89  _x += x;
90  return *this;
91  }
92 
94  const BasicPoint& subX(T x)
95  {
96  _x -= x;
97  return *this;
98  }
100  const BasicPoint& addY(T y)
101  {
102  _y += y;
103  return *this;
104  }
105 
107  const BasicPoint& subY(T y)
108  {
109  _y -= y;
110  return *this;
111  }
112 
114  const BasicPoint& move(T dx, T dy)
115  {
116  _x += dx;
117  _y += dy;
118  return *this;
119  }
120 
122  template<typename T2>
123  T calcDistance(const BasicPoint<T2>& otherPoint) const
124  {
125  if (*this == otherPoint)
126  {
127  return 0;
128  }
129 
130  return (T)(hypot(this->x() - otherPoint.x(), this->y() - otherPoint.y()));
131  }
132 
133  const BasicPoint& operator=(const BasicPoint& pt)
134  {
135  _x = pt._x; _y = pt._y;
136  return *this;
137  }
138 
139  bool operator==(const BasicPoint& pt) const
140  { return (_x == pt._x && _y == pt._y); }
141 
142  bool operator!=(const BasicPoint& pt) const
143  { return (_x != pt._x || _y != pt._y); }
144 
145  bool operator>(const BasicPoint& pt) const
146  {
147  if ( _x < pt._x || _y < pt._y)
148  return false;
149 
150  return ( (*this) != pt );
151  }
152 
153  bool operator<(const BasicPoint& pt) const
154  {
155  if ( _x > pt._x || _y > pt._y )
156  return false;
157 
158  return ( pt != (*this) );
159  }
160 
161  inline const BasicPoint operator+=(const BasicPoint<T>& pt)
162  {
163  _x += pt.x();
164  _y += pt.y();
165  return *this;
166  }
167 
168  inline BasicPoint operator+(const BasicPoint<T>& pt) const
169  {
170  return BasicPoint( (_x+pt.x()), (_y+pt.y()) );
171  }
172 
173  inline const BasicPoint operator-=(const BasicPoint<T>& pt)
174  {
175  _x -= pt.x();
176  _y -= pt.y();
177  return *this;
178  }
179 
180  inline BasicPoint operator-(const BasicPoint<T>& pt) const
181  {
182  return BasicPoint( (_x-pt.x()), (_y-pt.y()) );
183  }
184 
185  inline BasicPoint operator*(const double factor) const
186  {
187  return BasicPoint( (T)(_x * factor), (T)(_y * factor) );
188  }
189 
190  protected:
191  T _x;
192  T _y;
193 };
194 
195 typedef BasicPoint<Pt::ssize_t> Point;
196 typedef BasicPoint<double> PointF;
197 
198 inline Point round(const PointF& r)
199 {
200  return Point( lround(r.x()),
201  lround(r.y()) );
202 }
203 
204 } // namespace
205 
206 } // namespace
207 
208 #endif
209 
double hypot(double x, double y)
Return the euclidean distance of the given values.
Definition: Math.h:275
Point with X ynd X coordinates.
Definition: Point.h:45
T x() const
Return the X component of the BasicPoint.
Definition: Point.h:79
BasicPoint(T x, T y)
Construct a BasicPoint of (x,y)
Definition: Point.h:54
T calcDistance(const BasicPoint< T2 > &otherPoint) const
Calculate distance between this BasicPoint and the given BasicPoint.
Definition: Point.h:123
const BasicPoint & addY(T y)
Increment the Y component of the BasicPoint by the given value.
Definition: Point.h:100
T y() const
Return the Y component of the BasicPoint.
Definition: Point.h:83
void setX(T x_)
Set the X component of the BasicPoint.
Definition: Point.h:71
Pt::int32_t lround(float x)
Rounds to nearest integer value.
Definition: Math.h:289
void setY(T y_)
Set the Y component of the BasicPoint.
Definition: Point.h:75
const BasicPoint & subY(T y)
Decrement the Y component of the BasicPoint by the given value.
Definition: Point.h:107
const BasicPoint & subX(T x)
Decrement the X component of the BasicPoint by the given value.
Definition: Point.h:94
BasicPoint(const BasicPoint &pt)
Construct a BasicPoint from another BasicPoint.
Definition: Point.h:59
const BasicPoint & move(T dx, T dy)
Move the BasicPoint as far as th given the X and Y distances.
Definition: Point.h:114
BasicPoint()
Construct a BasicPoint of (0,0)
Definition: Point.h:49
void set(T x_, T y_)
Set the X and Y components of the BasicPoint.
Definition: Point.h:64
const BasicPoint & addX(T x)
Increment the X component of the BasicPoint by the given value.
Definition: Point.h:87