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
38namespace Pt {
39
40namespace Gfx {
41
42class PointI;
43
72class Point
73{
74 public:
75 Point()
76 : _x(0), _y(0)
77 {}
78
79 Point(Float x, Float y)
80 : _x(x), _y(y)
81 {}
82
83 Point(const Point& pt)
84 : _x(pt._x), _y(pt._y)
85 {}
86
87 void clear()
88 {
89 _x = 0;
90 _y = 0;
91 }
92
93 void set(Float xpos, Float ypos)
94 {
95 _x = xpos;
96 _y = ypos;
97 }
98
99 void setX(Float xpos)
100 { _x = xpos; }
101
102 void setY(Float ypos)
103 { _y = ypos; }
104
105 Float x() const
106 { return _x; }
107
108 Float y() const
109 { return _y; }
110
111 const Point& addX(Float x)
112 {
113 _x += x;
114 return *this;
115 }
116
117 const Point& subX(Float x)
118 {
119 _x -= x;
120 return *this;
121 }
122
123 const Point& addY(Float y)
124 {
125 _y += y;
126 return *this;
127 }
128
129 const Point& subY(Float y)
130 {
131 _y -= y;
132 return *this;
133 }
134
135 const Point& move(Float dx, Float dy)
136 {
137 _x += dx;
138 _y += dy;
139 return *this;
140 }
141
142 Float distanceTo(const Point& other) const
143 {
144 if(isEqual(other))
145 return 0;
146
147 return hypot(_x - other._x, _y - other._y);
148 }
149
150 Point& operator=(const Point& pt)
151 {
152 _x = pt._x;
153 _y = pt._y;
154 return *this;
155 }
156
157 bool isEqual(const Point& other, Float eps = FloatNearlyZero) const
158 {
159 return std::abs(_x - other._x) <= eps &&
160 std::abs(_y - other._y) <= eps;
161 }
162
163 Point& operator+=(const Point& pt)
164 {
165 _x += pt._x;
166 _y += pt._y;
167 return *this;
168 }
169
170 Point operator+(const Point& pt) const
171 {
172 return Point(_x + pt._x, _y + pt._y);
173 }
174
175 Point& operator-=(const Point& pt)
176 {
177 _x -= pt._x;
178 _y -= pt._y;
179 return *this;
180 }
181
182 Point operator-(const Point& pt) const
183 {
184 return Point(_x - pt._x, _y - pt._y);
185 }
186
187 Point operator*(Float factor) const
188 {
189 return Point(_x * factor, _y * factor);
190 }
191
192 Point operator/(Float factor) const
193 {
194 return Point(_x / factor, _y / factor);
195 }
196
197 Point operator+(Float factor) const
198 {
199 return Point(_x + factor, _y + factor);
200 }
201
202 Point operator-(Float factor) const
203 {
204 return Point(_x - factor, _y - factor);
205 }
206
207 Point& operator*=(Float factor)
208 {
209 _x *= factor;
210 _y *= factor;
211 return *this;
212 }
213
214 Point& operator/=(Float factor)
215 {
216 _x /= factor;
217 _y /= factor;
218 return *this;
219 }
220
223 bool isOrigin() const
224 { return _x == 0 && _y == 0; }
225
228 Point operator-() const
229 { return Point(-_x, -_y); }
230
233 Float length() const
234 { return hypot(_x, _y); }
235
238 Float lengthSquared() const
239 { return _x * _x + _y * _y; }
240
243 Point toNormalized() const
244 {
245 const Float len = this->length();
246 if(len == 0)
247 return Point();
248 return Point(_x / len, _y / len);
249 }
250
253 explicit Point(const PointI& pt);
254
257 Point& operator=(const PointI& pt);
258
261 PointI round() const;
262
265 PointI floor() const;
266
269 PointI ceil() const;
270
271 private:
272 Float _x;
273 Float _y;
274};
275
276typedef Point PointF;
277
291class PointI
292{
293 public:
294 PointI()
295 : _x(0), _y(0)
296 {}
297
298 PointI(Int x, Int y)
299 : _x(x), _y(y)
300 {}
301
302 PointI(const PointI& pt)
303 : _x(pt._x), _y(pt._y)
304 {}
305
306 void clear()
307 {
308 _x = 0;
309 _y = 0;
310 }
311
312 void set(Int xpos, Int ypos)
313 {
314 _x = xpos;
315 _y = ypos;
316 }
317
318 void setX(Int xpos)
319 { _x = xpos; }
320
321 void setY(Int ypos)
322 { _y = ypos; }
323
324 Int x() const
325 { return _x; }
326
327 Int y() const
328 { return _y; }
329
330 const PointI& addX(Int x)
331 {
332 _x += x;
333 return *this;
334 }
335
336 const PointI& subX(Int x)
337 {
338 _x -= x;
339 return *this;
340 }
341
342 const PointI& addY(Int y)
343 {
344 _y += y;
345 return *this;
346 }
347
348 const PointI& subY(Int y)
349 {
350 _y -= y;
351 return *this;
352 }
353
354 const PointI& move(Int dx, Int dy)
355 {
356 _x += dx;
357 _y += dy;
358 return *this;
359 }
360
361 PointI& operator=(const PointI& pt)
362 {
363 _x = pt._x;
364 _y = pt._y;
365 return *this;
366 }
367
368 bool operator==(const PointI& pt) const
369 { return (_x == pt._x && _y == pt._y); }
370
371 bool operator!=(const PointI& pt) const
372 { return (_x != pt._x || _y != pt._y); }
373
374 PointI& operator+=(const PointI& pt)
375 {
376 _x += pt._x;
377 _y += pt._y;
378 return *this;
379 }
380
381 PointI operator+(const PointI& pt) const
382 {
383 return PointI(_x + pt._x, _y + pt._y);
384 }
385
386 PointI& operator-=(const PointI& pt)
387 {
388 _x -= pt._x;
389 _y -= pt._y;
390 return *this;
391 }
392
393 PointI operator-(const PointI& pt) const
394 {
395 return PointI(_x - pt._x, _y - pt._y);
396 }
397
398 PointI operator*(Int factor) const
399 {
400 return PointI(_x * factor, _y * factor);
401 }
402
403 PointI operator+(Int offset) const
404 {
405 return PointI(_x + offset, _y + offset);
406 }
407
408 PointI operator-(Int offset) const
409 {
410 return PointI(_x - offset, _y - offset);
411 }
412
413 PointI& operator*=(Int factor)
414 {
415 _x *= factor;
416 _y *= factor;
417 return *this;
418 }
419
422 bool isOrigin() const
423 { return _x == 0 && _y == 0; }
424
427 PointI operator-() const
428 { return PointI(-_x, -_y); }
429
430 private:
431 Int _x;
432 Int _y;
433};
434
435inline Point::Point(const PointI& pt)
436: _x(static_cast<Float>(pt.x())), _y(static_cast<Float>(pt.y()))
437{}
438
439inline Point& Point::operator=(const PointI& pt)
440{
441 _x = static_cast<Float>(pt.x());
442 _y = static_cast<Float>(pt.y());
443 return *this;
444}
445
446inline PointI Point::round() const
447{
448 return PointI(static_cast<Int>(std::lround(_x)),
449 static_cast<Int>(std::lround(_y)));
450}
451
452inline PointI Point::floor() const
453{
454 return PointI(static_cast<Int>(std::floor(_x)),
455 static_cast<Int>(std::floor(_y)));
456}
457
458inline PointI Point::ceil() const
459{
460 return PointI(static_cast<Int>(std::ceil(_x)),
461 static_cast<Int>(std::ceil(_y)));
462}
463
464} // namespace
465
466} // namespace
467
468#endif
469
Integer X and Y coordinates.
Definition Point.h:292
PointI operator-() const
Returns the negation of this point.
Definition Point.h:427
bool isOrigin() const
Returns true if both X and Y are zero.
Definition Point.h:422
Floating-point X and Y coordinates.
Definition Point.h:73
Point operator-() const
Returns the negation of this point.
Definition Point.h:228
PointI round() const
Rounds each coordinate to the nearest integer and returns a PointI.
Definition Point.h:446
PointI floor() const
Floors each coordinate and returns a PointI.
Definition Point.h:452
PointI ceil() const
Ceils each coordinate and returns a PointI.
Definition Point.h:458
bool isOrigin() const
Returns true if both X and Y are zero.
Definition Point.h:223
Float length() const
Returns the Euclidean length of the vector from the origin.
Definition Point.h:233
Point toNormalized() const
Returns a unit-length copy of this point, or a zero point if the length is zero.
Definition Point.h:243
Float lengthSquared() const
Returns the squared Euclidean length; cheaper than length().
Definition Point.h:238
Graphics and imaging services.
Definition Api-Argb32Image.h:12
Core module.
Definition Allocator.h:33
double hypot(double x, double y)
Return the euclidean distance of the given values.
Definition Math.h:275