Gorgon Game Engine
Rectangle.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <iostream>
6 #include <string>
7 #include <sstream>
8 
9 #include "../Types.h"
10 #include "Point.h"
11 #include "Size.h"
12 #include "Bounds.h"
13 
14 namespace Gorgon { namespace Geometry {
15 
18  template <class T_>
20  public:
22  typedef T_ BaseType;
23 
24 
27 
29  basic_Rectangle(const T_ &left, const T_ &top, const T_ &width, const T_ &height) :
30  X(left), Y(top), Width(width), Height(height)
31  { }
32 
34  basic_Rectangle(const basic_Point<T_> &position, const basic_Size<T_> &size) :
35  X(position.X), Y(position.Y), Width(size.Width), Height(size.Height)
36  { }
37 
39  basic_Rectangle(const T_ &left, const T_ &top, const basic_Size<T_> &size) :
40  X(left), Y(top), Width(size.Width), Height(size.Height)
41  { }
42 
44  basic_Rectangle(const basic_Point<T_> &position, T_ width, T_ height) :
45  X(position.X), Y(position.Y), Width(width), Height(height)
46  { }
47 
49  basic_Rectangle(const basic_Point<T_> &topleft, const basic_Point<T_> &bottomright) :
50  X(topleft.X), Y(topleft.Y),
51  Width(bottomright.X-topleft.X), Height(bottomright.Y-topleft.Y)
52  { }
53 
55  template <class O_>
57  X(T_(rect.X)), Y(T_(rect.Y)), Width(T_(rect.Width)), Height(T_(rect.Height))
58  { }
59 
61  template <class O_>
62  basic_Rectangle(const basic_Bounds<O_> &bounds) : X(T_(bounds.Left)), Y(T_(bounds.Top)),
63  Width(T_(bounds.Right-bounds.Left)), Height(T_(bounds.Bottom-bounds.Top))
64  { }
65 
67  explicit basic_Rectangle(const std::string &str) {
68  auto s=str.begin();
69 
70  while(*s==' ' || *s=='\t') s++;
71 
72  if(*s=='<') s++;
73 
74  X=String::To<T_>(&str[s-str.begin()]);
75 
76  while(*s!=',' && s!=str.end()) s++;
77 
78  if(*s==',') s++;
79 
80  Y=String::To<T_>(&str[s-str.begin()]);
81 
82  while(*s!=' ' && s!=str.end()) s++;
83  while(*s==' ' || *s=='\t') s++;
84 
85  Width=String::To<T_>(&str[s-str.begin()]);
86 
87  while(*s!='x' && s!=str.end()) s++;
88 
89  if(*s=='x') s++;
90 
91  Height=String::To<T_>(&str[s-str.begin()]);
92  }
93 
96  static basic_Rectangle Parse(std::string s) {
97  static char tospace[] = ",<>x";
98 
99  std::string::size_type pos;
100  while((pos=s.find_first_of(tospace)) != std::string::npos) {
101  s[pos]=' ';
102  }
103 
104  basic_Rectangle ret;
105 
106  std::istringstream is(s);
107  is>>ret.Left;
108  is>>ret.Top;
109  is>>ret.Width;
110  is>>ret.Height;
111 
112  if(is.fail()) {
113  throw std::runtime_error("Input string is not properly formatted");
114  }
115 
116  return ret;
117  }
118 
120  template <class O_>
121  operator basic_Bounds<O_>() const {
122  return{TopLeft(), GetSize()};
123  }
124 
126  explicit operator std::string() const {
127  std::string str;
128 
129  str.push_back('<');
130  str += String::From(X);
131  str.push_back(',');
132  str += String::From(Y);
133  str.push_back(' ');
134  str += String::From(Width);
135  str.push_back('x');
136  str += String::From(Height);
137  str.push_back('>');
138 
139  return str;
140  }
141 
143  T_ Right() const { return Width +X; }
144 
146  T_ Bottom() const { return Height+Y; }
147 
149  void SetRight(const T_ &right) {
150  if(right>X)
151  Width=right-X;
152  else {
153  Width=X-right;
154  X=right;
155  }
156  }
157 
159  void SetBottom(const T_ &bottom) {
160  if(bottom>Y)
161  Height=bottom-Y;
162  else {
163  Height=Y-bottom;
164  Y=bottom;
165  }
166  }
167 
169  void Resize(const T_ &w, const T_ &h) {
170  Width=w;
171  Height=h;
172  }
173 
175  void Resize(const basic_Size<T_> &s) {
176  Width=s.Width;
177  Height=s.Height;
178  }
179 
181  void Move(const basic_Point<T_> &p) {
182  X = p.X;
183  Y = p.Y;
184  }
185 
187  void Move(const T_ &x, const T_ &y) {
188  X=x;
189  Y=y;
190  }
191 
194  return{X+Width/2, Y+Height/2};
195  }
196 
199  return{X, Y};
200  }
201 
204  return{X+Width, Y};
205  }
206 
209  return{X, Y+Height};
210  }
211 
214  return{X+Width, Y+Height};
215  }
216 
219  return{Width, Height};
220  }
221 
223  bool operator ==(const basic_Rectangle &r) const {
224  return X==r.X && Y==r.Y && Width==r.Width && Height==r.Height;
225  }
226 
228  bool operator !=(const basic_Rectangle &r) const {
229  return X!=r.X || Y!=r.Y || Width!=r.Width || Height!=r.Height;
230  }
231 
234  return{X+amount.X, Y+amount.Y, Width, Height};
235  }
236 
239  return{X, Y, Width+amount.Width, Height+amount.Height};
240  }
241 
244  return{X-amount.X, Y-amount.Y, Width, Height};
245  }
246 
249  return{X, Y, Width-amount.Width, Height-amount.Height};
250  }
251 
254  template<class O_>
256  return {X, Y, T_(Width*amount.Width), T_(Height*amount.Height)};
257  }
258 
261  template<class O_>
263  return {X, Y, T_(Width/amount.Width), T_(Height/amount.Height)};
264  }
265 
268  template<class O_>
269  basic_Rectangle operator *(const O_ &amount) const {
270  return {X, Y, Width*amount, Height*amount};
271  }
272 
275  template<class O_>
276  basic_Rectangle operator /(const O_ &amount) const {
277  return {X, Y, Width/amount, Height/amount};
278  }
279 
280 
283  X+=amount.X;
284  Y+=amount.Y;
285 
286  return *this;
287  }
288 
291  Width +=amount.Width;
292  Height+=amount.Height;
293 
294  return *this;
295  }
296 
299  X-=amount.X;
300  Y -=amount.Y;
301 
302  return *this;
303  }
304 
307  Width -=amount.Width;
308  Height-=amount.Height;
309 
310  return *this;
311  }
312 
315  template<class O_>
317  Width =T_(Width *amount.Width);
318  Height =T_(Height*amount.Height);
319 
320  return *this;
321  }
322 
325  template<class O_>
327  Width =T_(Width /amount.Width);
328  Height =T_(Height/amount.Height);
329 
330  return *this;
331  }
332 
335  template<class O_>
336  basic_Rectangle &operator *=(const O_ &amount) {
337  Width =T_(Width *amount);
338  Height =T_(Height*amount);
339 
340  return *this;
341  }
342 
345  template<class O_>
346  basic_Rectangle &operator /=(const O_ &amount) {
347  Width =T_(Width /amount);
348  Height =T_(Height/amount);
349 
350  return *this;
351  }
352 
354  T_ X;
355 
357  T_ Y;
358 
360  T_ Width;
361 
363  T_ Height;
364  };
365 
367  template <class T_>
368  std::ostream &operator << (std::ostream &out, const basic_Rectangle<T_> &Rectangle) {
369  out<<"<"<<Rectangle.X<<","<<Rectangle.Y<<" "<<Rectangle.Width<<"x"<<Rectangle.Height<<">";
370 
371  return out;
372  }
373 
376  template <class T_>
377  std::istream &operator >> (std::istream &in, basic_Rectangle<T_> &rect) {
378  while(in.peek()==' ' || in.peek()=='<')
379  in.ignore(1);
380 
381  std::string s;
382  std::stringstream ss;
383 
384  while(in.peek()!=',' && !in.eof())
385  s.append(1, (char)in.get());
386 
387  in.ignore(1);
388 
389  ss.str(s);
390  ss>>rect.Left;
391 
392  s="";
393 
394  while(in.peek()==' ' || in.peek()=='\t')
395  in.ignore(1);
396 
397  while(in.peek()!=',' && !in.eof())
398  s.append(1, (char)in.get());
399 
400  in.ignore(1);
401 
402  ss.str(s);
403  ss.clear();
404  ss>>rect.Top;
405 
406  s="";
407 
408  while(in.peek()==' ' || in.peek()=='\t')
409  in.ignore(1);
410 
411  while(in.peek()!=',' && in.peek()!='x' && !in.eof())
412  s.append(1, (char)in.get());
413 
414  in.ignore(1);
415 
416  ss.str(s);
417  ss.clear();
418  ss>>rect.Width;
419 
420  s="";
421 
422  while(in.peek()==' ' || in.peek()=='\t')
423  in.ignore(1);
424 
425  while(in.peek()!='>' && in.peek()!=' ' && in.peek()!='\t' && in.peek()!='\n' && in.peek()!='\r' && !in.eof())
426  s.append(1, in.get());
427 
428 
429  ss.str(s);
430  ss.clear();
431  ss>>rect.Height;
432 
433  if(in.peek()=='>')
434  in.ignore(1);
435 
436  return in;
437  }
438 
440  template<class T_>
441  bool IsInside(const basic_Rectangle<T_> &r, const basic_Point<T_> &p) {
442  return p.X>=r.X && p.Y>=r.Y && p.X<r.Right() && p.Y<r.Bottom();
443  }
444 
447 
450 
451 } }
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle(const std::string &str)
Conversion from string.
Definition: Rectangle.h:67
Gorgon::Geometry::basic_Rectangle::TopLeft
basic_Point< T_ > TopLeft() const
Returns the top left coordinates of the rectangle.
Definition: Rectangle.h:198
Gorgon::Geometry::basic_Bounds
This class represents boundaries of 2D objects.
Definition: Bounds.h:27
Gorgon::Geometry::IsInside
bool IsInside(const basic_Bounds< T_ > &b, const basic_Point< T_ > &p)
Checks whether the given point is inside this bounds.
Definition: Bounds.h:514
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle(const basic_Bounds< O_ > &bounds)
Converting constructor from bounds.
Definition: Rectangle.h:62
Gorgon::String::From
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string >::type From(const T_ &e)
Definition: Enum.h:303
Gorgon::Geometry::basic_Rectangle::operator*
basic_Rectangle operator*(const basic_Size< O_ > &amount) const
Scales this rectangle with the given size.
Definition: Rectangle.h:255
Gorgon::Geometry::basic_Rectangle::operator==
bool operator==(const basic_Rectangle &r) const
Compares two rectangles.
Definition: Rectangle.h:223
Gorgon::Geometry::Rectanglef
basic_Rectangle< Float > Rectanglef
Definition: Rectangle.h:446
Gorgon::Geometry::basic_Rectangle::Right
T_ Right() const
Calculates and returns the rightmost coordinate.
Definition: Rectangle.h:143
Gorgon::Geometry::basic_Rectangle::Move
void Move(const T_ &x, const T_ &y)
Changes the position of the rectangle.
Definition: Rectangle.h:187
Gorgon::Geometry::Rectangle
basic_Rectangle< int > Rectangle
Definition: Rectangle.h:449
Bounds.h
contains the Bounds class
Point.h
contains point class.
Gorgon::Geometry::basic_Point::X
T_ X
X coordinate.
Definition: Point.h:368
Gorgon::Geometry::basic_Rectangle::X
T_ X
X coordinate of the top left corner of this rectangle.
Definition: Rectangle.h:354
Gorgon::Geometry::basic_Rectangle::operator*=
basic_Rectangle & operator*=(const basic_Size< O_ > &amount)
Scales this rectangle with the given size.
Definition: Rectangle.h:316
Size.h
contains the Size class
Gorgon::Geometry::basic_Size::Height
T_ Height
Height of this size object.
Definition: Size.h:261
Gorgon::Geometry::basic_Rectangle::Center
basic_Point< T_ > Center() const
Returns the center point of the rectangle.
Definition: Rectangle.h:193
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle()
Default constructor, does not initialize stored values.
Definition: Rectangle.h:26
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle(const T_ &left, const T_ &top, const T_ &width, const T_ &height)
Filling constructor using values for top left corner and size.
Definition: Rectangle.h:29
Gorgon::Geometry::operator>>
std::istream & operator>>(std::istream &in, basic_Bounds< T_ > &bounds)
Stream extractor for bounds.
Definition: Bounds.h:423
Gorgon::Geometry::basic_Rectangle::Width
T_ Width
Width of the rectangle.
Definition: Rectangle.h:360
Gorgon::Geometry::basic_Rectangle::GetSize
basic_Size< T_ > GetSize() const
Returns the size of the rectangle.
Definition: Rectangle.h:218
Gorgon::Geometry::basic_Rectangle::Parse
static basic_Rectangle Parse(std::string s)
Properly parses a rectangle, throwing errors when necessary.
Definition: Rectangle.h:96
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::Geometry::basic_Rectangle::Move
void Move(const basic_Point< T_ > &p)
Changes the position of the rectangle.
Definition: Rectangle.h:181
Gorgon::Geometry::basic_Rectangle::Resize
void Resize(const basic_Size< T_ > &s)
Resizes the rectangle.
Definition: Rectangle.h:175
Gorgon::Geometry::basic_Rectangle::BaseType
T_ BaseType
Base type of the rectangle elements.
Definition: Rectangle.h:22
Gorgon::Geometry::basic_Rectangle::Bottom
T_ Bottom() const
Calculates and returns the bottommost coordinate.
Definition: Rectangle.h:146
Gorgon::Geometry::basic_Rectangle::SetRight
void SetRight(const T_ &right)
Moves the rectangle by changing its rightmost coordinate.
Definition: Rectangle.h:149
Gorgon::Geometry::basic_Rectangle::TopRight
basic_Point< T_ > TopRight() const
Returns the top right coordinates of the rectangle.
Definition: Rectangle.h:203
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle(const T_ &left, const T_ &top, const basic_Size< T_ > &size)
Filling constructor using values for top left corner and size.
Definition: Rectangle.h:39
Gorgon::Geometry::basic_Rectangle::operator+
basic_Rectangle operator+(const basic_Point< T_ > &amount) const
Adds a point to this rectangle, effectively translating it.
Definition: Rectangle.h:233
Gorgon::Geometry::basic_Rectangle::Resize
void Resize(const T_ &w, const T_ &h)
Resizes the rectangle.
Definition: Rectangle.h:169
Gorgon::Geometry::basic_Rectangle::SetBottom
void SetBottom(const T_ &bottom)
Moves the rectangle by changing its bottommost coordinate.
Definition: Rectangle.h:159
Gorgon::Geometry::basic_Rectangle::Y
T_ Y
Y coordinate of the top left corner of this rectangle.
Definition: Rectangle.h:357
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle(const basic_Point< T_ > &position, const basic_Size< T_ > &size)
Filling constructor using values for top left corner and size.
Definition: Rectangle.h:34
Gorgon::Geometry::basic_Rectangle::operator-=
basic_Rectangle & operator-=(const basic_Point< T_ > &amount)
Subtracts a point to this rectangle, effectively translating it.
Definition: Rectangle.h:298
Gorgon::Geometry::basic_Rectangle::Height
T_ Height
Height of the rectangle.
Definition: Rectangle.h:363
Gorgon::Geometry::basic_Rectangle::operator/=
basic_Rectangle & operator/=(const basic_Size< O_ > &amount)
Scales this rectangle with the given size.
Definition: Rectangle.h:326
Gorgon::Geometry::basic_Point::Y
T_ Y
Y coordinate.
Definition: Point.h:371
Gorgon::Geometry::operator<<
std::ostream & operator<<(std::ostream &out, const basic_Bounds< T_ > &bounds)
Allows streaming of bounds.
Definition: Bounds.h:415
Gorgon::Geometry::basic_Rectangle::BottomLeft
basic_Point< T_ > BottomLeft() const
Returns the bottom left coordinates of the rectangle.
Definition: Rectangle.h:208
Gorgon::Geometry::basic_Size::Width
T_ Width
Width of this size object.
Definition: Size.h:258
Gorgon::Geometry::basic_Rectangle::BottomRight
basic_Point< T_ > BottomRight() const
Returns the bottom right coordinates of the rectangle.
Definition: Rectangle.h:213
Gorgon::Geometry::basic_Rectangle::operator-
basic_Rectangle operator-(const basic_Point< T_ > &amount) const
Subtracts a point from this rectangle, effectively translating it.
Definition: Rectangle.h:243
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle(const basic_Point< T_ > &position, T_ width, T_ height)
Filling constructor using values for top left corner and size.
Definition: Rectangle.h:44
Gorgon::Geometry::basic_Rectangle::operator!=
bool operator!=(const basic_Rectangle &r) const
Compares two rectangles.
Definition: Rectangle.h:228
Gorgon::Geometry::basic_Rectangle
Represents a rectangle in a 2D space.
Definition: Rectangle.h:19
Gorgon::Geometry::basic_Rectangle::operator+=
basic_Rectangle & operator+=(const basic_Point< T_ > &amount)
Adds a point to this rectangle, effectively translating it.
Definition: Rectangle.h:282
Gorgon::Input::Keyboard::Keycodes::Left
constexpr Key Left
Definition: Keyboard.h:62
Gorgon::Geometry::basic_Rectangle::operator/
basic_Rectangle operator/(const basic_Size< O_ > &amount) const
Scales this rectangle with the given size.
Definition: Rectangle.h:262
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle(const basic_Rectangle< O_ > &rect)
Converting constructor from a different typed rectangle.
Definition: Rectangle.h:56
Gorgon::Geometry::basic_Rectangle::basic_Rectangle
basic_Rectangle(const basic_Point< T_ > &topleft, const basic_Point< T_ > &bottomright)
Filling constructor using values for top left and bottom right corners.
Definition: Rectangle.h:49