Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Vector2D.cs
Go to the documentation of this file.
1using System;
3
4namespace ReLogic.Utilities;
5
7[DebuggerDisplay("{DebugDisplayString,nq}")]
8public struct Vector2D : IEquatable<Vector2D>
9{
10 public double X;
11
12 public double Y;
13
14 private static Vector2D zeroVector = new Vector2D(0.0, 0.0);
15
16 private static Vector2D unitVector = new Vector2D(1.0, 1.0);
17
18 private static Vector2D unitXVector = new Vector2D(1.0, 0.0);
19
20 private static Vector2D unitYVector = new Vector2D(0.0, 1.0);
21
22 public static readonly double DoubleEpsilon = Math.Pow(0.5, 53.0);
23
24 public static Vector2D Zero => zeroVector;
25
26 public static Vector2D One => unitVector;
27
28 public static Vector2D UnitX => unitXVector;
29
30 public static Vector2D UnitY => unitYVector;
31
32 internal string DebugDisplayString => X + " " + Y;
33
34 public Vector2D(double x, double y)
35 {
36 X = x;
37 Y = y;
38 }
39
40 public Vector2D(double value)
41 {
42 X = value;
43 Y = value;
44 }
45
46 public override bool Equals(object obj)
47 {
48 if (obj is Vector2D)
49 {
50 return Equals((Vector2D)obj);
51 }
52 return false;
53 }
54
55 public bool Equals(Vector2D other)
56 {
57 if (X == other.X)
58 {
59 return Y == other.Y;
60 }
61 return false;
62 }
63
64 public override int GetHashCode()
65 {
66 return X.GetHashCode() + Y.GetHashCode();
67 }
68
69 public double Length()
70 {
71 return Math.Sqrt(X * X + Y * Y);
72 }
73
74 public double LengthSquared()
75 {
76 return X * X + Y * Y;
77 }
78
79 public void Normalize()
80 {
81 double num = 1.0 / Math.Sqrt(X * X + Y * Y);
82 X *= num;
83 Y *= num;
84 }
85
86 public override string ToString()
87 {
88 return "{X:" + X + " Y:" + Y + "}";
89 }
90
91 public static Vector2D Add(Vector2D value1, Vector2D value2)
92 {
93 value1.X += value2.X;
94 value1.Y += value2.Y;
95 return value1;
96 }
97
98 public static void Add(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
99 {
100 result.X = value1.X + value2.X;
101 result.Y = value1.Y + value2.Y;
102 }
103
104 public static Vector2D Barycentric(Vector2D value1, Vector2D value2, Vector2D value3, double amount1, double amount2)
105 {
106 return new Vector2D(Barycentric(value1.X, value2.X, value3.X, amount1, amount2), Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2));
107 }
108
109 public static void Barycentric(ref Vector2D value1, ref Vector2D value2, ref Vector2D value3, double amount1, double amount2, out Vector2D result)
110 {
111 result.X = Barycentric(value1.X, value2.X, value3.X, amount1, amount2);
112 result.Y = Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2);
113 }
114
115 public static Vector2D CatmullRom(Vector2D value1, Vector2D value2, Vector2D value3, Vector2D value4, double amount)
116 {
117 return new Vector2D(CatmullRom(value1.X, value2.X, value3.X, value4.X, amount), CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount));
118 }
119
120 public static void CatmullRom(ref Vector2D value1, ref Vector2D value2, ref Vector2D value3, ref Vector2D value4, double amount, out Vector2D result)
121 {
122 result.X = CatmullRom(value1.X, value2.X, value3.X, value4.X, amount);
123 result.Y = CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount);
124 }
125
126 public static Vector2D Clamp(Vector2D value1, Vector2D min, Vector2D max)
127 {
128 return new Vector2D(Clamp(value1.X, min.X, max.X), Clamp(value1.Y, min.Y, max.Y));
129 }
130
131 public static void Clamp(ref Vector2D value1, ref Vector2D min, ref Vector2D max, out Vector2D result)
132 {
133 result.X = Clamp(value1.X, min.X, max.X);
134 result.Y = Clamp(value1.Y, min.Y, max.Y);
135 }
136
137 public static double Distance(Vector2D value1, Vector2D value2)
138 {
139 double num3 = value1.X - value2.X;
140 double num2 = value1.Y - value2.Y;
141 return Math.Sqrt(num3 * num3 + num2 * num2);
142 }
143
144 public static void Distance(ref Vector2D value1, ref Vector2D value2, out double result)
145 {
146 double num = value1.X - value2.X;
147 double num2 = value1.Y - value2.Y;
148 result = Math.Sqrt(num * num + num2 * num2);
149 }
150
151 public static double DistanceSquared(Vector2D value1, Vector2D value2)
152 {
153 double num3 = value1.X - value2.X;
154 double num2 = value1.Y - value2.Y;
155 return num3 * num3 + num2 * num2;
156 }
157
158 public static void DistanceSquared(ref Vector2D value1, ref Vector2D value2, out double result)
159 {
160 double num = value1.X - value2.X;
161 double num2 = value1.Y - value2.Y;
162 result = num * num + num2 * num2;
163 }
164
165 public static Vector2D Divide(Vector2D value1, Vector2D value2)
166 {
167 value1.X /= value2.X;
168 value1.Y /= value2.Y;
169 return value1;
170 }
171
172 public static void Divide(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
173 {
174 result.X = value1.X / value2.X;
175 result.Y = value1.Y / value2.Y;
176 }
177
178 public static Vector2D Divide(Vector2D value1, double divider)
179 {
180 double num = 1.0 / divider;
181 value1.X *= num;
182 value1.Y *= num;
183 return value1;
184 }
185
186 public static void Divide(ref Vector2D value1, double divider, out Vector2D result)
187 {
188 double num = 1.0 / divider;
189 result.X = value1.X * num;
190 result.Y = value1.Y * num;
191 }
192
193 public static double Dot(Vector2D value1, Vector2D value2)
194 {
195 return value1.X * value2.X + value1.Y * value2.Y;
196 }
197
198 public static void Dot(ref Vector2D value1, ref Vector2D value2, out double result)
199 {
200 result = value1.X * value2.X + value1.Y * value2.Y;
201 }
202
203 public static Vector2D Hermite(Vector2D value1, Vector2D tangent1, Vector2D value2, Vector2D tangent2, double amount)
204 {
205 Vector2D result = default(Vector2D);
206 Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
207 return result;
208 }
209
210 public static void Hermite(ref Vector2D value1, ref Vector2D tangent1, ref Vector2D value2, ref Vector2D tangent2, double amount, out Vector2D result)
211 {
212 result.X = Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount);
213 result.Y = Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount);
214 }
215
216 public static Vector2D Lerp(Vector2D value1, Vector2D value2, double amount)
217 {
218 return new Vector2D(Lerp(value1.X, value2.X, amount), Lerp(value1.Y, value2.Y, amount));
219 }
220
221 public static void Lerp(ref Vector2D value1, ref Vector2D value2, double amount, out Vector2D result)
222 {
223 result.X = Lerp(value1.X, value2.X, amount);
224 result.Y = Lerp(value1.Y, value2.Y, amount);
225 }
226
227 public static Vector2D Max(Vector2D value1, Vector2D value2)
228 {
229 return new Vector2D((value1.X > value2.X) ? value1.X : value2.X, (value1.Y > value2.Y) ? value1.Y : value2.Y);
230 }
231
232 public static void Max(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
233 {
234 result.X = ((value1.X > value2.X) ? value1.X : value2.X);
235 result.Y = ((value1.Y > value2.Y) ? value1.Y : value2.Y);
236 }
237
238 public static Vector2D Min(Vector2D value1, Vector2D value2)
239 {
240 return new Vector2D((value1.X < value2.X) ? value1.X : value2.X, (value1.Y < value2.Y) ? value1.Y : value2.Y);
241 }
242
243 public static void Min(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
244 {
245 result.X = ((value1.X < value2.X) ? value1.X : value2.X);
246 result.Y = ((value1.Y < value2.Y) ? value1.Y : value2.Y);
247 }
248
249 public static Vector2D Multiply(Vector2D value1, Vector2D value2)
250 {
251 value1.X *= value2.X;
252 value1.Y *= value2.Y;
253 return value1;
254 }
255
256 public static Vector2D Multiply(Vector2D value1, double scaleFactor)
257 {
258 value1.X *= scaleFactor;
259 value1.Y *= scaleFactor;
260 return value1;
261 }
262
263 public static void Multiply(ref Vector2D value1, double scaleFactor, out Vector2D result)
264 {
265 result.X = value1.X * scaleFactor;
266 result.Y = value1.Y * scaleFactor;
267 }
268
269 public static void Multiply(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
270 {
271 result.X = value1.X * value2.X;
272 result.Y = value1.Y * value2.Y;
273 }
274
276 {
277 value.X = 0.0 - value.X;
278 value.Y = 0.0 - value.Y;
279 return value;
280 }
281
282 public static void Negate(ref Vector2D value, out Vector2D result)
283 {
284 result.X = 0.0 - value.X;
285 result.Y = 0.0 - value.Y;
286 }
287
289 {
290 double num = 1.0 / Math.Sqrt(value.X * value.X + value.Y * value.Y);
291 value.X *= num;
292 value.Y *= num;
293 return value;
294 }
295
296 public static void Normalize(ref Vector2D value, out Vector2D result)
297 {
298 double num = 1.0 / Math.Sqrt(value.X * value.X + value.Y * value.Y);
299 result.X = value.X * num;
300 result.Y = value.Y * num;
301 }
302
303 public static Vector2D Reflect(Vector2D vector, Vector2D normal)
304 {
305 double num = 2.0 * (vector.X * normal.X + vector.Y * normal.Y);
306 Vector2D result = default(Vector2D);
307 result.X = vector.X - normal.X * num;
308 result.Y = vector.Y - normal.Y * num;
309 return result;
310 }
311
312 public static void Reflect(ref Vector2D vector, ref Vector2D normal, out Vector2D result)
313 {
314 double num = 2.0 * (vector.X * normal.X + vector.Y * normal.Y);
315 result.X = vector.X - normal.X * num;
316 result.Y = vector.Y - normal.Y * num;
317 }
318
319 public static Vector2D SmoothStep(Vector2D value1, Vector2D value2, double amount)
320 {
321 return new Vector2D(SmoothStep(value1.X, value2.X, amount), SmoothStep(value1.Y, value2.Y, amount));
322 }
323
324 public static void SmoothStep(ref Vector2D value1, ref Vector2D value2, double amount, out Vector2D result)
325 {
326 result.X = SmoothStep(value1.X, value2.X, amount);
327 result.Y = SmoothStep(value1.Y, value2.Y, amount);
328 }
329
330 public static Vector2D Subtract(Vector2D value1, Vector2D value2)
331 {
332 value1.X -= value2.X;
333 value1.Y -= value2.Y;
334 return value1;
335 }
336
337 public static void Subtract(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
338 {
339 result.X = value1.X - value2.X;
340 result.Y = value1.Y - value2.Y;
341 }
342
344 {
345 value.X = 0.0 - value.X;
346 value.Y = 0.0 - value.Y;
347 return value;
348 }
349
350 public static bool operator ==(Vector2D value1, Vector2D value2)
351 {
352 if (value1.X == value2.X)
353 {
354 return value1.Y == value2.Y;
355 }
356 return false;
357 }
358
359 public static bool operator !=(Vector2D value1, Vector2D value2)
360 {
361 return !(value1 == value2);
362 }
363
364 public static Vector2D operator +(Vector2D value1, Vector2D value2)
365 {
366 value1.X += value2.X;
367 value1.Y += value2.Y;
368 return value1;
369 }
370
371 public static Vector2D operator -(Vector2D value1, Vector2D value2)
372 {
373 value1.X -= value2.X;
374 value1.Y -= value2.Y;
375 return value1;
376 }
377
378 public static Vector2D operator *(Vector2D value1, Vector2D value2)
379 {
380 value1.X *= value2.X;
381 value1.Y *= value2.Y;
382 return value1;
383 }
384
385 public static Vector2D operator *(Vector2D value, double scaleFactor)
386 {
387 value.X *= scaleFactor;
388 value.Y *= scaleFactor;
389 return value;
390 }
391
392 public static Vector2D operator *(double scaleFactor, Vector2D value)
393 {
394 value.X *= scaleFactor;
395 value.Y *= scaleFactor;
396 return value;
397 }
398
399 public static Vector2D operator /(Vector2D value1, Vector2D value2)
400 {
401 value1.X /= value2.X;
402 value1.Y /= value2.Y;
403 return value1;
404 }
405
406 public static Vector2D operator /(Vector2D value1, double divider)
407 {
408 double num = 1.0 / divider;
409 value1.X *= num;
410 value1.Y *= num;
411 return value1;
412 }
413
414 public static double Clamp(double value, double min, double max)
415 {
416 value = ((value > max) ? max : value);
417 value = ((value < min) ? min : value);
418 return value;
419 }
420
421 public static double Lerp(double value1, double value2, double amount)
422 {
423 return value1 + (value2 - value1) * amount;
424 }
425
426 public static double SmoothStep(double value1, double value2, double amount)
427 {
428 double amount2 = Clamp(amount, 0.0, 1.0);
429 return Hermite(value1, 0.0, value2, 0.0, amount2);
430 }
431
432 public static double Hermite(double value1, double tangent1, double value2, double tangent2, double amount)
433 {
434 double num = amount * amount * amount;
435 double num2 = amount * amount;
436 if (Math.Abs(amount) <= DoubleEpsilon)
437 {
438 return value1;
439 }
440 if (amount == 1.0)
441 {
442 return value2;
443 }
444 return (2.0 * value1 - 2.0 * value2 + tangent2 + tangent1) * num + (3.0 * value2 - 3.0 * value1 - 2.0 * tangent1 - tangent2) * num2 + tangent1 * amount + value1;
445 }
446
447 public static double Barycentric(double value1, double value2, double value3, double amount1, double amount2)
448 {
449 return value1 + (value2 - value1) * amount1 + (value3 - value1) * amount2;
450 }
451
452 public static double CatmullRom(double value1, double value2, double value3, double value4, double amount)
453 {
454 double num = amount * amount;
455 double num2 = num * amount;
456 return 0.5 * (2.0 * value2 + (value3 - value1) * amount + (2.0 * value1 - 5.0 * value2 + 4.0 * value3 - value4) * num + (3.0 * value2 - value1 - 3.0 * value3 + value4) * num2);
457 }
458}
static double Sqrt(double d)
static double Pow(double x, double y)
static double Abs(double value)
static double Barycentric(double value1, double value2, double value3, double amount1, double amount2)
Definition Vector2D.cs:447
static void Subtract(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
Definition Vector2D.cs:337
Vector2D(double value)
Definition Vector2D.cs:40
Vector2D(double x, double y)
Definition Vector2D.cs:34
static Vector2D UnitY
Definition Vector2D.cs:30
static void Reflect(ref Vector2D vector, ref Vector2D normal, out Vector2D result)
Definition Vector2D.cs:312
static void Multiply(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
Definition Vector2D.cs:269
static void Min(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
Definition Vector2D.cs:243
static Vector2D SmoothStep(Vector2D value1, Vector2D value2, double amount)
Definition Vector2D.cs:319
static Vector2D Subtract(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:330
override string ToString()
Definition Vector2D.cs:86
static Vector2D Multiply(Vector2D value1, double scaleFactor)
Definition Vector2D.cs:256
static void Max(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
Definition Vector2D.cs:232
static Vector2D Zero
Definition Vector2D.cs:24
override int GetHashCode()
Definition Vector2D.cs:64
static double Clamp(double value, double min, double max)
Definition Vector2D.cs:414
static Vector2D operator+(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:364
static void DistanceSquared(ref Vector2D value1, ref Vector2D value2, out double result)
Definition Vector2D.cs:158
static void Dot(ref Vector2D value1, ref Vector2D value2, out double result)
Definition Vector2D.cs:198
static void Barycentric(ref Vector2D value1, ref Vector2D value2, ref Vector2D value3, double amount1, double amount2, out Vector2D result)
Definition Vector2D.cs:109
static Vector2D operator-(Vector2D value)
Definition Vector2D.cs:343
static Vector2D Barycentric(Vector2D value1, Vector2D value2, Vector2D value3, double amount1, double amount2)
Definition Vector2D.cs:104
static void Add(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
Definition Vector2D.cs:98
static double Hermite(double value1, double tangent1, double value2, double tangent2, double amount)
Definition Vector2D.cs:432
static void SmoothStep(ref Vector2D value1, ref Vector2D value2, double amount, out Vector2D result)
Definition Vector2D.cs:324
bool Equals(Vector2D other)
Definition Vector2D.cs:55
static void Normalize(ref Vector2D value, out Vector2D result)
Definition Vector2D.cs:296
static Vector2D Divide(Vector2D value1, double divider)
Definition Vector2D.cs:178
static Vector2D unitXVector
Definition Vector2D.cs:18
static Vector2D UnitX
Definition Vector2D.cs:28
static Vector2D zeroVector
Definition Vector2D.cs:14
static void Clamp(ref Vector2D value1, ref Vector2D min, ref Vector2D max, out Vector2D result)
Definition Vector2D.cs:131
static void Divide(ref Vector2D value1, double divider, out Vector2D result)
Definition Vector2D.cs:186
static Vector2D Normalize(Vector2D value)
Definition Vector2D.cs:288
static void Multiply(ref Vector2D value1, double scaleFactor, out Vector2D result)
Definition Vector2D.cs:263
static Vector2D operator/(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:399
static double SmoothStep(double value1, double value2, double amount)
Definition Vector2D.cs:426
static void CatmullRom(ref Vector2D value1, ref Vector2D value2, ref Vector2D value3, ref Vector2D value4, double amount, out Vector2D result)
Definition Vector2D.cs:120
static void Negate(ref Vector2D value, out Vector2D result)
Definition Vector2D.cs:282
static void Lerp(ref Vector2D value1, ref Vector2D value2, double amount, out Vector2D result)
Definition Vector2D.cs:221
static double Distance(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:137
static double Lerp(double value1, double value2, double amount)
Definition Vector2D.cs:421
static Vector2D Add(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:91
static void Hermite(ref Vector2D value1, ref Vector2D tangent1, ref Vector2D value2, ref Vector2D tangent2, double amount, out Vector2D result)
Definition Vector2D.cs:210
static Vector2D Hermite(Vector2D value1, Vector2D tangent1, Vector2D value2, Vector2D tangent2, double amount)
Definition Vector2D.cs:203
static Vector2D CatmullRom(Vector2D value1, Vector2D value2, Vector2D value3, Vector2D value4, double amount)
Definition Vector2D.cs:115
static readonly double DoubleEpsilon
Definition Vector2D.cs:22
static Vector2D Reflect(Vector2D vector, Vector2D normal)
Definition Vector2D.cs:303
static bool operator==(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:350
override bool Equals(object obj)
Definition Vector2D.cs:46
static Vector2D unitVector
Definition Vector2D.cs:16
static Vector2D Negate(Vector2D value)
Definition Vector2D.cs:275
static Vector2D operator*(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:378
static Vector2D Multiply(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:249
static Vector2D Max(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:227
static double DistanceSquared(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:151
static Vector2D Clamp(Vector2D value1, Vector2D min, Vector2D max)
Definition Vector2D.cs:126
static Vector2D One
Definition Vector2D.cs:26
static Vector2D Divide(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:165
static Vector2D Lerp(Vector2D value1, Vector2D value2, double amount)
Definition Vector2D.cs:216
static Vector2D unitYVector
Definition Vector2D.cs:20
static void Divide(ref Vector2D value1, ref Vector2D value2, out Vector2D result)
Definition Vector2D.cs:172
static double CatmullRom(double value1, double value2, double value3, double value4, double amount)
Definition Vector2D.cs:452
static Vector2D Min(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:238
static void Distance(ref Vector2D value1, ref Vector2D value2, out double result)
Definition Vector2D.cs:144
static double Dot(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:193
static bool operator!=(Vector2D value1, Vector2D value2)
Definition Vector2D.cs:359