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 "export.hpp"
35#include "nomlib/config.hpp"
36
37namespace nom {
38
40const std::string POINT_DELIMITER = ", ";
41
43template <typename T>
44struct NOM_EXPORT Point2
45{
47 Point2 ( void ) :
48 x ( -1 ),
49 y ( -1 )
50 {
51 //NOM_LOG_TRACE(NOM);
52 }
53
55 ~Point2 ( void )
56 {
57 //NOM_LOG_TRACE(NOM);
58 }
59
61 Point2 ( T x, T y ) :
62 x ( x ),
63 y ( y )
64 {
65 //NOM_LOG_TRACE(NOM);
66 }
67
73 template <typename U>
74 explicit Point2 ( const Point2<U>& copy ) :
75 x { static_cast<T> ( copy.x ) },
76 y { static_cast<T> ( copy.y ) }
77 {
78 //NOM_LOG_TRACE(NOM);
79 }
80
82 inline const Point2<T>& get ( void ) const
83 {
84 return *this;
85 }
86
90 static const Point2 null;
91
93 static const Point2 zero;
94
96 T x;
97
99 T y;
100};
101
109template <typename T>
110NOM_EXPORT
111inline std::ostream& operator << ( std::ostream& os, const Point2<T>& pos )
112{
113 os
114 << pos.x
115 << POINT_DELIMITER
116 << pos.y;
117
118 return os;
119}
120
129template <typename T>
130NOM_EXPORT
131inline bool operator == ( const Point2<T>& lhs, const Point2<T>& rhs )
132{
133 return ( lhs.x == rhs.x ) && ( lhs.y == rhs.y );
134}
135
140template <typename T>
141NOM_EXPORT
142inline bool operator <( const Point2<T> lhs, const Point2<T>& rhs )
143{
144 return ( lhs.x < rhs.x ) && ( lhs.y < rhs.y );
145}
146
151template <typename T>
152NOM_EXPORT
153inline bool operator >( const Point2<T>& lhs, const Point2<T>& rhs )
154{
155 return ( rhs.x < lhs.x ) && ( rhs.y < lhs.y );
156}
157
162template <typename T>
163NOM_EXPORT
164inline bool operator <=( const Point2<T>& lhs, const Point2<T>& rhs )
165{
166 return ( lhs.x <= rhs.x ) && ( lhs.y <= rhs.y );
167}
168
173template <typename T>
174NOM_EXPORT
175inline bool operator >=( const Point2<T>& lhs, const Point2<T>& rhs )
176{
177 return ( rhs.x <= lhs.x ) && ( rhs.y <= lhs.y );
178}
179
188template <typename T>
189NOM_EXPORT
190inline bool operator != ( const Point2<T>& lhs, const Point2<T>& rhs )
191{
192 return ! ( lhs == rhs );
193}
194
201template <typename T>
202NOM_EXPORT
203inline Point2<T> operator + ( const Point2<T>& lhs, const Point2<T>& rhs )
204{
205 return Point2<T> ( lhs.x + rhs.x,
206 lhs.y + rhs.y
207 );
208}
209
216template <typename T>
217NOM_EXPORT
218inline Point2<T> operator +(int lhs, const Point2<T>& rhs)
219{
220 return Point2<T>(lhs + rhs.x, lhs + rhs.y);
221}
222
229template <typename T>
230NOM_EXPORT
231inline Point2<T> operator +(const Point2<T>& lhs, int rhs)
232{
233 return Point2<T>(lhs.x + rhs, lhs.y + rhs);
234}
235
241template <typename T>
242NOM_EXPORT
243inline Point2<T> operator ++ ( Point2<T>& rhs )
244{
245 return Point2<T> ( ++rhs.x,
246 ++rhs.y
247 );
248}
249
255template <typename T>
256NOM_EXPORT
257inline Point2<T> operator - ( const Point2<T>& rhs )
258{
259 return Point2<T> ( -rhs.x,
260 -rhs.y
261 );
262}
263
270template <typename T>
271NOM_EXPORT
272inline Point2<T> operator - ( const Point2<T>& lhs, const Point2<T>& rhs )
273{
274 return Point2<T> ( lhs.x - rhs.x,
275 lhs.y - rhs.y
276 );
277}
278
285template <typename T>
286NOM_EXPORT
287inline Point2<T> operator -(int lhs, const Point2<T>& rhs)
288{
289 return Point2<T>(lhs - rhs.x, lhs - rhs.y);
290}
291
298template <typename T>
299NOM_EXPORT
300inline Point2<T> operator -(const Point2<T>& lhs, int rhs)
301{
302 return Point2<T>(lhs.x - rhs, lhs.y - rhs);
303}
304
310template <typename T>
311NOM_EXPORT
312inline Point2<T> operator -- ( Point2<T>& rhs )
313{
314 return Point2<T> ( --rhs.x,
315 --rhs.y
316 );
317}
318
325template <typename T>
326NOM_EXPORT
327inline Point2<T> operator * ( const Point2<T>& lhs, const Point2<T>& rhs )
328{
329 return Point2<T> ( lhs.x * rhs.x,
330 lhs.y * rhs.y
331 );
332}
333
340template <typename T>
341NOM_EXPORT
342inline Point2<T> operator *(int lhs, const Point2<T>& rhs)
343{
344 return Point2<T>(lhs * rhs.x, lhs * rhs.y);
345}
346
353template <typename T>
354NOM_EXPORT
355inline Point2<T> operator *(const Point2<T>& lhs, int rhs)
356{
357 return Point2<T>(lhs.x * rhs, lhs.y * rhs);
358}
359
368template <typename T>
369NOM_EXPORT
370inline Point2<T> operator / ( const Point2<T>& lhs, const Point2<T>& rhs )
371{
372 return Point2<T> ( lhs.x / rhs.x,
373 lhs.y / rhs.y
374 );
375}
376
385template <typename T>
386NOM_EXPORT
387inline Point2<T> operator /(int lhs, const Point2<T>& rhs)
388{
389 return Point2<T>(lhs / rhs.x, lhs / rhs.y);
390}
391
400template <typename T>
401NOM_EXPORT
402inline Point2<T> operator /(const Point2<T>& lhs, int rhs)
403{
404 return Point2<T>(lhs.x / rhs, lhs.y / rhs);
405}
406
416template <typename T>
417NOM_EXPORT
418inline Point2<T>& operator += ( Point2<T>& lhs, const Point2<T>& rhs )
419{
420 lhs.x += rhs.x;
421 lhs.y += rhs.y;
422
423 return lhs;
424}
425
435template <typename T>
436NOM_EXPORT
437inline Point2<T>& operator +=(int lhs, const Point2<T>& rhs)
438{
439 lhs += rhs.x;
440 lhs += rhs.y;
441
442 return lhs;
443}
444
454template <typename T>
455NOM_EXPORT
456inline Point2<T>& operator +=(Point2<T>& lhs, int rhs)
457{
458 lhs.x += rhs;
459 lhs.y += rhs;
460
461 return lhs;
462}
463
473template <typename T>
474NOM_EXPORT
475inline Point2<T>& operator -= ( Point2<T>& lhs, const Point2<T>& rhs )
476{
477 lhs.x -= rhs.x;
478 lhs.y -= rhs.y;
479
480 return lhs;
481}
482
492template <typename T>
493NOM_EXPORT
494inline Point2<T>& operator -=(int lhs, const Point2<T>& rhs)
495{
496 lhs -= rhs.x;
497 lhs -= rhs.y;
498
499 return lhs;
500}
501
511template <typename T>
512NOM_EXPORT
513inline Point2<T>& operator -=(Point2<T>& lhs, int rhs)
514{
515 lhs.x -= rhs;
516 lhs.y -= rhs;
517
518 return lhs;
519}
520
530template <typename T>
531NOM_EXPORT
532inline Point2<T>& operator *= ( Point2<T>& lhs, const Point2<T>& rhs )
533{
534 lhs.x *= rhs.x;
535 lhs.y *= rhs.y;
536
537 return lhs;
538}
539
549template <typename T>
550NOM_EXPORT
551inline Point2<T>& operator *=(int lhs, const Point2<T>& rhs)
552{
553 lhs *= rhs.x;
554 lhs *= rhs.y;
555
556 return lhs;
557}
558
568template <typename T>
569NOM_EXPORT
570inline Point2<T>& operator *=(Point2<T>& lhs, int rhs)
571{
572 lhs.x *= rhs;
573 lhs.y *= rhs;
574
575 return lhs;
576}
577
587template <typename T>
588NOM_EXPORT
589inline Point2<T>& operator /= ( Point2<T>& lhs, Point2<T>& rhs )
590{
591 lhs.x /= rhs.x;
592 lhs.y /= rhs.y;
593
594 return lhs;
595}
596
606template <typename T>
607NOM_EXPORT
608inline Point2<T>& operator /=(int lhs, Point2<T>& rhs)
609{
610 lhs /= rhs.x;
611 lhs /= rhs.y;
612
613 return lhs;
614}
615
625template <typename T>
626NOM_EXPORT
627inline Point2<T>& operator /=(Point2<T>& lhs, int rhs)
628{
629 lhs.x /= rhs;
630 lhs.y /= rhs;
631
632 return lhs;
633}
634
636typedef Point2<int> Point2i;
637
639typedef Point2<float> Point2f;
640
642typedef Point2<double> Point2d;
643
645template <> NOM_EXPORT const Point2i Point2i::null;
646
648template <> NOM_EXPORT const Point2f Point2f::null;
649
651template <> NOM_EXPORT const Point2d Point2d::null;
652
654template <> NOM_EXPORT const Point2i Point2i::zero;
655
657template <> NOM_EXPORT const Point2f Point2f::zero;
658
661template <> NOM_EXPORT const Point2d Point2d::zero;
662
663} // namespace nom
664
665#endif // include guard defined
2D point container
Definition Point2.hpp:45
Point2(const Point2< U > &copy)
Copy constructor.
Definition Point2.hpp:74
~Point2(void)
Destructor.
Definition Point2.hpp:55
static const Point2 null
Definition Point2.hpp:90
Point2(void)
Default constructor; initialize values to Point2<T>::null.
Definition Point2.hpp:47
static const Point2 zero
Definition Point2.hpp:93
Point2(T x, T y)
Constructor variant for initializing x, y at construction.
Definition Point2.hpp:61
const Point2< T > & get(void) const
Obtain a reference of the object.
Definition Point2.hpp:82