Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
trect.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(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/***********************************************************************************************
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 * $Archive:: /Commando/Code/wwlib/trect.h $*
26 * *
27 * $Author:: Byon_g $*
28 * *
29 * $Modtime:: 11/28/00 2:37p $*
30 * *
31 * $Revision:: 2 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36#if _MSC_VER >= 1000
37#pragma once
38#endif // _MSC_VER >= 1000
39
40#ifndef TRECT_H
41#define TRECT_H
42
43#include "point.h"
44
45
46/*
47** This class manages a rectangle.
48*/
49template<class T>
50class TRect
51{
52 public:
53 TRect(T x=0, T y=0, T w=0, T h=0) : X(x), Y(y), Width(w), Height(h) {}
54 TRect(TPoint2D<T> const & point, T w, T h) : X(point.X), Y(point.Y), Width(w), Height(h) {}
55
56 // Equality comparison operators.
57 bool operator == (TRect<T> const & rvalue) const {return(X==rvalue.X && Y==rvalue.Y && Width==rvalue.Width && Height==rvalue.Height);}
58 bool operator != (TRect<T> const & rvalue) const {return(X!=rvalue.X || Y!=rvalue.Y || Width!=rvalue.Width || Height!=rvalue.Height);}
59
60 // Addition and subtraction operators.
61 TRect<T> const & operator += (TPoint2D<T> const & point) {X += point.X;Y += point.Y;return(*this);}
62 TRect<T> const & operator -= (TPoint2D<T> const & point) {X -= point.X;Y -= point.Y;return(*this);}
63 TRect<T> const operator + (TPoint2D<T> const & point) {return(TRect<T>(X + point.X, Y + point.Y, Width, Height));}
64 TRect<T> const operator - (TPoint2D<T> const & point) {return(TRect<T>(X - point.X, Y - point.Y, Width, Height));}
65
66 TRect<T> const Intersect(TRect<T> const & rectangle, T * x=NULL, T * y=NULL) const;
67 TRect<T> const Union(TRect<T> const & rect2) const;
68
69 /*
70 ** Bias this rectangle within another.
71 */
72 TRect<T> const Bias_To(TRect<T> const & rect) const {return(TRect<T>(X + rect.X, Y + rect.Y, Width, Height));}
73
74 /*
75 ** Determine if two rectangles overlap.
76 */
77 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);}
78
79 /*
80 ** Determine is rectangle is valid.
81 */
82 bool Is_Valid(void) const {return(Width > 0 && Height > 0);}
83
84 /*
85 ** Returns size of rectangle if each discrete location within it is presumed
86 ** to be of size 1.
87 */
88 int Size(void) const {return(int(Width) * int(Height));}
89
90 /*
91 ** Fetch points of rectangle (used as a convenience for the programmer).
92 */
93 TPoint2D<T> Top_Left(void) const {return(TPoint2D<T>(X, Y));}
94 TPoint2D<T> Top_Right(void) const {return(TPoint2D<T>(X + Width - 1, Y));}
95 TPoint2D<T> Bottom_Left(void) const {return(TPoint2D<T>(X, Y + Height - 1));}
96 TPoint2D<T> Bottom_Right(void) const {return(TPoint2D<T>(X + Width - 1, Y + Height - 1));}
97
98 /*
99 ** Determine if a point lies within the rectangle.
100 */
101 bool Is_Point_Within(TPoint2D<T> const & point) const {return(point.X >= X && point.X < X+Width && point.Y >= Y && point.Y < Y+Height);}
102
103 public:
104
105 /*
106 ** Coordinate of upper left corner of rectangle.
107 */
108 T X;
109 T Y;
110
111 /*
112 ** Dimensions of rectangle. If the width or height is less than or equal to
113 ** zero, then the rectangle is in an invalid state.
114 */
117};
118
119
120template<class T>
121TRect<T> const TRect<T>::Intersect(TRect<T> const & rectangle, T * x, T * y) const
122{
123 TRect<T> rect(0, 0, 0, 0); // Dummy (illegal) rectangle.
124 TRect<T> r = rectangle; // Working rectangle.
125
126 /*
127 ** Both rectangles must be valid or else no intersection can occur. In such
128 ** a case, return an illegal rectangle.
129 */
130 if (!Is_Valid() || !rectangle.Is_Valid()) return(rect);
131
132 /*
133 ** The rectangle spills past the left edge.
134 */
135 if (r.X < X) {
136 r.Width -= X - r.X;
137 r.X = X;
138 }
139 if (r.Width < 1) return(rect);
140
141 /*
142 ** The rectangle spills past top edge.
143 */
144 if (r.Y < Y) {
145 r.Height -= Y - r.Y;
146 r.Y = Y;
147 }
148 if (r.Height < 1) return(rect);
149
150 /*
151 ** The rectangle spills past the right edge.
152 */
153 if (r.X + r.Width > X + Width) {
154 r.Width -= (r.X + r.Width) - (X + Width);
155 }
156 if (r.Width < 1) return(rect);
157
158 /*
159 ** The rectangle spills past the bottom edge.
160 */
161 if (r.Y + r.Height > Y + Height) {
162 r.Height -= (r.Y + r.Height) - (Y + Height);
163 }
164 if (r.Height < 1) return(rect);
165
166 /*
167 ** Adjust Height relative draw position according to Height new rectangle
168 ** union.
169 */
170 if (x != NULL) {
171 *x -= (r.X-X);
172 }
173 if (y != NULL) {
174 *y -= (r.Y-Y);
175 }
176
177 return(r);
178}
179
180
181template<class T>
182TRect<T> const TRect<T>::Union(TRect<T> const & rect2) const
183{
184 if (Is_Valid()) {
185 if (rect2.Is_Valid()) {
186 TRect<T> result = *this;
187
188 if (result.X > rect2.X) {
189 result.Width += result.X-rect2.X;
190 result.X = rect2.X;
191 }
192 if (result.Y > rect2.Y) {
193 result.Height += result.Y-rect2.Y;
194 result.Y = rect2.Y;
195 }
196 if (result.X+result.Width < rect2.X+rect2.Width) {
197 result.Width = ((rect2.X+rect2.Width)-result.X)+1;
198 }
199 if (result.Y+result.Height < rect2.Y+rect2.Height) {
200 result.Height = ((rect2.Y+rect2.Height)-result.Y)+1;
201 }
202 return(result);
203 }
204 return(*this);
205 }
206 return(rect2);
207}
208
209
210template<class T>
211TPoint2D<T> const TPoint2D<T>::Bias_To(TRect<T> const & rect) const
212{
213 return(TPoint2D<T>(X + rect.X, Y + rect.Y));
214}
215
216
217/*
218** This typedef provides an uncluttered type name for a rectangle that
219** is composed of integers.
220*/
222
223
224#endif
225
#define NULL
Definition BaseType.h:92
TPoint2D< T > const Bias_To(TRect< T > const &rect) const
Definition trect.h:211
TPoint2D(void)
Definition Point.h:55
Definition RECT.h:55
TRect< T > const Union(TRect< T > const &rect2) const
Definition trect.h:182
TRect< T > const Bias_To(TRect< T > const &rect) const
Definition trect.h:72
TRect< T > const & operator+=(TPoint2D< T > const &point)
Definition trect.h:61
bool Is_Valid(void) const
Definition trect.h:82
bool Is_Overlapping(TRect< T > const &rect) const
Definition trect.h:77
int Size(void) const
Definition trect.h:88
TPoint2D< T > Top_Right(void) const
Definition trect.h:94
int Width
Definition trect.h:115
TPoint2D< T > Top_Left(void) const
Definition trect.h:93
bool Is_Point_Within(TPoint2D< T > const &point) const
Definition trect.h:101
int Height
Definition trect.h:116
TRect< T > const Intersect(TRect< T > const &rectangle, T *x=NULL, T *y=NULL) const
Definition trect.h:121
TRect< T > const operator-(TPoint2D< T > const &point)
Definition trect.h:64
TPoint2D< T > Bottom_Left(void) const
Definition trect.h:95
bool operator!=(TRect< T > const &rvalue) const
Definition trect.h:58
TRect(T x=0, T y=0, T w=0, T h=0)
Definition trect.h:53
TRect(TPoint2D< T > const &point, T w, T h)
Definition trect.h:54
TPoint2D< T > Bottom_Right(void) const
Definition trect.h:96
TRect< T > const operator+(TPoint2D< T > const &point)
Definition trect.h:63
TRect< T > const & operator-=(TPoint2D< T > const &point)
Definition trect.h:62
bool operator==(TRect< T > const &rvalue) const
Definition trect.h:57
TRect< int > Rect
Definition trect.h:221