1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef RECTANGLE_H 4 #define RECTANGLE_H 5 6 #include "type2.h" 7 #include "myMath.h" 8 #include "System/creg/creg_cond.h" 9 10 struct SRectangle { 11 CR_DECLARE_STRUCT(SRectangle) 12 SRectangleSRectangle13 SRectangle() 14 : x1(0) 15 , z1(0) 16 , x2(0) 17 , z2(0) 18 {} SRectangleSRectangle19 SRectangle(int x1_, int z1_, int x2_, int z2_) 20 : x1(x1_) 21 , z1(z1_) 22 , x2(x2_) 23 , z2(z2_) 24 {} 25 GetWidthSRectangle26 int GetWidth() const { return x2 - x1; } GetHeightSRectangle27 int GetHeight() const { return z2 - z1; } GetAreaSRectangle28 int GetArea() const { return (GetWidth() * GetHeight()); } 29 InsideSRectangle30 bool Inside(const int2 pos) const { 31 // note: *min inclusive, *max exclusive 32 const bool xb = (pos.x >= x1 && pos.x < x2); 33 const bool yb = (pos.y >= y1 && pos.y < y2); 34 return (xb && yb); 35 } 36 ClampPosSRectangle37 void ClampPos(int2* pos) const { 38 pos->x = Clamp(pos->x, x1, x2); 39 pos->y = Clamp(pos->y, y1, y2); 40 } 41 ClampInSRectangle42 void ClampIn(const SRectangle& rect) { 43 x1 = Clamp(x1, rect.x1, rect.x2); 44 x2 = Clamp(x2, rect.x1, rect.x2); 45 y1 = Clamp(y1, rect.y1, rect.y2); 46 y2 = Clamp(y2, rect.y1, rect.y2); 47 } 48 CheckOverlapSRectangle49 bool CheckOverlap(const SRectangle& rect) const { 50 return 51 x1 < rect.x2 && x2 > rect.x1 && 52 y1 < rect.y2 && y2 > rect.y1; 53 } 54 55 bool operator< (const SRectangle& other) const { 56 if (x1 == other.x1) { 57 return (z1 < other.z1); 58 } else { 59 return (x1 < other.x1); 60 } 61 } 62 63 template<typename T> 64 SRectangle operator* (const T v) const { 65 return SRectangle( 66 x1 * v, z1 * v, 67 x2 * v, z2 * v 68 ); 69 } 70 71 union { 72 int x1; 73 int left; 74 }; 75 union { 76 int z1; 77 int y1; 78 int top; 79 }; 80 union { 81 int x2; 82 int right; 83 }; 84 union { 85 int z2; 86 int y2; 87 int bottom; 88 }; 89 }; 90 91 #endif // RECTANGLE_H 92 93