RECT.CPP 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/RECT.CPP 1 3/03/97 10:25a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : RECT.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 07/22/96 *
  30. * *
  31. * Last Update : July 22, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Rect::Rect -- Constructs a rectangle object. *
  36. * Rect::Is_Valid -- Determines if the rectangle is valid. *
  37. * Rect::Intersect -- Find the intersection between two rectangles. *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "rect.h"
  40. /***********************************************************************************************
  41. * Rect::Rect -- Constructs a rectangle object. *
  42. * *
  43. * This will construct a rectangle object according to the parameters specified. *
  44. * *
  45. * INPUT: x,y -- The X and Y values of the upper left corner of the rectangle. *
  46. * *
  47. * w,h -- The width and height values of the rectangle. *
  48. * *
  49. * OUTPUT: none *
  50. * *
  51. * WARNINGS: none *
  52. * *
  53. * HISTORY: *
  54. * 07/22/1996 JLB : Created. *
  55. *=============================================================================================*/
  56. Rect::Rect(int x, int y, int w, int h) :
  57. X(x),
  58. Y(y),
  59. Width(w),
  60. Height(h)
  61. {
  62. }
  63. /***********************************************************************************************
  64. * Rect::Is_Valid -- Determines if the rectangle is valid. *
  65. * *
  66. * An invalid rectangle has values that do not make any sense. This is a useful state since *
  67. * this can be used to determine if a rectangle has been initialized correctly or for *
  68. * detecting an error return condition for rectangle manipulation routines. *
  69. * *
  70. * INPUT: none *
  71. * *
  72. * OUTPUT: bool; Does this rectangle appear valid? *
  73. * *
  74. * WARNINGS: An invalid rectangle is one that has a width or height of less than one. *
  75. * *
  76. * HISTORY: *
  77. * 07/22/1996 JLB : Created. *
  78. *=============================================================================================*/
  79. bool Rect::Is_Valid(void) const
  80. {
  81. return(Width > 0 && Height > 0);
  82. }
  83. /***********************************************************************************************
  84. * Rect::Intersect -- Find the intersection between two rectangles. *
  85. * *
  86. * This routine will take the specified rectangle and use it like a "cookie cutter" on this *
  87. * rectangle. The intersection of these two rectangles is returned. An optional X and *
  88. * Y parameter is supplied so that an absolute coordinate can be maintained to the new *
  89. * rectangle. *
  90. * *
  91. * INPUT: rectangle -- Reference to the rectangle to use as a cookie cutter. *
  92. * *
  93. * x,y -- Optional pointer to a coordinate that will be adjusted to stay *
  94. * in an absolute position in coordinates even though it is a *
  95. * relative offset. *
  96. * *
  97. * OUTPUT: Returns with the rectangle that is the intersection of the one specified and *
  98. * this rectangle. *
  99. * *
  100. * WARNINGS: The rectangle returned may be invalid. This can occur if there is no legal *
  101. * intersection between the rectangles. *
  102. * *
  103. * HISTORY: *
  104. * 07/22/1996 JLB : Created. *
  105. *=============================================================================================*/
  106. Rect const Rect::Intersect(Rect const & rectangle, int * x, int * y) const
  107. {
  108. Rect rect(0, 0, 0, 0); // Dummy (illegal) rectangle.
  109. Rect r = rectangle; // Working rectangle.
  110. /*
  111. ** Both rectangles must be valid or else no intersection can occur. In such
  112. ** a case, return an illegal rectangle.
  113. */
  114. if (!Is_Valid() || !rectangle.Is_Valid()) return(rect);
  115. /*
  116. ** The rectangle spills past the left edge.
  117. */
  118. if (r.X < X) {
  119. r.Width -= X - r.X;
  120. r.X = X;
  121. }
  122. if (r.Width < 1) return(rect);
  123. /*
  124. ** The rectangle spills past top edge.
  125. */
  126. if (r.Y < Y) {
  127. r.Height -= Y - r.Y;
  128. r.Y = Y;
  129. }
  130. if (r.Height < 1) return(rect);
  131. /*
  132. ** The rectangle spills past the right edge.
  133. */
  134. if (r.X + r.Width > X + Width) {
  135. r.Width -= (r.X + r.Width) - (X + Width);
  136. }
  137. if (r.Width < 1) return(rect);
  138. /*
  139. ** The rectangle spills past the bottom edge.
  140. */
  141. if (r.Y + r.Height > Y + Height) {
  142. r.Height -= (r.Y + r.Height) - (Y + Height);
  143. }
  144. if (r.Height < 1) return(rect);
  145. /*
  146. ** Adjust Height relative draw position according to Height new rectangle
  147. ** union.
  148. */
  149. if (x != NULL) {
  150. *x -= (r.X-X);
  151. }
  152. if (y != NULL) {
  153. *y -= (r.Y-Y);
  154. }
  155. return(r);
  156. }
  157. Rect const Union(Rect const & rect1, Rect const & rect2)
  158. {
  159. if (rect1.Is_Valid()) {
  160. if (rect2.Is_Valid()) {
  161. Rect result = rect1;
  162. if (result.X > rect2.X) {
  163. result.Width += result.X-rect2.X;
  164. result.X = rect2.X;
  165. }
  166. if (result.Y > rect2.Y) {
  167. result.Height += result.Y-rect2.Y;
  168. result.Y = rect2.Y;
  169. }
  170. if (result.X+result.Width < rect2.X+rect2.Width) {
  171. result.Width = ((rect2.X+rect2.Width)-result.X)+1;
  172. }
  173. if (result.Y+result.Height < rect2.Y+rect2.Height) {
  174. result.Height = ((rect2.Y+rect2.Height)-result.Y)+1;
  175. }
  176. return(result);
  177. }
  178. return(rect1);
  179. }
  180. return(rect2);
  181. }