nomlib
Loading...
Searching...
No Matches
Rect.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_RECT_HEADERS
30#define NOMLIB_MATH_RECT_HEADERS
31
32#include "export.hpp"
33#include "nomlib/config.hpp"
34#include "nomlib/math/Point2.hpp"
35#include "nomlib/math/Size2.hpp"
36#include "nomlib/ptree/Value.hpp"
37
38namespace nom {
39
41const std::string RECT_DELIMITER = ", ";
42
46template<typename T>
47struct NOM_EXPORT Rect
48{
50 Rect ( void ) :
51 x ( -1 ),
52 y ( -1 ),
53 w ( -1 ),
54 h ( -1 )
55 {
56 //NOM_LOG_TRACE(NOM);
57 }
58
60 ~Rect ( void )
61 {
62 //NOM_LOG_TRACE(NOM);
63 }
64
66 Rect ( T left, T top, T right, T bottom ) :
67 x ( left ),
68 y ( top ),
69 w ( right ),
70 h ( bottom )
71 {
72 //NOM_LOG_TRACE(NOM);
73 }
74
76 Rect ( const Point2<T>& pos, const Size2<T>& size ) :
77 x ( pos.x ),
78 y ( pos.y ),
79 w ( size.w ),
80 h ( size.h )
81 {
82 //NOM_LOG_TRACE(NOM);
83 }
84
90 template <typename U>
91 explicit Rect ( const Rect<U>& copy ) :
92 x { static_cast<T> ( copy.x ) },
93 y { static_cast<T> ( copy.y ) },
94 w { static_cast<T> ( copy.w ) },
95 h { static_cast<T> ( copy.h ) }
96 {
97 //NOM_LOG_TRACE(NOM);
98 }
99
102 bool contains(T xx, T yy) const
103 {
104 return (
105 ( xx >= this->x && xx <= this->x + this->w ) &&
106 ( yy >= this->y && yy <= this->y + this->h )
107 );
108 }
109
112 bool contains ( const Point2<T>& pos ) const
113 {
114 return this->contains ( pos.x, pos.y );
115 }
116
118 bool intersects ( const Rect<T>& rectangle ) const
119 {
120 T leftA, leftB = 0;
121 T rightA, rightB = 0;
122 T topA, topB = 0;
123 T bottomA, bottomB = 0;
124
125 // Calculate sides of RectA
126 leftA = this->x;
127 rightA = leftA + this->w;
128 topA = this->y;
129 bottomA = topA + this->h;
130
131 // Calculate sides of RectB
132 leftB = rectangle.x;
133 rightB = leftB + rectangle.w;
134 topB = rectangle.y;
135 bottomB = topB + rectangle.h;
136
137 if ( bottomA <= topB ) return false; // No collision
138 if ( topA >= bottomB ) return false; // No collision
139 if ( rightA <= leftB ) return false; // No collision
140 if ( leftA >= rightB ) return false; // No collision
141
142 return true; // Collision!
143 }
144
150 const Object serialize( void ) const
151 {
152 Object object;
153
154 object["x"] = this->x;
155 object["y"] = this->y;
156 object["width"] = this->w;
157 object["height"] = this->h;
158
159 return object;
160 }
161
163 const Point2i position( void ) const
164 {
165 return Point2i( NOM_SCAST( T, this->x ), NOM_SCAST( T, this->y ) );
166 }
167
169 const Size2i size( void ) const
170 {
171 return Size2i( NOM_SCAST( T, this->w ), NOM_SCAST( T, this->h ) );
172 }
173
174 const T& left() const
175 {
176 return this->x;
177 }
178
179 const T& top() const
180 {
181 return this->y;
182 }
183
184 const T& right() const
185 {
186 return(this->x + this->w);
187 }
188
189 const T& bottom() const
190 {
191 return(this->y + this->h);
192 }
193
194 template <typename U>
195 void set_position(const Point2<U>& pos)
196 {
197 this->x = pos.x;
198 this->y = pos.y;
199 }
200
201 template <typename U>
202 void set_size(const Size2<U>& dims)
203 {
204 this->w = dims.w;
205 this->h = dims.h;
206 }
207
211 NOM_EXPORT static const Rect null;
212
214 NOM_EXPORT static const Rect zero;
215
217 T x;
219
220 T y;
222 T w;
223
225 T h;
226};
227
243template <typename T>
244NOM_EXPORT
245inline std::ostream& operator <<( std::ostream& os, const Rect<T>& rect )
246{
247 os
248 << rect.x
249 << RECT_DELIMITER
250 << rect.y
251 << RECT_DELIMITER
252 << rect.w
253 << RECT_DELIMITER
254 << rect.h;
255 return os;
256}
257
266template <typename T>
267NOM_EXPORT
268inline bool operator == ( const Rect<T>& lhs, const Rect<T>& rhs )
269{
270 return ( lhs.x == rhs.x ) && ( lhs.w == rhs.w )
271 &&
272 ( lhs.y == rhs.y ) && ( lhs.h == rhs.h );
273}
274
283template <typename T>
284NOM_EXPORT
285inline bool operator != ( const Rect<T>& lhs, const Rect<T>& rhs )
286{
287 return ! ( lhs == rhs );
288}
289
296template <typename T>
297NOM_EXPORT
298inline Rect<T> operator + ( const Rect<T>& lhs, const Rect<T>& rhs )
299{
300 return Rect<T> ( lhs.x + rhs.x,
301 lhs.y + rhs.y,
302 lhs.w + rhs.w,
303 lhs.h + rhs.h
304 );
305}
306
313template <typename T>
314NOM_EXPORT
315inline Rect<T> operator +(int lhs, const Rect<T>& rhs)
316{
317 return Rect<T>(lhs + rhs.x, lhs + rhs.y, lhs + rhs.w, lhs + rhs.h);
318}
319
326template <typename T>
327NOM_EXPORT
328inline Rect<T> operator +(const Rect<T>& lhs, int rhs)
329{
330 return Rect<T>(lhs.x + rhs, lhs.y + rhs, lhs.w + rhs, lhs.h + rhs);
331}
332
338template <typename T>
339NOM_EXPORT
340inline Rect<T> operator ++ ( const Rect<T>& rhs )
341{
342 return Rect<T> ( rhs.x + 1,
343 rhs.y + 1,
344 rhs.w + 1,
345 rhs.h + 1
346 );
347}
348
354template <typename T>
355NOM_EXPORT
356inline Rect<T> operator -( const Rect<T>& rhs )
357{
358 return Rect<T> ( -rhs.x,
359 -rhs.y,
360 -rhs.w
361 -rhs.h
362 );
363}
364
371template <typename T>
372NOM_EXPORT
373inline Rect<T> operator -( const Rect<T>& lhs, const Rect<T>& rhs )
374{
375 return Rect<T> ( lhs.x - rhs.x,
376 lhs.y - rhs.y,
377 lhs.w - rhs.w,
378 lhs.h - rhs.h
379 );
380}
381
388template <typename T>
389NOM_EXPORT
390inline Rect<T> operator -(int lhs, const Rect<T>& rhs)
391{
392 return Rect<T>(lhs - rhs.x, lhs - rhs.y, lhs - rhs.w, lhs - rhs.h);
393}
394
401template <typename T>
402NOM_EXPORT
403inline Rect<T> operator -(const Rect<T>& lhs, int rhs)
404{
405 return Rect<T>(lhs.x - rhs, lhs.y - rhs, lhs.w - rhs, lhs.h - rhs);
406}
407
413template <typename T>
414NOM_EXPORT
415inline Rect<T> operator -- ( const Rect<T>& rhs )
416{
417 return Rect<T> ( rhs.x - 1,
418 rhs.y - 1,
419 rhs.w - 1,
420 rhs.h - 1
421 );
422}
423
430template <typename T>
431NOM_EXPORT
432inline Rect<T> operator * ( const Rect<T>& lhs, const Rect<T>& rhs )
433{
434 return Rect<T> ( lhs.x * rhs.x,
435 lhs.y * rhs.y,
436 lhs.w * rhs.w,
437 lhs.h * rhs.h
438 );
439}
440
447template <typename T>
448NOM_EXPORT
449inline Rect<T> operator *(int lhs, const Rect<T>& rhs)
450{
451 return Rect<T>(lhs * rhs.x, lhs * rhs.y, lhs * rhs.w, lhs * rhs.h);
452}
453
460template <typename T>
461NOM_EXPORT
462inline Rect<T> operator *(const Rect<T>& lhs, int rhs)
463{
464 return Rect<T> ( lhs.x * rhs,
465 lhs.y * rhs,
466 lhs.w * rhs,
467 lhs.h * rhs
468 );
469}
470
479template <typename T>
480NOM_EXPORT
481inline Rect<T> operator /( const Rect<T>& lhs, const Rect<T>& rhs )
482{
483 return Rect<T> ( lhs.x / rhs.x,
484 lhs.y / rhs.y,
485 lhs.w / rhs.w,
486 lhs.h / rhs.h
487 );
488}
489
498template <typename T>
499NOM_EXPORT
500inline Rect<T> operator /(int lhs, const Rect<T>& rhs)
501{
502 return Rect<T> ( lhs / rhs.x,
503 lhs / rhs.y,
504 lhs / rhs.w,
505 lhs / rhs.h
506 );
507}
508
517template <typename T>
518NOM_EXPORT
519inline Rect<T> operator /(const Rect<T>& lhs, int rhs)
520{
521 return Rect<T> ( lhs.x / rhs,
522 lhs.y / rhs,
523 lhs.w / rhs,
524 lhs.h / rhs
525 );
526}
527
537template <typename T>
538NOM_EXPORT
539inline Rect<T>& operator +=( Rect<T>& lhs, const Rect<T>& rhs )
540{
541 lhs.x += rhs.x;
542 lhs.y += rhs.y;
543 lhs.w += rhs.w;
544 lhs.h += rhs.h;
545
546 return lhs;
547}
548
558template <typename T>
559NOM_EXPORT
560inline Rect<T>& operator +=(int lhs, const Rect<T>& rhs)
561{
562 lhs += rhs.x;
563 lhs += rhs.y;
564 lhs += rhs.w;
565 lhs += rhs.h;
566
567 return lhs;
568}
569
579template <typename T>
580NOM_EXPORT
581inline Rect<T>& operator +=(Rect<T>& lhs, int rhs)
582{
583 lhs.x += rhs;
584 lhs.y += rhs;
585 lhs.w += rhs;
586 lhs.h += rhs;
587
588 return lhs;
589}
590
600template <typename T>
601NOM_EXPORT
602inline Rect<T>& operator -=( Rect<T>& lhs, const Rect<T>& rhs )
603{
604 lhs.x -= rhs.x;
605 lhs.y -= rhs.y;
606 lhs.w -= rhs.w;
607 lhs.h -= rhs.h;
608
609 return lhs;
610}
611
621template <typename T>
622NOM_EXPORT
623inline Rect<T>& operator -=(int lhs, const Rect<T>& rhs)
624{
625 lhs -= rhs.x;
626 lhs -= rhs.y;
627 lhs -= rhs.w;
628 lhs -= rhs.h;
629
630 return lhs;
631}
632
642template <typename T>
643NOM_EXPORT
644inline Rect<T>& operator -=(Rect<T>& lhs, int rhs)
645{
646 lhs.x -= rhs;
647 lhs.y -= rhs;
648 lhs.w -= rhs;
649 lhs.h -= rhs;
650
651 return lhs;
652}
653
663template <typename T>
664NOM_EXPORT
665inline Rect<T>& operator *=( Rect<T>& lhs, const Rect<T>& rhs )
666{
667 lhs.x *= rhs.x;
668 lhs.y *= rhs.y;
669 lhs.w *= rhs.w;
670 lhs.h *= rhs.h;
671
672 return lhs;
673}
674
684template <typename T>
685NOM_EXPORT
686inline Rect<T>& operator *=(int lhs, const Rect<T>& rhs)
687{
688 lhs *= rhs.x;
689 lhs *= rhs.y;
690 lhs *= rhs.w;
691 lhs *= rhs.h;
692
693 return lhs;
694}
695
705template <typename T>
706NOM_EXPORT
707inline Rect<T>& operator *=(Rect<T>& lhs, int rhs)
708{
709 lhs.x *= rhs;
710 lhs.y *= rhs;
711 lhs.w *= rhs;
712 lhs.h *= rhs;
713
714 return lhs;
715}
716
726template <typename T>
727NOM_EXPORT
728inline Rect<T>& operator /=( Rect<T>& lhs, const Rect<T>& rhs )
729{
730 lhs.x /= rhs.x;
731 lhs.y /= rhs.y;
732 lhs.w /= rhs.w;
733 lhs.h /= rhs.h;
734
735 return lhs;
736}
737
747template <typename T>
748NOM_EXPORT
749inline Rect<T>& operator /=(int lhs, const Rect<T>& rhs)
750{
751 lhs /= rhs.x;
752 lhs /= rhs.y;
753 lhs /= rhs.w;
754 lhs /= rhs.h;
755
756 return lhs;
757}
758
768template <typename T>
769NOM_EXPORT
770inline Rect<T>& operator /=(Rect<T>& lhs, int rhs)
771{
772 lhs.x /= rhs;
773 lhs.y /= rhs;
774 lhs.w /= rhs;
775 lhs.h /= rhs;
776
777 return lhs;
778}
779
784template <typename T>
785NOM_EXPORT
786inline bool operator <( const Rect<T> lhs, const Rect<T>& rhs )
787{
788 return ( lhs.x < rhs.x ) && ( lhs.y < rhs.y )
789 &&
790 ( lhs.w < rhs.w ) && ( lhs.h < rhs.h );
791}
792
797template <typename T>
798NOM_EXPORT
799inline bool operator >( const Rect<T>& lhs, const Rect<T>& rhs )
800{
801 return ( rhs.x < lhs.x ) && ( rhs.y < lhs.y )
802 &&
803 ( rhs.w < lhs.w ) && ( rhs.h < lhs.h );
804}
805
810template <typename T>
811NOM_EXPORT
812inline bool operator <=( const Rect<T>& lhs, const Rect<T>& rhs )
813{
814 return ( lhs.x <= rhs.x ) && ( lhs.y <= rhs.y )
815 &&
816 ( lhs.w <= rhs.w ) && ( lhs.h <= rhs.h );
817}
818
823template <typename T>
824NOM_EXPORT
825inline bool operator >=( const Rect<T>& lhs, const Rect<T>& rhs )
826{
827 return ( rhs.x <= lhs.x ) && ( rhs.y <= lhs.y )
828 &&
829 ( rhs.w <= lhs.w ) && ( rhs.h <= lhs.h );
830}
831
833typedef Rect<int> IntRect;
834
836typedef Rect<float> FloatRect;
837
839typedef Rect<double> DoubleRect;
840
842template <> NOM_EXPORT const IntRect IntRect::null;
843
845template <> NOM_EXPORT const FloatRect FloatRect::null;
846
848template <> NOM_EXPORT const DoubleRect DoubleRect::null;
849
851template <> NOM_EXPORT const IntRect IntRect::zero;
852
854template <> NOM_EXPORT const FloatRect FloatRect::zero;
855
857template <> NOM_EXPORT const DoubleRect DoubleRect::zero;
858
859} // namespace nom
860
861#endif // include guard defined
2D point container
Definition Point2.hpp:45
T x
Represents the X-axis coordinate point.
Definition Point2.hpp:96
T y
Represents the Y-axis coordinate point.
Definition Point2.hpp:99
Rectangle shape container.
Definition Rect.hpp:48
bool intersects(const Rect< T > &rectangle) const
Checks to see if our rectangle overlaps with another.
Definition Rect.hpp:118
const Point2i position(void) const
Definition Rect.hpp:163
Rect(const Point2< T > &pos, const Size2< T > &size)
Construct an object from a Point2 and Size2 container types.
Definition Rect.hpp:76
const Object serialize(void) const
Save an object to a nom::Value object.
Definition Rect.hpp:150
bool contains(const Point2< T > &pos) const
Definition Rect.hpp:112
Rect(void)
Default constructor; initialize values to Rect<T>::null.
Definition Rect.hpp:50
static NOM_EXPORT const Rect zero
Definition Rect.hpp:214
Rect(T left, T top, T right, T bottom)
Construct a Rectangle from coordinates.
Definition Rect.hpp:66
Rect(const Rect< U > &copy)
Copy constructor.
Definition Rect.hpp:91
static NOM_EXPORT const Rect null
Definition Rect.hpp:211
~Rect(void)
Destructor.
Definition Rect.hpp:60
bool contains(T xx, T yy) const
Definition Rect.hpp:102
const Size2i size(void) const
Definition Rect.hpp:169
Size coordinates (width & height) container.
Definition Size2.hpp:58