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 
42 class PointI;
43 
46 class Point
47 {
48  public:
49  Point()
50  : _x(0), _y(0)
51  {}
52 
53  Point(Float x, Float y)
54  : _x(x), _y(y)
55  {}
56 
57  Point(const Point& pt)
58  : _x(pt._x), _y(pt._y)
59  {}
60 
61  void clear()
62  {
63  _x = 0;
64  _y = 0;
65  }
66 
67  void set(Float xpos, Float ypos)
68  {
69  _x = xpos;
70  _y = ypos;
71  }
72 
73  void setX(Float xpos)
74  { _x = xpos; }
75 
76  void setY(Float ypos)
77  { _y = ypos; }
78 
79  Float x() const
80  { return _x; }
81 
82  Float y() const
83  { return _y; }
84 
85  const Point& addX(Float x)
86  {
87  _x += x;
88  return *this;
89  }
90 
91  const Point& subX(Float x)
92  {
93  _x -= x;
94  return *this;
95  }
96 
97  const Point& addY(Float y)
98  {
99  _y += y;
100  return *this;
101  }
102 
103  const Point& subY(Float y)
104  {
105  _y -= y;
106  return *this;
107  }
108 
109  const Point& move(Float dx, Float dy)
110  {
111  _x += dx;
112  _y += dy;
113  return *this;
114  }
115 
116  Float distanceTo(const Point& other) const
117  {
118  if(isEqual(other))
119  return 0;
120 
121  return hypot(_x - other._x, _y - other._y);
122  }
123 
124  Point& operator=(const Point& pt)
125  {
126  _x = pt._x;
127  _y = pt._y;
128  return *this;
129  }
130 
131  bool isEqual(const Point& other, Float eps = FloatNearlyZero) const
132  {
133  return std::abs(_x - other._x) <= eps &&
134  std::abs(_y - other._y) <= eps;
135  }
136 
137  Point& operator+=(const Point& pt)
138  {
139  _x += pt._x;
140  _y += pt._y;
141  return *this;
142  }
143 
144  Point operator+(const Point& pt) const
145  {
146  return Point(_x + pt._x, _y + pt._y);
147  }
148 
149  Point& operator-=(const Point& pt)
150  {
151  _x -= pt._x;
152  _y -= pt._y;
153  return *this;
154  }
155 
156  Point operator-(const Point& pt) const
157  {
158  return Point(_x - pt._x, _y - pt._y);
159  }
160 
161  Point operator*(Float factor) const
162  {
163  return Point(_x * factor, _y * factor);
164  }
165 
166  Point operator/(Float factor) const
167  {
168  return Point(_x / factor, _y / factor);
169  }
170 
171  Point operator+(Float factor) const
172  {
173  return Point(_x + factor, _y + factor);
174  }
175 
176  Point operator-(Float factor) const
177  {
178  return Point(_x - factor, _y - factor);
179  }
180 
181  Point& operator*=(Float factor)
182  {
183  _x *= factor;
184  _y *= factor;
185  return *this;
186  }
187 
188  Point& operator/=(Float factor)
189  {
190  _x /= factor;
191  _y /= factor;
192  return *this;
193  }
194 
197  bool isOrigin() const
198  { return _x == 0 && _y == 0; }
199 
202  Point operator-() const
203  { return Point(-_x, -_y); }
204 
207  Float length() const
208  { return hypot(_x, _y); }
209 
212  Float lengthSquared() const
213  { return _x * _x + _y * _y; }
214 
218  {
219  const Float len = this->length();
220  if(len == 0)
221  return Point();
222  return Point(_x / len, _y / len);
223  }
224 
227  explicit Point(const PointI& pt);
228 
231  Point& operator=(const PointI& pt);
232 
235  PointI round() const;
236 
239  PointI floor() const;
240 
243  PointI ceil() const;
244 
245  private:
246  Float _x;
247  Float _y;
248 };
249 
250 typedef Point PointF;
251 
254 class PointI
255 {
256  public:
257  PointI()
258  : _x(0), _y(0)
259  {}
260 
261  PointI(Int x, Int y)
262  : _x(x), _y(y)
263  {}
264 
265  PointI(const PointI& pt)
266  : _x(pt._x), _y(pt._y)
267  {}
268 
269  void clear()
270  {
271  _x = 0;
272  _y = 0;
273  }
274 
275  void set(Int xpos, Int ypos)
276  {
277  _x = xpos;
278  _y = ypos;
279  }
280 
281  void setX(Int xpos)
282  { _x = xpos; }
283 
284  void setY(Int ypos)
285  { _y = ypos; }
286 
287  Int x() const
288  { return _x; }
289 
290  Int y() const
291  { return _y; }
292 
293  const PointI& addX(Int x)
294  {
295  _x += x;
296  return *this;
297  }
298 
299  const PointI& subX(Int x)
300  {
301  _x -= x;
302  return *this;
303  }
304 
305  const PointI& addY(Int y)
306  {
307  _y += y;
308  return *this;
309  }
310 
311  const PointI& subY(Int y)
312  {
313  _y -= y;
314  return *this;
315  }
316 
317  const PointI& move(Int dx, Int dy)
318  {
319  _x += dx;
320  _y += dy;
321  return *this;
322  }
323 
324  PointI& operator=(const PointI& pt)
325  {
326  _x = pt._x;
327  _y = pt._y;
328  return *this;
329  }
330 
331  bool operator==(const PointI& pt) const
332  { return (_x == pt._x && _y == pt._y); }
333 
334  bool operator!=(const PointI& pt) const
335  { return (_x != pt._x || _y != pt._y); }
336 
337  PointI& operator+=(const PointI& pt)
338  {
339  _x += pt._x;
340  _y += pt._y;
341  return *this;
342  }
343 
344  PointI operator+(const PointI& pt) const
345  {
346  return PointI(_x + pt._x, _y + pt._y);
347  }
348 
349  PointI& operator-=(const PointI& pt)
350  {
351  _x -= pt._x;
352  _y -= pt._y;
353  return *this;
354  }
355 
356  PointI operator-(const PointI& pt) const
357  {
358  return PointI(_x - pt._x, _y - pt._y);
359  }
360 
361  PointI operator*(Int factor) const
362  {
363  return PointI(_x * factor, _y * factor);
364  }
365 
366  PointI operator+(Int offset) const
367  {
368  return PointI(_x + offset, _y + offset);
369  }
370 
371  PointI operator-(Int offset) const
372  {
373  return PointI(_x - offset, _y - offset);
374  }
375 
376  PointI& operator*=(Int factor)
377  {
378  _x *= factor;
379  _y *= factor;
380  return *this;
381  }
382 
385  bool isOrigin() const
386  { return _x == 0 && _y == 0; }
387 
391  { return PointI(-_x, -_y); }
392 
393  private:
394  Int _x;
395  Int _y;
396 };
397 
398 inline Point::Point(const PointI& pt)
399 : _x(static_cast<Float>(pt.x())), _y(static_cast<Float>(pt.y()))
400 {}
401 
402 inline Point& Point::operator=(const PointI& pt)
403 {
404  _x = static_cast<Float>(pt.x());
405  _y = static_cast<Float>(pt.y());
406  return *this;
407 }
408 
409 inline PointI Point::round() const
410 {
411  return PointI(static_cast<Int>(std::lround(_x)),
412  static_cast<Int>(std::lround(_y)));
413 }
414 
415 inline PointI Point::floor() const
416 {
417  return PointI(static_cast<Int>(std::floor(_x)),
418  static_cast<Int>(std::floor(_y)));
419 }
420 
421 inline PointI Point::ceil() const
422 {
423  return PointI(static_cast<Int>(std::ceil(_x)),
424  static_cast<Int>(std::ceil(_y)));
425 }
426 
427 } // namespace
428 
429 } // namespace
430 
431 #endif
432 
Core module.
Definition: Allocator.h:33
PointI floor() const
Floors each coordinate and returns a PointI.
Definition: Point.h:415
Point toNormalized() const
Returns a unit-length copy of this point, or a zero point if the length is zero.
Definition: Point.h:217
PointI round() const
Rounds each coordinate to the nearest integer and returns a PointI.
Definition: Point.h:409
bool isOrigin() const
Returns true if both X and Y are zero.
Definition: Point.h:385
Float lengthSquared() const
Returns the squared Euclidean length; cheaper than length().
Definition: Point.h:212
PointI ceil() const
Ceils each coordinate and returns a PointI.
Definition: Point.h:421
Point with floating-point X and Y coordinates.
Definition: Point.h:47
Point with integer X and Y coordinates.
Definition: Point.h:255
bool isOrigin() const
Returns true if both X and Y are zero.
Definition: Point.h:197
double hypot(double x, double y)
Return the euclidean distance of the given values.
Definition: Math.h:275
Float length() const
Returns the Euclidean length of the vector from the origin.
Definition: Point.h:207
PointI operator-() const
Returns the negation of this point.
Definition: Point.h:390
Point operator-() const
Returns the negation of this point.
Definition: Point.h:202