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