Falco Engine 3.9.0.1 (beta)
Rect.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6
7namespace FalcoEngine
8{
9 public struct Rect
10 {
11 private float m_XMin;
12 private float m_YMin;
13 private float m_Width;
14 private float m_Height;
15
19 public static Rect zero => new Rect(0f, 0f, 0f, 0f);
20
24 public float x
25 {
26 get
27 {
28 return m_XMin;
29 }
30 set
31 {
32 m_XMin = value;
33 }
34 }
35
39 public float y
40 {
41 get
42 {
43 return m_YMin;
44 }
45 set
46 {
47 m_YMin = value;
48 }
49 }
50
55 {
56 get
57 {
58 return new Vector2(m_XMin, m_YMin);
59 }
60 set
61 {
62 m_XMin = value.x;
63 m_YMin = value.y;
64 }
65 }
66
71 {
72 get
73 {
74 return new Vector2(x + m_Width / 2f, y + m_Height / 2f);
75 }
76 set
77 {
78 m_XMin = value.x - m_Width / 2f;
79 m_YMin = value.y - m_Height / 2f;
80 }
81 }
82
86 public Vector2 min
87 {
88 get
89 {
90 return new Vector2(xMin, yMin);
91 }
92 set
93 {
94 xMin = value.x;
95 yMin = value.y;
96 }
97 }
98
103 {
104 get
105 {
106 return new Vector2(xMax, yMax);
107 }
108 set
109 {
110 xMax = value.x;
111 yMax = value.y;
112 }
113 }
114
118 public float width
119 {
120 get
121 {
122 return m_Width;
123 }
124 set
125 {
126 m_Width = value;
127 }
128 }
129
133 public float height
134 {
135 get
136 {
137 return m_Height;
138 }
139 set
140 {
141 m_Height = value;
142 }
143 }
144
149 {
150 get
151 {
152 return new Vector2(m_Width, m_Height);
153 }
154 set
155 {
156 m_Width = value.x;
157 m_Height = value.y;
158 }
159 }
160
164 public float xMin
165 {
166 get
167 {
168 return m_XMin;
169 }
170 set
171 {
172 float xMax = this.xMax;
173 m_XMin = value;
174 m_Width = xMax - m_XMin;
175 }
176 }
177
181 public float yMin
182 {
183 get
184 {
185 return m_YMin;
186 }
187 set
188 {
189 float yMax = this.yMax;
190 m_YMin = value;
191 m_Height = yMax - m_YMin;
192 }
193 }
194
198 public float xMax
199 {
200 get
201 {
202 return m_Width + m_XMin;
203 }
204 set
205 {
206 m_Width = value - m_XMin;
207 }
208 }
209
213 public float yMax
214 {
215 get
216 {
217 return m_Height + m_YMin;
218 }
219 set
220 {
221 m_Height = value - m_YMin;
222 }
223 }
224
232 public Rect(float x, float y, float width, float height)
233 {
234 m_XMin = x;
235 m_YMin = y;
236 m_Width = width;
237 m_Height = height;
238 }
239
246 {
247 m_XMin = position.x;
248 m_YMin = position.y;
249 m_Width = size.x;
250 m_Height = size.y;
251 }
252
257 public Rect(Rect source)
258 {
259 m_XMin = source.m_XMin;
260 m_YMin = source.m_YMin;
261 m_Width = source.m_Width;
262 m_Height = source.m_Height;
263 }
264
275 public static Rect MinMaxRect(float xmin, float ymin, float xmax, float ymax)
276 {
277 return new Rect(xmin, ymin, xmax - xmin, ymax - ymin);
278 }
279
287 public void Set(float x, float y, float width, float height)
288 {
289 m_XMin = x;
290 m_YMin = y;
291 m_Width = width;
292 m_Height = height;
293 }
294
303 public bool Contains(Vector2 point)
304 {
305 return point.x >= xMin && point.x < xMax && point.y >= yMin && point.y < yMax;
306 }
307
316 public bool Contains(Vector3 point)
317 {
318 return point.x >= xMin && point.x < xMax && point.y >= yMin && point.y < yMax;
319 }
320
329 public bool Contains(Vector3 point, bool allowInverse)
330 {
331 if (!allowInverse)
332 {
333 return Contains(point);
334 }
335 bool flag = false;
336 if ((width < 0f && point.x <= xMin && point.x > xMax) || (width >= 0f && point.x >= xMin && point.x < xMax))
337 {
338 flag = true;
339 }
340 if (flag && ((height < 0f && point.y <= yMin && point.y > yMax) || (height >= 0f && point.y >= yMin && point.y < yMax)))
341 {
342 return true;
343 }
344 return false;
345 }
346
347 private static Rect OrderMinMax(Rect rect)
348 {
349 if (rect.xMin > rect.xMax)
350 {
351 float xMin = rect.xMin;
352 rect.xMin = rect.xMax;
353 rect.xMax = xMin;
354 }
355 if (rect.yMin > rect.yMax)
356 {
357 float yMin = rect.yMin;
358 rect.yMin = rect.yMax;
359 rect.yMax = yMin;
360 }
361 return rect;
362 }
363
369 public bool Overlaps(Rect other)
370 {
371 return other.xMax > xMin && other.xMin < xMax && other.yMax > yMin && other.yMin < yMax;
372 }
373
379 public bool Overlaps(Rect other, bool allowInverse)
380 {
381 Rect rect = this;
382 if (allowInverse)
383 {
384 rect = OrderMinMax(rect);
385 other = OrderMinMax(other);
386 }
387 return rect.Overlaps(other);
388 }
389
395 public static Vector2 NormalizedToPoint(Rect rectangle, Vector2 normalizedRectCoordinates)
396 {
397 return new Vector2(Mathf.Lerp(rectangle.x, rectangle.xMax, normalizedRectCoordinates.x), Mathf.Lerp(rectangle.y, rectangle.yMax, normalizedRectCoordinates.y));
398 }
399
405 public static Vector2 PointToNormalized(Rect rectangle, Vector2 point)
406 {
407 return new Vector2(Mathf.InverseLerp(rectangle.x, rectangle.xMax, point.x), Mathf.InverseLerp(rectangle.y, rectangle.yMax, point.y));
408 }
409
410 public static bool operator !=(Rect lhs, Rect rhs)
411 {
412 return !(lhs == rhs);
413 }
414
415 public static bool operator ==(Rect lhs, Rect rhs)
416 {
417 return lhs.x == rhs.x && lhs.y == rhs.y && lhs.width == rhs.width && lhs.height == rhs.height;
418 }
419
420 public override int GetHashCode()
421 {
422 return x.GetHashCode() ^ (width.GetHashCode() << 2) ^ (y.GetHashCode() >> 2) ^ (height.GetHashCode() >> 1);
423 }
424
425 public override bool Equals(object other)
426 {
427 if (!(other is Rect))
428 {
429 return false;
430 }
431 return Equals((Rect)other);
432 }
433
434 public bool Equals(Rect other)
435 {
436 return x.Equals(other.x) && y.Equals(other.y) && width.Equals(other.width) && height.Equals(other.height);
437 }
438
443 public override string ToString()
444 {
445 return String.Format("(x:{0:F2}, y:{1:F2}, width:{2:F2}, height:{3:F2})", x, y, width, height);
446 }
447
452 public string ToString(string format)
453 {
454 return String.Format("(x:{0}, y:{1}, width:{2}, height:{3})", x.ToString(format, CultureInfo.InvariantCulture.NumberFormat), y.ToString(format, CultureInfo.InvariantCulture.NumberFormat), width.ToString(format, CultureInfo.InvariantCulture.NumberFormat), height.ToString(format, CultureInfo.InvariantCulture.NumberFormat));
455 }
456 }
457}
static float Lerp(float a, float b, float t)
Definition: Mathf.cs:458
static float InverseLerp(float a, float b, float value)
Definition: Mathf.cs:609
bool Overlaps(Rect other, bool allowInverse)
Definition: Rect.cs:379
Rect(Vector2 position, Vector2 size)
Definition: Rect.cs:245
float height
Definition: Rect.cs:134
bool Contains(Vector3 point)
Definition: Rect.cs:316
Vector2 position
Definition: Rect.cs:55
Vector2 min
Definition: Rect.cs:87
float y
Definition: Rect.cs:40
float xMax
Definition: Rect.cs:199
float yMax
Definition: Rect.cs:214
bool Overlaps(Rect other)
Definition: Rect.cs:369
bool Contains(Vector3 point, bool allowInverse)
Definition: Rect.cs:329
static bool operator==(Rect lhs, Rect rhs)
Definition: Rect.cs:415
float width
Definition: Rect.cs:119
static bool operator!=(Rect lhs, Rect rhs)
Definition: Rect.cs:410
static Vector2 NormalizedToPoint(Rect rectangle, Vector2 normalizedRectCoordinates)
Definition: Rect.cs:395
bool Equals(Rect other)
Definition: Rect.cs:434
Vector2 center
Definition: Rect.cs:71
float yMin
Definition: Rect.cs:182
Rect(float x, float y, float width, float height)
Definition: Rect.cs:232
override string ToString()
Definition: Rect.cs:443
static Rect MinMaxRect(float xmin, float ymin, float xmax, float ymax)
Definition: Rect.cs:275
override int GetHashCode()
Definition: Rect.cs:420
override bool Equals(object other)
Definition: Rect.cs:425
Vector2 max
Definition: Rect.cs:103
bool Contains(Vector2 point)
Definition: Rect.cs:303
float x
Definition: Rect.cs:25
string ToString(string format)
Definition: Rect.cs:452
Rect(Rect source)
Definition: Rect.cs:257
void Set(float x, float y, float width, float height)
Definition: Rect.cs:287
static Rect zero
Definition: Rect.cs:19
Vector2 size
Definition: Rect.cs:149
static Vector2 PointToNormalized(Rect rectangle, Vector2 point)
Definition: Rect.cs:405
float xMin
Definition: Rect.cs:165