doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Rectangle.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 #ifndef IDRECTANGLE_H_
29 #define IDRECTANGLE_H_
30 //
31 // simple rectangle
32 //
33 extern void RotateVector(idVec3 &v, idVec3 origin, float a, float c, float s);
34 class idRectangle {
35 public:
36  float x; // horiz position
37  float y; // vert position
38  float w; // width
39  float h; // height;
40  idRectangle() { x = y = w= h = 0.0; }
41  idRectangle(float ix, float iy, float iw, float ih) { x = ix; y = iy; w = iw; h = ih; }
42  float Bottom() const { return y + h; }
43  float Right() const { return x + w; }
44  void Offset (float x, float y) {
45  this->x += x;
46  this->y += y;
47  }
48  bool Contains(float xt, float yt) {
49  if (w == 0.0 && h == 0.0) {
50  return false;
51  }
52  if (xt >= x && xt <= Right() && yt >= y && yt <= Bottom()) {
53  return true;
54  }
55  return false;
56  }
57  void Empty() { x = y = w = h = 0.0; };
58 
59  void ClipAgainst(idRectangle r, bool sizeOnly) {
60  if (!sizeOnly) {
61  if (x < r.x) {
62  x = r.x;
63  }
64  if (y < r.y) {
65  y = r.y;
66  }
67  }
68  if (x + w > r.x + r.w) {
69  w = (r.x + r.w) - x;
70  }
71  if (y + h > r.y + r.h) {
72  h = (r.y + r.h) - y;
73  }
74  }
75 
76 
77 
78  void Rotate(float a, idRectangle &out) {
79  idVec3 p1, p2, p3, p4, p5;
80  float c, s;
81  idVec3 center;
82  center.Set((x + w) / 2.0, (y + h) / 2.0, 0);
83  p1.Set(x, y, 0);
84  p2.Set(Right(), y, 0);
85  p4.Set(x, Bottom(), 0);
86  if (a) {
87  s = sin( DEG2RAD( a ) );
88  c = cos( DEG2RAD( a ) );
89  }
90  else {
91  s = c = 0;
92  }
93  RotateVector(p1, center, a, c, s);
94  RotateVector(p2, center, a, c, s);
95  RotateVector(p4, center, a, c, s);
96  out.x = p1.x;
97  out.y = p1.y;
98  out.w = (p2 - p1).Length();
99  out.h = (p4 - p1).Length();
100  }
101 
102  idRectangle & operator+=( const idRectangle &a );
103  idRectangle & operator-=( const idRectangle &a );
104  idRectangle & operator/=( const idRectangle &a );
105  idRectangle & operator/=( const float a );
106  idRectangle & operator*=( const float a );
107  idRectangle & operator=( const idVec4 v );
108  int operator==(const idRectangle &a) const;
109  float & operator[]( const int index );
110  char * String( void ) const;
111  const idVec4& ToVec4() const;
112 
113 };
114 
115 ID_INLINE const idVec4 &idRectangle::ToVec4() const {
116  return *reinterpret_cast<const idVec4 *>(&x);
117 }
118 
119 
121  x += a.x;
122  y += a.y;
123  w += a.w;
124  h += a.h;
125 
126  return *this;
127 }
128 
130  x /= a.x;
131  y /= a.y;
132  w /= a.w;
133  h /= a.h;
134 
135  return *this;
136 }
137 
138 ID_INLINE idRectangle &idRectangle::operator/=( const float a ) {
139  float inva = 1.0f / a;
140  x *= inva;
141  y *= inva;
142  w *= inva;
143  h *= inva;
144 
145  return *this;
146 }
147 
149  x -= a.x;
150  y -= a.y;
151  w -= a.w;
152  h -= a.h;
153 
154  return *this;
155 }
156 
157 ID_INLINE idRectangle &idRectangle::operator*=( const float a ) {
158  x *= a;
159  y *= a;
160  w *= a;
161  h *= a;
162 
163  return *this;
164 }
165 
166 
168  x = v.x;
169  y = v.y;
170  w = v.z;
171  h = v.w;
172  return *this;
173 }
174 
175 ID_INLINE int idRectangle::operator==( const idRectangle &a ) const {
176  return (x == a.x && y == a.y && w == a.w && a.h);
177 }
178 
179 ID_INLINE float& idRectangle::operator[]( int index ) {
180  return ( &x )[ index ];
181 }
182 
183 class idRegion {
184 public:
185  idRegion() { };
186 
187  void Empty() {
188  rects.Clear();
189  }
190 
191  bool Contains(float xt, float yt) {
192  int c = rects.Num();
193  for (int i = 0; i < c; i++) {
194  if (rects[i].Contains(xt, yt)) {
195  return true;
196  }
197  }
198  return false;
199  }
200 
201  void AddRect(float x, float y, float w, float h) {
202  rects.Append(idRectangle(x, y, w, h));
203  }
204 
205  int GetRectCount() {
206  return rects.Num();
207  }
208 
210  if (index >= 0 && index < rects.Num()) {
211  return &rects[index];
212  }
213  return NULL;
214  }
215 
216 protected:
217 
219 };
220 
221 
222 #endif
idRectangle * GetRect(int index)
Definition: Rectangle.h:209
idRectangle & operator-=(const idRectangle &a)
Definition: Rectangle.h:148
float y
Definition: Vector.h:811
const GLdouble * v
Definition: glext.h:2936
void Offset(float x, float y)
Definition: Rectangle.h:44
void Set(const float x, const float y, const float z)
Definition: Vector.h:409
GLenum GLint GLint y
Definition: glext.h:2849
float z
Definition: Vector.h:812
Definition: Vector.h:316
void Empty()
Definition: Rectangle.h:187
void Empty()
Definition: Rectangle.h:57
GLdouble s
Definition: glext.h:2935
float x
Definition: Vector.h:318
GLenum GLint x
Definition: glext.h:2849
int i
Definition: process.py:33
idRectangle & operator/=(const idRectangle &a)
Definition: Rectangle.h:129
idRectangle & operator*=(const float a)
Definition: Rectangle.h:157
void Rotate(float a, idRectangle &out)
Definition: Rectangle.h:78
void ClipAgainst(idRectangle r, bool sizeOnly)
Definition: Rectangle.h:59
const idVec4 & ToVec4() const
Definition: Rectangle.h:115
GLuint index
Definition: glext.h:3476
const GLubyte * c
Definition: glext.h:4677
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:3454
Definition: Vector.h:808
float y
Definition: Rectangle.h:37
#define NULL
Definition: Lib.h:88
float y
Definition: Vector.h:319
idRectangle(float ix, float iy, float iw, float ih)
Definition: Rectangle.h:41
float w
Definition: Vector.h:813
int operator==(const idRectangle &a) const
Definition: Rectangle.h:175
int GetRectCount()
Definition: Rectangle.h:205
void RotateVector(idVec3 &v, idVec3 origin, float a, float c, float s)
GLubyte GLubyte GLubyte a
Definition: glext.h:4662
char * String(void) const
#define DEG2RAD(a)
Definition: Math.h:56
idList< idRectangle > rects
Definition: Rectangle.h:218
float x
Definition: Rectangle.h:36
float Right() const
Definition: Rectangle.h:43
idRegion()
Definition: Rectangle.h:185
int Append(const type &obj)
Definition: List.h:646
GLdouble GLdouble GLdouble r
Definition: glext.h:2951
bool Contains(float xt, float yt)
Definition: Rectangle.h:191
float & operator[](const int index)
Definition: Rectangle.h:179
int Num(void) const
Definition: List.h:265
bool Contains(float xt, float yt)
Definition: Rectangle.h:48
float Bottom() const
Definition: Rectangle.h:42
float w
Definition: Rectangle.h:38
void AddRect(float x, float y, float w, float h)
Definition: Rectangle.h:201
idRectangle & operator+=(const idRectangle &a)
Definition: Rectangle.h:120
float h
Definition: Rectangle.h:39
float x
Definition: Vector.h:810
idRectangle & operator=(const idVec4 v)
Definition: Rectangle.h:167
void Clear(void)
Definition: List.h:184