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 "nomlib/config.hpp"
33#include "nomlib/math/Point2.hpp"
34#include "nomlib/math/Size2.hpp"
35#include "nomlib/ptree/Value.hpp"
36
37namespace nom {
38
40const std::string RECT_DELIMITER = ", ";
41
45template<typename T>
46struct Rect
47{
49 Rect ( void ) :
50 x ( -1 ),
51 y ( -1 ),
52 w ( -1 ),
53 h ( -1 )
54 {
55 //NOM_LOG_TRACE(NOM);
56 }
57
59 ~Rect ( void )
60 {
61 //NOM_LOG_TRACE(NOM);
62 }
63
65 Rect ( T left, T top, T right, T bottom ) :
66 x ( left ),
67 y ( top ),
68 w ( right ),
69 h ( bottom )
70 {
71 //NOM_LOG_TRACE(NOM);
72 }
73
75 Rect ( const Point2<T>& pos, const Size2<T>& size ) :
76 x ( pos.x ),
77 y ( pos.y ),
78 w ( size.w ),
79 h ( size.h )
80 {
81 //NOM_LOG_TRACE(NOM);
82 }
83
89 template <typename U>
90 explicit Rect ( const Rect<U>& copy ) :
91 x { static_cast<T> ( copy.x ) },
92 y { static_cast<T> ( copy.y ) },
93 w { static_cast<T> ( copy.w ) },
94 h { static_cast<T> ( copy.h ) }
95 {
96 //NOM_LOG_TRACE(NOM);
97 }
98
101 bool contains ( T x, T y ) const
102 {
103 return (
104 ( x >= this->x && x <= this->x + this->w ) &&
105 ( y >= this->y && y <= this->y + this->h )
106 );
107 }
108
111 bool contains ( const Point2<T>& pos ) const
112 {
113 return this->contains ( pos.x, pos.y );
114 }
115
117 bool intersects ( const Rect<T>& rectangle ) const
118 {
119 T leftA, leftB = 0;
120 T rightA, rightB = 0;
121 T topA, topB = 0;
122 T bottomA, bottomB = 0;
123
124 // Calculate sides of RectA
125 leftA = this->x;
126 rightA = leftA + this->w;
127 topA = this->y;
128 bottomA = topA + this->h;
129
130 // Calculate sides of RectB
131 leftB = rectangle.x;
132 rightB = leftB + rectangle.w;
133 topB = rectangle.y;
134 bottomB = topB + rectangle.h;
135
136 if ( bottomA <= topB ) return false; // No collision
137 if ( topA >= bottomB ) return false; // No collision
138 if ( rightA <= leftB ) return false; // No collision
139 if ( leftA >= rightB ) return false; // No collision
140
141 return true; // Collision!
142 }
143
149 const Object serialize( void ) const
150 {
151 Object object;
152
153 object["x"] = this->x;
154 object["y"] = this->y;
155 object["width"] = this->w;
156 object["height"] = this->h;
157
158 return object;
159 }
160
162 const Point2i position( void ) const
163 {
164 return Point2i( NOM_SCAST( T, this->x ), NOM_SCAST( T, this->y ) );
165 }
166
168 const Size2i size( void ) const
169 {
170 return Size2i( NOM_SCAST( T, this->w ), NOM_SCAST( T, this->h ) );
171 }
172
173 const T& left() const
174 {
175 return this->x;
176 }
177
178 const T& top() const
179 {
180 return this->y;
181 }
182
183 const T& right() const
184 {
185 return(this->x + this->w);
186 }
187
188 const T& bottom() const
189 {
190 return(this->y + this->h);
191 }
192
193 template <typename U>
194 void set_position(const Point2<U>& pos)
195 {
196 this->x = pos.x;
197 this->y = pos.y;
198 }
199
200 template <typename U>
201 void set_size(const Size2<U>& dims)
202 {
203 this->w = dims.w;
204 this->h = dims.h;
205 }
206
210 static const Rect null;
211
213 static const Rect zero;
214
216 T x;
218
219 T y;
221
222 T w;
223
225 T h;
226};
227
243template <typename T>
244inline std::ostream& operator <<( std::ostream& os, const Rect<T>& rect )
245{
246 os
247 << rect.x
248 << RECT_DELIMITER
249 << rect.y
250 << RECT_DELIMITER
251 << rect.w
252 << RECT_DELIMITER
253 << rect.h;
254 return os;
255}
256
265template <typename T>
266inline bool operator == ( const Rect<T>& lhs, const Rect<T>& rhs )
267{
268 return ( lhs.x == rhs.x ) && ( lhs.w == rhs.w )
269 &&
270 ( lhs.y == rhs.y ) && ( lhs.h == rhs.h );
271}
272
281template <typename T>
282inline bool operator != ( const Rect<T>& lhs, const Rect<T>& rhs )
283{
284 return ! ( lhs == rhs );
285}
286
293template <typename T>
294inline Rect<T> operator + ( const Rect<T>& lhs, const Rect<T>& rhs )
295{
296 return Rect<T> ( lhs.x + rhs.x,
297 lhs.y + rhs.y,
298 lhs.w + rhs.w,
299 lhs.h + rhs.h
300 );
301}
302
309template <typename T>
310inline Rect<T> operator +(int lhs, const Rect<T>& rhs)
311{
312 return Rect<T>(lhs + rhs.x, lhs + rhs.y, lhs + rhs.w, lhs + rhs.h);
313}
314
321template <typename T>
322inline Rect<T> operator +(const Rect<T>& lhs, int rhs)
323{
324 return Rect<T>(lhs.x + rhs, lhs.y + rhs, lhs.w + rhs, lhs.h + rhs);
325}
326
332template <typename T>
333inline Rect<T> operator ++ ( const Rect<T>& rhs )
334{
335 return Rect<T> ( rhs.x + 1,
336 rhs.y + 1,
337 rhs.w + 1,
338 rhs.h + 1
339 );
340}
341
347template <typename T>
348inline Rect<T> operator -( const Rect<T>& rhs )
349{
350 return Rect<T> ( -rhs.x,
351 -rhs.y,
352 -rhs.w
353 -rhs.h
354 );
355}
356
363template <typename T>
364inline Rect<T> operator -( const Rect<T>& lhs, const Rect<T>& rhs )
365{
366 return Rect<T> ( lhs.x - rhs.x,
367 lhs.y - rhs.y,
368 lhs.w - rhs.w,
369 lhs.h - rhs.h
370 );
371}
372
379template <typename T>
380inline Rect<T> operator -(int lhs, const Rect<T>& rhs)
381{
382 return Rect<T>(lhs - rhs.x, lhs - rhs.y, lhs - rhs.w, lhs - rhs.h);
383}
384
391template <typename T>
392inline Rect<T> operator -(const Rect<T>& lhs, int rhs)
393{
394 return Rect<T>(lhs.x - rhs, lhs.y - rhs, lhs.w - rhs, lhs.h - rhs);
395}
396
402template <typename T>
403inline Rect<T> operator -- ( const Rect<T>& rhs )
404{
405 return Rect<T> ( rhs.x - 1,
406 rhs.y - 1,
407 rhs.w - 1,
408 rhs.h - 1
409 );
410}
411
418template <typename T>
419inline Rect<T> operator * ( const Rect<T>& lhs, const Rect<T>& rhs )
420{
421 return Rect<T> ( lhs.x * rhs.x,
422 lhs.y * rhs.y,
423 lhs.w * rhs.w,
424 lhs.h * rhs.h
425 );
426}
427
434template <typename T>
435inline Rect<T> operator *(int lhs, const Rect<T>& rhs)
436{
437 return Rect<T>(lhs * rhs.x, lhs * rhs.y, lhs * rhs.w, lhs * rhs.h);
438}
439
446template <typename T>
447inline Rect<T> operator *(const Rect<T>& lhs, int rhs)
448{
449 return Rect<T> ( lhs.x * rhs,
450 lhs.y * rhs,
451 lhs.w * rhs,
452 lhs.h * rhs
453 );
454}
455
464template <typename T>
465inline Rect<T> operator /( const Rect<T>& lhs, const Rect<T>& rhs )
466{
467 return Rect<T> ( lhs.x / rhs.x,
468 lhs.y / rhs.y,
469 lhs.w / rhs.w,
470 lhs.h / rhs.h
471 );
472}
473
482template <typename T>
483inline Rect<T> operator /(int lhs, const Rect<T>& rhs)
484{
485 return Rect<T> ( lhs / rhs.x,
486 lhs / rhs.y,
487 lhs / rhs.w,
488 lhs / rhs.h
489 );
490}
491
500template <typename T>
501inline Rect<T> operator /(const Rect<T>& lhs, int rhs)
502{
503 return Rect<T> ( lhs.x / rhs,
504 lhs.y / rhs,
505 lhs.w / rhs,
506 lhs.h / rhs
507 );
508}
509
519template <typename T>
520inline Rect<T>& operator +=( Rect<T>& lhs, const Rect<T>& rhs )
521{
522 lhs.x += rhs.x;
523 lhs.y += rhs.y;
524 lhs.w += rhs.w;
525 lhs.h += rhs.h;
526
527 return lhs;
528}
529
539template <typename T>
540inline Rect<T>& operator +=(int lhs, const Rect<T>& rhs)
541{
542 lhs += rhs.x;
543 lhs += rhs.y;
544 lhs += rhs.w;
545 lhs += rhs.h;
546
547 return lhs;
548}
549
559template <typename T>
560inline Rect<T>& operator +=(Rect<T>& lhs, int rhs)
561{
562 lhs.x += rhs;
563 lhs.y += rhs;
564 lhs.w += rhs;
565 lhs.h += rhs;
566
567 return lhs;
568}
569
579template <typename T>
580inline Rect<T>& operator -=( Rect<T>& lhs, const Rect<T>& rhs )
581{
582 lhs.x -= rhs.x;
583 lhs.y -= rhs.y;
584 lhs.w -= rhs.w;
585 lhs.h -= rhs.h;
586
587 return lhs;
588}
589
599template <typename T>
600inline Rect<T>& operator -=(int lhs, const Rect<T>& rhs)
601{
602 lhs -= rhs.x;
603 lhs -= rhs.y;
604 lhs -= rhs.w;
605 lhs -= rhs.h;
606
607 return lhs;
608}
609
619template <typename T>
620inline Rect<T>& operator -=(Rect<T>& lhs, int rhs)
621{
622 lhs.x -= rhs;
623 lhs.y -= rhs;
624 lhs.w -= rhs;
625 lhs.h -= rhs;
626
627 return lhs;
628}
629
639template <typename T>
640inline Rect<T>& operator *=( Rect<T>& lhs, const Rect<T>& rhs )
641{
642 lhs.x *= rhs.x;
643 lhs.y *= rhs.y;
644 lhs.w *= rhs.w;
645 lhs.h *= rhs.h;
646
647 return lhs;
648}
649
659template <typename T>
660inline Rect<T>& operator *=(int lhs, const Rect<T>& rhs)
661{
662 lhs *= rhs.x;
663 lhs *= rhs.y;
664 lhs *= rhs.w;
665 lhs *= rhs.h;
666
667 return lhs;
668}
669
679template <typename T>
680inline Rect<T>& operator *=(Rect<T>& lhs, int rhs)
681{
682 lhs.x *= rhs;
683 lhs.y *= rhs;
684 lhs.w *= rhs;
685 lhs.h *= rhs;
686
687 return lhs;
688}
689
699template <typename T>
700inline Rect<T>& operator /=( Rect<T>& lhs, const Rect<T>& rhs )
701{
702 lhs.x /= rhs.x;
703 lhs.y /= rhs.y;
704 lhs.w /= rhs.w;
705 lhs.h /= rhs.h;
706
707 return lhs;
708}
709
719template <typename T>
720inline Rect<T>& operator /=(int lhs, const Rect<T>& rhs)
721{
722 lhs /= rhs.x;
723 lhs /= rhs.y;
724 lhs /= rhs.w;
725 lhs /= rhs.h;
726
727 return lhs;
728}
729
739template <typename T>
740inline Rect<T>& operator /=(Rect<T>& lhs, int rhs)
741{
742 lhs.x /= rhs;
743 lhs.y /= rhs;
744 lhs.w /= rhs;
745 lhs.h /= rhs;
746
747 return lhs;
748}
749
754template <typename T>
755inline bool operator <( const Rect<T> lhs, const Rect<T>& rhs )
756{
757 return ( lhs.x < rhs.x ) && ( lhs.y < rhs.y )
758 &&
759 ( lhs.w < rhs.w ) && ( lhs.h < rhs.h );
760}
761
766template <typename T>
767inline bool operator >( const Rect<T>& lhs, const Rect<T>& rhs )
768{
769 return ( rhs.x < lhs.x ) && ( rhs.y < lhs.y )
770 &&
771 ( rhs.w < lhs.w ) && ( rhs.h < lhs.h );
772}
773
778template <typename T>
779inline bool operator <=( const Rect<T>& lhs, const Rect<T>& rhs )
780{
781 return ( lhs.x <= rhs.x ) && ( lhs.y <= rhs.y )
782 &&
783 ( lhs.w <= rhs.w ) && ( lhs.h <= rhs.h );
784}
785
790template <typename T>
791inline bool operator >=( const Rect<T>& lhs, const Rect<T>& rhs )
792{
793 return ( rhs.x <= lhs.x ) && ( rhs.y <= lhs.y )
794 &&
795 ( rhs.w <= lhs.w ) && ( rhs.h <= lhs.h );
796}
797
799typedef Rect<int> IntRect;
800
802typedef Rect<float> FloatRect;
803
805typedef Rect<double> DoubleRect;
806
807} // namespace nom
808
809#endif // include guard defined
2D point container
Definition Point2.hpp:44
T x
Represents the X-axis coordinate point.
Definition Point2.hpp:95
T y
Represents the Y-axis coordinate point.
Definition Point2.hpp:98
Rectangle shape container.
Definition Rect.hpp:47
bool intersects(const Rect< T > &rectangle) const
Checks to see if our rectangle overlaps with another.
Definition Rect.hpp:117
bool contains(T x, T y) const
Definition Rect.hpp:101
const Point2i position(void) const
Definition Rect.hpp:162
Rect(const Point2< T > &pos, const Size2< T > &size)
Construct an object from a Point2 and Size2 container types.
Definition Rect.hpp:75
const Object serialize(void) const
Save an object to a nom::Value object.
Definition Rect.hpp:149
static const Rect null
Definition Rect.hpp:210
bool contains(const Point2< T > &pos) const
Definition Rect.hpp:111
Rect(void)
Default constructor; initialize values to Rect<T>::null.
Definition Rect.hpp:49
static const Rect zero
Definition Rect.hpp:213
Rect(T left, T top, T right, T bottom)
Construct a Rectangle from coordinates.
Definition Rect.hpp:65
Rect(const Rect< U > &copy)
Copy constructor.
Definition Rect.hpp:90
~Rect(void)
Destructor.
Definition Rect.hpp:59
const Size2i size(void) const
Definition Rect.hpp:168
Size coordinates (width & height) container.
Definition Size2.hpp:57