nomlib
Loading...
Searching...
No Matches
Point2.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_MATH_POINT2_HEADERS
30#define NOMLIB_MATH_POINT2_HEADERS
31
32#include <iostream>
33
34#include "nomlib/config.hpp"
35
36namespace nom {
37
39const std::string POINT_DELIMITER = ", ";
40
42template <typename T>
43struct Point2
44{
46 Point2 ( void ) :
47 x ( -1 ),
48 y ( -1 )
49 {
50 //NOM_LOG_TRACE(NOM);
51 }
52
54 ~Point2 ( void )
55 {
56 //NOM_LOG_TRACE(NOM);
57 }
58
60 Point2 ( T x, T y ) :
61 x ( x ),
62 y ( y )
63 {
64 //NOM_LOG_TRACE(NOM);
65 }
66
72 template <typename U>
73 explicit Point2 ( const Point2<U>& copy ) :
74 x { static_cast<T> ( copy.x ) },
75 y { static_cast<T> ( copy.y ) }
76 {
77 //NOM_LOG_TRACE(NOM);
78 }
79
81 inline const Point2<T>& get ( void ) const
82 {
83 return *this;
84 }
85
89 static const Point2 null;
90
92 static const Point2 zero;
93
95 T x;
96
98 T y;
99};
100
108template <typename T>
109inline std::ostream& operator << ( std::ostream& os, const Point2<T>& pos )
110{
111 os
112 << pos.x
113 << POINT_DELIMITER
114 << pos.y;
115
116 return os;
117}
118
127template <typename T>
128inline bool operator == ( const Point2<T>& lhs, const Point2<T>& rhs )
129{
130 return ( lhs.x == rhs.x ) && ( lhs.y == rhs.y );
131}
132
137template <typename T>
138inline bool operator <( const Point2<T> lhs, const Point2<T>& rhs )
139{
140 return ( lhs.x < rhs.x ) && ( lhs.y < rhs.y );
141}
142
147template <typename T>
148inline bool operator >( const Point2<T>& lhs, const Point2<T>& rhs )
149{
150 return ( rhs.x < lhs.x ) && ( rhs.y < lhs.y );
151}
152
157template <typename T>
158inline bool operator <=( const Point2<T>& lhs, const Point2<T>& rhs )
159{
160 return ( lhs.x <= rhs.x ) && ( lhs.y <= rhs.y );
161}
162
167template <typename T>
168inline bool operator >=( const Point2<T>& lhs, const Point2<T>& rhs )
169{
170 return ( rhs.x <= lhs.x ) && ( rhs.y <= lhs.y );
171}
172
181template <typename T>
182inline bool operator != ( const Point2<T>& lhs, const Point2<T>& rhs )
183{
184 return ! ( lhs == rhs );
185}
186
193template <typename T>
194inline Point2<T> operator + ( const Point2<T>& lhs, const Point2<T>& rhs )
195{
196 return Point2<T> ( lhs.x + rhs.x,
197 lhs.y + rhs.y
198 );
199}
200
207template <typename T>
208inline Point2<T> operator +(int lhs, const Point2<T>& rhs)
209{
210 return Point2<T>(lhs + rhs.x, lhs + rhs.y);
211}
212
219template <typename T>
220inline Point2<T> operator +(const Point2<T>& lhs, int rhs)
221{
222 return Point2<T>(lhs.x + rhs, lhs.y + rhs);
223}
224
230template <typename T>
231inline Point2<T> operator ++ ( Point2<T>& rhs )
232{
233 return Point2<T> ( ++rhs.x,
234 ++rhs.y
235 );
236}
237
243template <typename T>
244inline Point2<T> operator - ( const Point2<T>& rhs )
245{
246 return Point2<T> ( -rhs.x,
247 -rhs.y
248 );
249}
250
257template <typename T>
258inline Point2<T> operator - ( const Point2<T>& lhs, const Point2<T>& rhs )
259{
260 return Point2<T> ( lhs.x - rhs.x,
261 lhs.y - rhs.y
262 );
263}
264
271template <typename T>
272inline Point2<T> operator -(int lhs, const Point2<T>& rhs)
273{
274 return Point2<T>(lhs - rhs.x, lhs - rhs.y);
275}
276
283template <typename T>
284inline Point2<T> operator -(const Point2<T>& lhs, int rhs)
285{
286 return Point2<T>(lhs.x - rhs, lhs.y - rhs);
287}
288
294template <typename T>
295inline Point2<T> operator -- ( Point2<T>& rhs )
296{
297 return Point2<T> ( --rhs.x,
298 --rhs.y
299 );
300}
301
308template <typename T>
309inline Point2<T> operator * ( const Point2<T>& lhs, const Point2<T>& rhs )
310{
311 return Point2<T> ( lhs.x * rhs.x,
312 lhs.y * rhs.y
313 );
314}
315
322template <typename T>
323inline Point2<T> operator *(int lhs, const Point2<T>& rhs)
324{
325 return Point2<T>(lhs * rhs.x, lhs * rhs.y);
326}
327
334template <typename T>
335inline Point2<T> operator *(const Point2<T>& lhs, int rhs)
336{
337 return Point2<T>(lhs.x * rhs, lhs.y * rhs);
338}
339
348template <typename T>
349inline Point2<T> operator / ( const Point2<T>& lhs, const Point2<T>& rhs )
350{
351 return Point2<T> ( lhs.x / rhs.x,
352 lhs.y / rhs.y
353 );
354}
355
364template <typename T>
365inline Point2<T> operator /(int lhs, const Point2<T>& rhs)
366{
367 return Point2<T>(lhs / rhs.x, lhs / rhs.y);
368}
369
378template <typename T>
379inline Point2<T> operator /(const Point2<T>& lhs, int rhs)
380{
381 return Point2<T>(lhs.x / rhs, lhs.y / rhs);
382}
383
393template <typename T>
394inline Point2<T>& operator += ( Point2<T>& lhs, const Point2<T>& rhs )
395{
396 lhs.x += rhs.x;
397 lhs.y += rhs.y;
398
399 return lhs;
400}
401
411template <typename T>
412inline Point2<T>& operator +=(int lhs, const Point2<T>& rhs)
413{
414 lhs += rhs.x;
415 lhs += rhs.y;
416
417 return lhs;
418}
419
429template <typename T>
430inline Point2<T>& operator +=(Point2<T>& lhs, int rhs)
431{
432 lhs.x += rhs;
433 lhs.y += rhs;
434
435 return lhs;
436}
437
447template <typename T>
448inline Point2<T>& operator -= ( Point2<T>& lhs, const Point2<T>& rhs )
449{
450 lhs.x -= rhs.x;
451 lhs.y -= rhs.y;
452
453 return lhs;
454}
455
465template <typename T>
466inline Point2<T>& operator -=(int lhs, const Point2<T>& rhs)
467{
468 lhs -= rhs.x;
469 lhs -= rhs.y;
470
471 return lhs;
472}
473
483template <typename T>
484inline Point2<T>& operator -=(Point2<T>& lhs, int rhs)
485{
486 lhs.x -= rhs;
487 lhs.y -= rhs;
488
489 return lhs;
490}
491
501template <typename T>
502inline Point2<T>& operator *= ( Point2<T>& lhs, const Point2<T>& rhs )
503{
504 lhs.x *= rhs.x;
505 lhs.y *= rhs.y;
506
507 return lhs;
508}
509
519template <typename T>
520inline Point2<T>& operator *=(int lhs, const Point2<T>& rhs)
521{
522 lhs *= rhs.x;
523 lhs *= rhs.y;
524
525 return lhs;
526}
527
537template <typename T>
538inline Point2<T>& operator *=(Point2<T>& lhs, int rhs)
539{
540 lhs.x *= rhs;
541 lhs.y *= rhs;
542
543 return lhs;
544}
545
555template <typename T>
556inline Point2<T>& operator /= ( Point2<T>& lhs, Point2<T>& rhs )
557{
558 lhs.x /= rhs.x;
559 lhs.y /= rhs.y;
560
561 return lhs;
562}
563
573template <typename T>
574inline Point2<T>& operator /=(int lhs, Point2<T>& rhs)
575{
576 lhs /= rhs.x;
577 lhs /= rhs.y;
578
579 return lhs;
580}
581
591template <typename T>
592inline Point2<T>& operator /=(Point2<T>& lhs, int rhs)
593{
594 lhs.x /= rhs;
595 lhs.y /= rhs;
596
597 return lhs;
598}
599
601typedef Point2<int> Point2i;
602
604typedef Point2<float> Point2f;
605
607typedef Point2<double> Point2d;
608
609} // namespace nom
610
611#endif // include guard defined
2D point container
Definition Point2.hpp:44
Point2(const Point2< U > &copy)
Copy constructor.
Definition Point2.hpp:73
~Point2(void)
Destructor.
Definition Point2.hpp:54
static const Point2 null
Definition Point2.hpp:89
Point2(void)
Default constructor; initialize values to Point2<T>::null.
Definition Point2.hpp:46
static const Point2 zero
Definition Point2.hpp:92
Point2(T x, T y)
Constructor variant for initializing x, y at construction.
Definition Point2.hpp:60
const Point2< T > & get(void) const
Obtain a reference of the object.
Definition Point2.hpp:81