RECT.CPP 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/RECT.CPP 1 3/03/97 10:25a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : RECT.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 07/22/96 *
  26. * *
  27. * Last Update : July 22, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * Rect::Rect -- Constructs a rectangle object. *
  32. * Rect::Is_Valid -- Determines if the rectangle is valid. *
  33. * Rect::Intersect -- Find the intersection between two rectangles. *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "rect.h"
  36. /***********************************************************************************************
  37. * Rect::Rect -- Constructs a rectangle object. *
  38. * *
  39. * This will construct a rectangle object according to the parameters specified. *
  40. * *
  41. * INPUT: x,y -- The X and Y values of the upper left corner of the rectangle. *
  42. * *
  43. * w,h -- The width and height values of the rectangle. *
  44. * *
  45. * OUTPUT: none *
  46. * *
  47. * WARNINGS: none *
  48. * *
  49. * HISTORY: *
  50. * 07/22/1996 JLB : Created. *
  51. *=============================================================================================*/
  52. Rect::Rect(int x, int y, int w, int h) :
  53. X(x),
  54. Y(y),
  55. Width(w),
  56. Height(h)
  57. {
  58. }
  59. /***********************************************************************************************
  60. * Rect::Is_Valid -- Determines if the rectangle is valid. *
  61. * *
  62. * An invalid rectangle has values that do not make any sense. This is a useful state since *
  63. * this can be used to determine if a rectangle has been initialized correctly or for *
  64. * detecting an error return condition for rectangle manipulation routines. *
  65. * *
  66. * INPUT: none *
  67. * *
  68. * OUTPUT: bool; Does this rectangle appear valid? *
  69. * *
  70. * WARNINGS: An invalid rectangle is one that has a width or height of less than one. *
  71. * *
  72. * HISTORY: *
  73. * 07/22/1996 JLB : Created. *
  74. *=============================================================================================*/
  75. bool Rect::Is_Valid(void) const
  76. {
  77. return(Width > 0 && Height > 0);
  78. }
  79. /***********************************************************************************************
  80. * Rect::Intersect -- Find the intersection between two rectangles. *
  81. * *
  82. * This routine will take the specified rectangle and use it like a "cookie cutter" on this *
  83. * rectangle. The intersection of these two rectangles is returned. An optional X and *
  84. * Y parameter is supplied so that an absolute coordinate can be maintained to the new *
  85. * rectangle. *
  86. * *
  87. * INPUT: rectangle -- Reference to the rectangle to use as a cookie cutter. *
  88. * *
  89. * x,y -- Optional pointer to a coordinate that will be adjusted to stay *
  90. * in an absolute position in coordinates even though it is a *
  91. * relative offset. *
  92. * *
  93. * OUTPUT: Returns with the rectangle that is the intersection of the one specified and *
  94. * this rectangle. *
  95. * *
  96. * WARNINGS: The rectangle returned may be invalid. This can occur if there is no legal *
  97. * intersection between the rectangles. *
  98. * *
  99. * HISTORY: *
  100. * 07/22/1996 JLB : Created. *
  101. *=============================================================================================*/
  102. Rect const Rect::Intersect(Rect const & rectangle, int * x, int * y) const
  103. {
  104. Rect rect(0, 0, 0, 0); // Dummy (illegal) rectangle.
  105. Rect 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. Rect const Union(Rect const & rect1, Rect const & rect2)
  154. {
  155. if (rect1.Is_Valid()) {
  156. if (rect2.Is_Valid()) {
  157. Rect result = rect1;
  158. if (result.X > rect2.X) {
  159. result.Width += result.X-rect2.X;
  160. result.X = rect2.X;
  161. }
  162. if (result.Y > rect2.Y) {
  163. result.Height += result.Y-rect2.Y;
  164. result.Y = rect2.Y;
  165. }
  166. if (result.X+result.Width < rect2.X+rect2.Width) {
  167. result.Width = ((rect2.X+rect2.Width)-result.X)+1;
  168. }
  169. if (result.Y+result.Height < rect2.Y+rect2.Height) {
  170. result.Height = ((rect2.Y+rect2.Height)-result.Y)+1;
  171. }
  172. return(result);
  173. }
  174. return(rect1);
  175. }
  176. return(rect2);
  177. }