SRect2.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <ionCore.h>
5 
6 
7 namespace ion
8 {
9 
12  template <typename T>
13  class SRect2
14  {
15 
16  public:
17 
19 
21  {}
22 
23  SRect2(vec2<T> const & position, vec2<T> const & size)
24  : Position(position), Size(size)
25  {}
26 
27  SRect2(T const x, T const y, T const w, T const h)
28  : Position(x, y), Size(w, h)
29  {}
30 
32  {
33  return Position + Size;
34  }
35 
37  {
38  return Position + Size / 2;
39  }
40 
41  bool Intersects(SRect2<T> const & r) const
42  {
43  return (OtherCorner().Y > r.Position.Y || Equals(OtherCorner().Y, r.Position.Y)) &&
44  (Position.Y < r.OtherCorner().Y || Equals(Position.Y, r.OtherCorner().Y)) &&
45  (OtherCorner().X > r.Position.X || Equals(OtherCorner().X, r.Position.X)) &&
46  (Position.X < r.OtherCorner().X || Equals(Position.X, r.OtherCorner().X));
47  }
48 
49  SRect2 GetIntersection(SRect2<T> const & r) const
50  {
51  vec2<T> Pos(std::max(r.Position.X, Position.X), std::max(r.Position.Y, Position.Y));
52  vec2<T> Corner(std::min(r.OtherCorner().X, OtherCorner().X), std::min(r.OtherCorner().Y, OtherCorner().Y));
53 
54  return SRect2<T>(Pos, Corner - Pos);
55  }
56 
57  bool IsPointInside(vec2<T> const & v) const
58  {
59  return (OtherCorner().Y > v.Y &&
60  Position.Y < v.Y &&
61  OtherCorner().X > v.X &&
62  Position.X < v.X);
63  }
64 
65  bool IsPointInsideOrOn(vec2<T> const & v) const
66  {
67  return (OtherCorner().Y >= v.Y &&
68  Position.Y <= v.Y &&
69  OtherCorner().X >= v.X &&
70  Position.X <= v.X);
71  }
72 
73  T GetArea() const
74  {
75  return Size.X * Size.Y;
76  }
77 
78  void Bounds(vec2<T> const & pos1, vec2<T> const & pos2)
79  {
80  Position = pos1;
81  Size = pos2 - Position;
82  if (Size.X < 0)
83  {
84  Position.X += Size.X;
85  Size.X *= -1;
86  }
87  if (Size.Y < 0)
88  {
89  Position.Y += Size.Y;
90  Size.Y *= -1;
91  }
92  }
93 
94  void ClipTo(SRect2 const & r)
95  {
96  vec2<T> UpperLeftCorner = Position;
97  vec2<T> LowerRightCorner = OtherCorner();
98  vec2<T> const otherUpperLeftCorner = r.Position;
99  vec2<T> const otherLowerRightCorner = r.OtherCorner();
100 
101  if (otherLowerRightCorner.X < LowerRightCorner.X)
102  LowerRightCorner.X = otherLowerRightCorner.X;
103  if (otherLowerRightCorner.Y < LowerRightCorner.Y)
104  LowerRightCorner.Y = otherLowerRightCorner.Y;
105 
106  if (otherUpperLeftCorner.X > UpperLeftCorner.X)
107  UpperLeftCorner.X = otherUpperLeftCorner.X;
108  if (otherUpperLeftCorner.Y > UpperLeftCorner.Y)
109  UpperLeftCorner.Y = otherUpperLeftCorner.Y;
110 
111  // correct possible invalid rect resulting from clipping
112  if (UpperLeftCorner.Y > LowerRightCorner.Y)
113  UpperLeftCorner.Y = LowerRightCorner.Y;
114  if (UpperLeftCorner.X > LowerRightCorner.X)
115  UpperLeftCorner.X = LowerRightCorner.X;
116 
117  Position = UpperLeftCorner;
118  Size = LowerRightCorner - UpperLeftCorner;
119  }
120 
122  // Inequality operator (for sorting) //
124 
125  bool operator < (SRect2<T> const & other) const
126  {
127  return
128  (Position < other.Position) ||
129  (Position == other.Position && Size < other.Size);
130  }
131 
132  bool operator ==(SRect2<T> const & other) const
133  {
134  return other.Position == Position && other.Size == Size;
135  }
136 
137  friend std::ostream & operator << (std::ostream & stream, SRect2<T> const & rect)
138  {
139  return stream << rect.Position << " " << rect.Size;
140  }
141 
142  };
143 
148 
149  typedef SRect2f rect2f;
150  typedef SRect2d rect2d;
151  typedef SRect2i rect2i;
152  typedef SRect2u rect2u;
153 
154 }
2D vector
Definition: vec2.h:17
SRect2< int > SRect2i
Definition: SRect2.h:146
SRect2< ion::uint > SRect2u
Definition: SRect2.h:147
SRect2< double > SRect2d
Definition: SRect2.h:145
SRect2u rect2u
Definition: SRect2.h:152
bool operator==(SRect2< T > const &other) const
Definition: SRect2.h:132
SRect2(T const x, T const y, T const w, T const h)
Definition: SRect2.h:27
SRect2()
Definition: SRect2.h:20
SRect2(vec2< T > const &position, vec2< T > const &size)
Definition: SRect2.h:23
vec2< T > Position
Definition: SRect2.h:18
T X
Definition: vec2.h:20
SRect2< float > SRect2f
Definition: SRect2.h:144
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
bool IsPointInside(vec2< T > const &v) const
Definition: SRect2.h:57
vec2< T > Size
Definition: SRect2.h:18
SRect2 GetIntersection(SRect2< T > const &r) const
Definition: SRect2.h:49
SRect2d rect2d
Definition: SRect2.h:150
SRect2i rect2i
Definition: SRect2.h:151
2D rectangle
Definition: SRect2.h:13
void Bounds(vec2< T > const &pos1, vec2< T > const &pos2)
Definition: SRect2.h:78
SRect2f rect2f
Definition: SRect2.h:149
vec2< T > OtherCorner() const
Definition: SRect2.h:31
T Y
Definition: vec2.h:21
bool IsPointInsideOrOn(vec2< T > const &v) const
Definition: SRect2.h:65
void ClipTo(SRect2 const &r)
Definition: SRect2.h:94
T GetArea() const
Definition: SRect2.h:73
bool Intersects(SRect2< T > const &r) const
Definition: SRect2.h:41
vec2< T > GetCenter() const
Definition: SRect2.h:36