Coords.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. 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 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON 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
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/math/Coords.hpp"
30 
31 namespace nom {
32 
33 const Coords Coords::null ( -1, -1, -1, -1 );
34 
35 Coords::Coords ( void ) : x ( 0 ), y ( 0 ), width ( 0 ), height ( 0 ) {}
36 
37 Coords::~Coords ( void ) {}
38 
39 Coords::Coords ( int32 x, int32 y, int32 w, int32 h ) : x ( x ),
40  y ( y ),
41  width ( w ),
42  height ( h )
43 {}
44 
45 Coords::Coords ( int32 x, int32 y ) : x ( x ), y ( y ),
46  width ( 0 ), height ( 0 )
47 {}
48 
49 Coords::Coords ( const Coords& coords ) : x ( coords.x ), y ( coords.y ),
50  width ( coords.width ),
51  height ( coords.height )
52 {}
53 
54 Coords Coords::getPosition ( void ) const
55 {
56  return Coords ( this->x, this->y );
57 }
58 
59 Coords Coords::getSize ( void ) const
60 {
61  return Coords ( this->width, this->height );
62 }
63 
64 void Coords::setPosition ( int32 x, int32 y )
65 {
66  this->x = x;
67  this->y = y;
68 }
69 
70 void Coords::setSize ( int32 width, int32 height )
71 {
72  this->width = width;
73  this->height = height;
74 }
75 
76 bool Coords::contains ( int32 x, int32 y ) const
77 {
78  return (
79  ( x >= this->x && x <= this->x + this->width ) &&
80  ( y >= this->y && y <= this->y + this->height )
81  );
82 }
83 
84 bool Coords::contains ( const Coords& pos ) const
85 {
86  return this->contains ( pos.x, pos.y );
87 }
88 
89 bool Coords::intersects ( Coords& rectangle ) const
90 {
91  uint32 leftA, leftB = 0;
92  uint32 rightA, rightB = 0;
93  uint32 topA, topB = 0;
94  uint32 bottomA, bottomB = 0;
95 
96  // Calculate sides of RectA
97  leftA = this->x;
98  rightA = this->x + this->width;
99  topA = this->y;
100  bottomA = this->y + this->height;
101 
102  // Calculate sides of RectB
103  leftB = rectangle.x;
104  rightB = rectangle.x + rectangle.width;
105  topB = rectangle.y;
106  bottomB = rectangle.y + rectangle.height;
107 
108  if ( bottomA <= topB ) return false;
109  if ( topA >= bottomB ) return false;
110  if ( rightA <= leftB ) return false;
111  if ( leftA >= rightB ) return false;
112 
113  return true; // we've got a collision!
114 }
115 
116 bool Coords::isNull ( void ) const
117 {
118  if ( *this == Coords::null )
119  {
120  return true;
121  }
122 
123  return false;
124 }
125 
126 const Coords& Coords::get ( void ) const
127 {
128  return *this;
129 }
130 
131 std::ostream& operator << ( std::ostream& os, const Coords& coords )
132 {
133  os << coords.x << coords_delimiter
134  << coords.y << coords_delimiter
135  << coords.width << coords_delimiter
136  << coords.height;
137  return os;
138 }
139 
141 {
142  this->x = other.x;
143  this->y = other.y;
144  this->width = other.width;
145  this->height = other.height;
146 
147  return *this;
148 }
149 
150 bool operator == ( const Coords& left, const Coords& right )
151 {
152  return (left.x == right.x ) &&
153  (left.y == right.y ) &&
154  (left.width == right.width ) &&
155  (left.height == right.height );
156 }
157 
158 bool operator != ( const Coords& left, const Coords& right )
159 {
160  return ! ( left == right );
161 }
162 
163 Coords operator + ( const Coords& left, const Coords& right )
164 {
165  return Coords ( static_cast<int32> ( left.x + right.x ),
166  static_cast<int32> ( left.y + right.y ),
167  static_cast<int32> ( left.width + right.width ),
168  static_cast<int32> ( left.height + right.height )
169  );
170 }
171 
172 Coords operator ++ ( Coords& left )
173 {
174  return Coords ( static_cast<int32> ( left.x ++ ),
175  static_cast<int32> ( left.y ++ ),
176  static_cast<int32> ( left.width ++ ),
177  static_cast<int32> ( left.height ++ )
178  );
179 }
180 
181 Coords operator - ( const Coords& left, const Coords& right )
182 {
183  return Coords ( static_cast<int32> ( left.x - right.x ),
184  static_cast<int32> ( left.y - right.y ),
185  static_cast<int32> ( left.width - right.width ),
186  static_cast<int32> ( left.height - right.height )
187  );
188 }
189 
190 Coords operator -- ( Coords& left )
191 {
192  return Coords ( static_cast<int32> ( left.x -- ),
193  static_cast<int32> ( left.y -- ),
194  static_cast<int32> ( left.width -- ),
195  static_cast<int32> ( left.height -- )
196  );
197 }
198 
199 Coords operator * ( const Coords& left, const Coords& right )
200 {
201  return Coords ( static_cast<int32> ( left.x * right.x ),
202  static_cast<int32> ( left.y * right.y ),
203  static_cast<int32> ( left.width * right.width ),
204  static_cast<int32> ( left.height * right.height )
205  );
206 }
207 
208 Coords operator / ( const Coords& left, const Coords& right )
209 {
210  return Coords ( static_cast<int32> ( left.x / right.x ),
211  static_cast<int32> ( left.y / right.y ),
212  static_cast<int32> ( left.width / right.width ),
213  static_cast<int32> ( left.height / right.height )
214  );
215 }
216 
217 Coords& operator += ( Coords& left, const Coords& right )
218 {
219  return left = left + right;
220 }
221 
222 Coords& operator -= ( Coords& left, const Coords& right )
223 {
224  return left = left - right;
225 }
226 
227 Coords& operator *= ( Coords& left, const Coords& right )
228 {
229  return left = left * right;
230 }
231 
232 
233 } // namespace nom
Coords & operator=(const Coords &other)
Copy assignment operator.
Definition: Coords.cpp:140
bool isNull(void) const
Definition: Coords.cpp:116
const Coords & get(void) const
Convenience getter for obtaining a copy of this object.
Definition: Coords.cpp:126
~Coords(void)
Destructor.
Definition: Coords.cpp:37
static const Coords null
Convenience object that will always contain a value of -1.
Definition: Coords.hpp:94
Coords(void)
Default constructor; sets all values to their respective defaults.
Definition: Coords.cpp:35
bool intersects(Coords &rectangle) const
Checks to see if our rectangle overlaps with another.
Definition: Coords.cpp:89
bool contains(int32 x, int32 y) const
Definition: Coords.cpp:76
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44