trect.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/trect.h $*
  25. * *
  26. * $Author:: Byon_g $*
  27. * *
  28. * $Modtime:: 11/28/00 2:37p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef TRECT_H
  39. #define TRECT_H
  40. #include "point.h"
  41. /*
  42. ** This class manages a rectangle.
  43. */
  44. template<class T>
  45. class TRect
  46. {
  47. public:
  48. TRect(T x=0, T y=0, T w=0, T h=0) : X(x), Y(y), Width(w), Height(h) {}
  49. TRect(TPoint2D<T> const & point, T w, T h) : X(point.X), Y(point.Y), Width(w), Height(h) {}
  50. // Equality comparison operators.
  51. bool operator == (TRect<T> const & rvalue) const {return(X==rvalue.X && Y==rvalue.Y && Width==rvalue.Width && Height==rvalue.Height);}
  52. bool operator != (TRect<T> const & rvalue) const {return(X!=rvalue.X || Y!=rvalue.Y || Width!=rvalue.Width || Height!=rvalue.Height);}
  53. // Addition and subtraction operators.
  54. TRect<T> const & operator += (TPoint2D<T> const & point) {X += point.X;Y += point.Y;return(*this);}
  55. TRect<T> const & operator -= (TPoint2D<T> const & point) {X -= point.X;Y -= point.Y;return(*this);}
  56. TRect<T> const operator + (TPoint2D<T> const & point) {return(TRect<T>(X + point.X, Y + point.Y, Width, Height));}
  57. TRect<T> const operator - (TPoint2D<T> const & point) {return(TRect<T>(X - point.X, Y - point.Y, Width, Height));}
  58. TRect<T> const Intersect(TRect<T> const & rectangle, T * x=NULL, T * y=NULL) const;
  59. TRect<T> const Union(TRect<T> const & rect2) const;
  60. /*
  61. ** Bias this rectangle within another.
  62. */
  63. TRect<T> const Bias_To(TRect<T> const & rect) const {return(TRect<T>(X + rect.X, Y + rect.Y, Width, Height));}
  64. /*
  65. ** Determine if two rectangles overlap.
  66. */
  67. bool Is_Overlapping(TRect<T> const & rect) const {return(X < rect.X+rect.Width && Y < rect.Y+rect.Height && X+Width > rect.X && Y+Height > rect.Y);}
  68. /*
  69. ** Determine is rectangle is valid.
  70. */
  71. bool Is_Valid(void) const {return(Width > 0 && Height > 0);}
  72. /*
  73. ** Returns size of rectangle if each discrete location within it is presumed
  74. ** to be of size 1.
  75. */
  76. int Size(void) const {return(int(Width) * int(Height));}
  77. /*
  78. ** Fetch points of rectangle (used as a convenience for the programmer).
  79. */
  80. TPoint2D<T> Top_Left(void) const {return(TPoint2D<T>(X, Y));}
  81. TPoint2D<T> Top_Right(void) const {return(TPoint2D<T>(X + Width - 1, Y));}
  82. TPoint2D<T> Bottom_Left(void) const {return(TPoint2D<T>(X, Y + Height - 1));}
  83. TPoint2D<T> Bottom_Right(void) const {return(TPoint2D<T>(X + Width - 1, Y + Height - 1));}
  84. /*
  85. ** Determine if a point lies within the rectangle.
  86. */
  87. bool Is_Point_Within(TPoint2D<T> const & point) const {return(point.X >= X && point.X < X+Width && point.Y >= Y && point.Y < Y+Height);}
  88. public:
  89. /*
  90. ** Coordinate of upper left corner of rectangle.
  91. */
  92. T X;
  93. T Y;
  94. /*
  95. ** Dimensions of rectangle. If the width or height is less than or equal to
  96. ** zero, then the rectangle is in an invalid state.
  97. */
  98. T Width;
  99. T Height;
  100. };
  101. template<class T>
  102. TRect<T> const TRect<T>::Intersect(TRect<T> const & rectangle, T * x, T * y) const
  103. {
  104. TRect<T> rect(0, 0, 0, 0); // Dummy (illegal) rectangle.
  105. TRect<T> r = rectangle; // Working rectangle.
  106. /*
  107. ** Both rectangles must be valid or else no intersection can occur. In such
  108. ** a case, return an illegal rectangle.
  109. */
  110. if (!Is_Valid() || !rectangle.Is_Valid()) return(rect);
  111. /*
  112. ** The rectangle spills past the left edge.
  113. */
  114. if (r.X < X) {
  115. r.Width -= X - r.X;
  116. r.X = X;
  117. }
  118. if (r.Width < 1) return(rect);
  119. /*
  120. ** The rectangle spills past top edge.
  121. */
  122. if (r.Y < Y) {
  123. r.Height -= Y - r.Y;
  124. r.Y = Y;
  125. }
  126. if (r.Height < 1) return(rect);
  127. /*
  128. ** The rectangle spills past the right edge.
  129. */
  130. if (r.X + r.Width > X + Width) {
  131. r.Width -= (r.X + r.Width) - (X + Width);
  132. }
  133. if (r.Width < 1) return(rect);
  134. /*
  135. ** The rectangle spills past the bottom edge.
  136. */
  137. if (r.Y + r.Height > Y + Height) {
  138. r.Height -= (r.Y + r.Height) - (Y + Height);
  139. }
  140. if (r.Height < 1) return(rect);
  141. /*
  142. ** Adjust Height relative draw position according to Height new rectangle
  143. ** union.
  144. */
  145. if (x != NULL) {
  146. *x -= (r.X-X);
  147. }
  148. if (y != NULL) {
  149. *y -= (r.Y-Y);
  150. }
  151. return(r);
  152. }
  153. template<class T>
  154. TRect<T> const TRect<T>::Union(TRect<T> const & rect2) const
  155. {
  156. if (Is_Valid()) {
  157. if (rect2.Is_Valid()) {
  158. TRect<T> result = *this;
  159. if (result.X > rect2.X) {
  160. result.Width += result.X-rect2.X;
  161. result.X = rect2.X;
  162. }
  163. if (result.Y > rect2.Y) {
  164. result.Height += result.Y-rect2.Y;
  165. result.Y = rect2.Y;
  166. }
  167. if (result.X+result.Width < rect2.X+rect2.Width) {
  168. result.Width = ((rect2.X+rect2.Width)-result.X)+1;
  169. }
  170. if (result.Y+result.Height < rect2.Y+rect2.Height) {
  171. result.Height = ((rect2.Y+rect2.Height)-result.Y)+1;
  172. }
  173. return(result);
  174. }
  175. return(*this);
  176. }
  177. return(rect2);
  178. }
  179. template<class T>
  180. TPoint2D<T> const TPoint2D<T>::Bias_To(TRect<T> const & rect) const
  181. {
  182. return(TPoint2D<T>(X + rect.X, Y + rect.Y));
  183. }
  184. /*
  185. ** This typedef provides an uncluttered type name for a rectangle that
  186. ** is composed of integers.
  187. */
  188. typedef TRect<int> Rect;
  189. #endif