Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Rectangle.cs
Go to the documentation of this file.
1using System;
5using Microsoft.Xna.Framework.Design;
6
8
10[TypeConverter(typeof(RectangleConverter))]
11public struct Rectangle : IEquatable<Rectangle>
12{
13 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
14 public int X;
15
16 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
17 public int Y;
18
19 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
20 public int Width;
21
22 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
23 public int Height;
24
25 private static Rectangle _empty = default(Rectangle);
26
27 public int Left => X;
28
29 public int Right => X + Width;
30
31 public int Top => Y;
32
33 public int Bottom => Y + Height;
34
36 {
37 get
38 {
39 return new Point(X, Y);
40 }
41 set
42 {
43 X = value.X;
44 Y = value.Y;
45 }
46 }
47
48 public Point Center => new Point(X + Width / 2, Y + Height / 2);
49
50 public static Rectangle Empty => _empty;
51
52 public bool IsEmpty
53 {
54 get
55 {
56 if (Width == 0 && Height == 0 && X == 0)
57 {
58 return Y == 0;
59 }
60 return false;
61 }
62 }
63
64 public Rectangle(int x, int y, int width, int height)
65 {
66 X = x;
67 Y = y;
68 Width = width;
69 Height = height;
70 }
71
72 public void Offset(Point amount)
73 {
74 X += amount.X;
75 Y += amount.Y;
76 }
77
78 public void Offset(int offsetX, int offsetY)
79 {
80 X += offsetX;
81 Y += offsetY;
82 }
83
84 public void Inflate(int horizontalAmount, int verticalAmount)
85 {
86 X -= horizontalAmount;
87 Y -= verticalAmount;
88 Width += horizontalAmount * 2;
89 Height += verticalAmount * 2;
90 }
91
92 public bool Contains(int x, int y)
93 {
94 if (X <= x && x < X + Width && Y <= y)
95 {
96 return y < Y + Height;
97 }
98 return false;
99 }
100
101 public bool Contains(Point value)
102 {
103 if (X <= value.X && value.X < X + Width && Y <= value.Y)
104 {
105 return value.Y < Y + Height;
106 }
107 return false;
108 }
109
110 public void Contains(ref Point value, out bool result)
111 {
112 result = X <= value.X && value.X < X + Width && Y <= value.Y && value.Y < Y + Height;
113 }
114
116 {
117 if (X <= value.X && value.X + value.Width <= X + Width && Y <= value.Y)
118 {
119 return value.Y + value.Height <= Y + Height;
120 }
121 return false;
122 }
123
124 public void Contains(ref Rectangle value, out bool result)
125 {
126 result = X <= value.X && value.X + value.Width <= X + Width && Y <= value.Y && value.Y + value.Height <= Y + Height;
127 }
128
130 {
131 if (value.X < X + Width && X < value.X + value.Width && value.Y < Y + Height)
132 {
133 return Y < value.Y + value.Height;
134 }
135 return false;
136 }
137
138 public void Intersects(ref Rectangle value, out bool result)
139 {
140 result = value.X < X + Width && X < value.X + value.Width && value.Y < Y + Height && Y < value.Y + value.Height;
141 }
142
143 public static Rectangle Intersect(Rectangle value1, Rectangle value2)
144 {
145 int num = value1.X + value1.Width;
146 int num2 = value2.X + value2.Width;
147 int num3 = value1.Y + value1.Height;
148 int num4 = value2.Y + value2.Height;
149 int num5 = ((value1.X > value2.X) ? value1.X : value2.X);
150 int num6 = ((value1.Y > value2.Y) ? value1.Y : value2.Y);
151 int num7 = ((num < num2) ? num : num2);
152 int num8 = ((num3 < num4) ? num3 : num4);
153 Rectangle result = default(Rectangle);
154 if (num7 > num5 && num8 > num6)
155 {
156 result.X = num5;
157 result.Y = num6;
158 result.Width = num7 - num5;
159 result.Height = num8 - num6;
160 }
161 else
162 {
163 result.X = 0;
164 result.Y = 0;
165 result.Width = 0;
166 result.Height = 0;
167 }
168 return result;
169 }
170
171 public static void Intersect(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
172 {
173 int num = value1.X + value1.Width;
174 int num2 = value2.X + value2.Width;
175 int num3 = value1.Y + value1.Height;
176 int num4 = value2.Y + value2.Height;
177 int num5 = ((value1.X > value2.X) ? value1.X : value2.X);
178 int num6 = ((value1.Y > value2.Y) ? value1.Y : value2.Y);
179 int num7 = ((num < num2) ? num : num2);
180 int num8 = ((num3 < num4) ? num3 : num4);
181 if (num7 > num5 && num8 > num6)
182 {
183 result.X = num5;
184 result.Y = num6;
185 result.Width = num7 - num5;
186 result.Height = num8 - num6;
187 }
188 else
189 {
190 result.X = 0;
191 result.Y = 0;
192 result.Width = 0;
193 result.Height = 0;
194 }
195 }
196
197 public static Rectangle Union(Rectangle value1, Rectangle value2)
198 {
199 int num = value1.X + value1.Width;
200 int num2 = value2.X + value2.Width;
201 int num3 = value1.Y + value1.Height;
202 int num4 = value2.Y + value2.Height;
203 int num5 = ((value1.X < value2.X) ? value1.X : value2.X);
204 int num6 = ((value1.Y < value2.Y) ? value1.Y : value2.Y);
205 int num7 = ((num > num2) ? num : num2);
206 int num8 = ((num3 > num4) ? num3 : num4);
207 Rectangle result = default(Rectangle);
208 result.X = num5;
209 result.Y = num6;
210 result.Width = num7 - num5;
211 result.Height = num8 - num6;
212 return result;
213 }
214
215 public static void Union(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
216 {
217 int num = value1.X + value1.Width;
218 int num2 = value2.X + value2.Width;
219 int num3 = value1.Y + value1.Height;
220 int num4 = value2.Y + value2.Height;
221 int num5 = ((value1.X < value2.X) ? value1.X : value2.X);
222 int num6 = ((value1.Y < value2.Y) ? value1.Y : value2.Y);
223 int num7 = ((num > num2) ? num : num2);
224 int num8 = ((num3 > num4) ? num3 : num4);
225 result.X = num5;
226 result.Y = num6;
227 result.Width = num7 - num5;
228 result.Height = num8 - num6;
229 }
230
231 public bool Equals(Rectangle other)
232 {
233 if (X == other.X && Y == other.Y && Width == other.Width)
234 {
235 return Height == other.Height;
236 }
237 return false;
238 }
239
240 public override bool Equals(object obj)
241 {
242 bool result = false;
243 if (obj is Rectangle)
244 {
245 result = Equals((Rectangle)obj);
246 }
247 return result;
248 }
249
250 public override string ToString()
251 {
252 CultureInfo currentCulture = CultureInfo.CurrentCulture;
253 return string.Format(currentCulture, "{{X:{0} Y:{1} Width:{2} Height:{3}}}", X.ToString(currentCulture), Y.ToString(currentCulture), Width.ToString(currentCulture), Height.ToString(currentCulture));
254 }
255
256 public override int GetHashCode()
257 {
258 return X.GetHashCode() + Y.GetHashCode() + Width.GetHashCode() + Height.GetHashCode();
259 }
260
261 public static bool operator ==(Rectangle a, Rectangle b)
262 {
263 if (a.X == b.X && a.Y == b.Y && a.Width == b.Width)
264 {
265 return a.Height == b.Height;
266 }
267 return false;
268 }
269
270 public static bool operator !=(Rectangle a, Rectangle b)
271 {
272 if (a.X == b.X && a.Y == b.Y && a.Width == b.Width)
273 {
274 return a.Height != b.Height;
275 }
276 return true;
277 }
278}
static CultureInfo CurrentCulture
void Offset(int offsetX, int offsetY)
Definition Rectangle.cs:78
bool Contains(Rectangle value)
Definition Rectangle.cs:115
void Contains(ref Point value, out bool result)
Definition Rectangle.cs:110
void Inflate(int horizontalAmount, int verticalAmount)
Definition Rectangle.cs:84
bool Intersects(Rectangle value)
Definition Rectangle.cs:129
static bool operator==(Rectangle a, Rectangle b)
Definition Rectangle.cs:261
static Rectangle Intersect(Rectangle value1, Rectangle value2)
Definition Rectangle.cs:143
static void Intersect(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
Definition Rectangle.cs:171
static Rectangle Union(Rectangle value1, Rectangle value2)
Definition Rectangle.cs:197
static void Union(ref Rectangle value1, ref Rectangle value2, out Rectangle result)
Definition Rectangle.cs:215
override bool Equals(object obj)
Definition Rectangle.cs:240
bool Contains(int x, int y)
Definition Rectangle.cs:92
bool Equals(Rectangle other)
Definition Rectangle.cs:231
Rectangle(int x, int y, int width, int height)
Definition Rectangle.cs:64
static bool operator!=(Rectangle a, Rectangle b)
Definition Rectangle.cs:270
void Contains(ref Rectangle value, out bool result)
Definition Rectangle.cs:124
void Intersects(ref Rectangle value, out bool result)
Definition Rectangle.cs:138