-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDVector3.cs
More file actions
291 lines (264 loc) · 10.8 KB
/
DVector3.cs
File metadata and controls
291 lines (264 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
namespace SimpleFlightComputer
{
/// <summary>
/// A double precision 3D Vector.
/// </summary>
public struct DVector3 : IEquatable<DVector3>
{
/// <summary>
/// Gets the zero vector.
/// </summary>
public static readonly DVector3 Zero = new DVector3(0, 0, 0);
/// <summary>
/// The X coordinate.
/// </summary>
public double X;
/// <summary>
/// The Y coordinate.
/// </summary>
public double Y;
/// <summary>
/// The Z coordinate.
/// </summary>
public double Z;
/// <summary>
/// Initializes a new instance of the <see cref="DVector3" /> struct.
/// </summary>
/// <param name="_X">The X coordinate.</param>
/// <param name="_Y">The Y coordinate.</param>
/// <param name="_Z">The Z coordinate.</param>
public DVector3(double _X, double _Y, double _Z)
{
this.X = _X;
this.Y = _Y;
this.Z = _Z;
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode()
{
unchecked
{
int hashCode = this.X.GetHashCode();
hashCode = (hashCode * 397) ^ this.Y.GetHashCode();
hashCode = (hashCode * 397) ^ this.Z.GetHashCode();
return hashCode;
}
}
/// <summary>
/// Determines whether the specified <see cref="T:System.Object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="T:System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
return obj is DVector3 other && this == other;
}
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// <see langword="true" /> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <see langword="false" />.
/// </returns>
public bool Equals(DVector3 other)
{
return this == other;
}
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
public override string ToString()
{
return $"DVector3 [{this.X}, {this.Y}, {this.Z}]";
}
/// <summary>
/// Implements the + operator.
/// </summary>
/// <param name="left">The left argument.</param>
/// <param name="right">The right argument.</param>
/// <returns>The result of the operator.</returns>
public static DVector3 operator +(in DVector3 left, in DVector3 right)
{
return new DVector3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
}
/// <summary>
/// Negates the coordinates.
/// </summary>
/// <param name="vec">The vector.</param>
/// <returns>The negated vector.</returns>
public static DVector3 operator -(in DVector3 vec)
{
return new DVector3(-vec.X, -vec.Y, -vec.Z);
}
/// <summary>
/// Implements the - operator.
/// </summary>
/// <param name="left">The left argument.</param>
/// <param name="right">The right argument.</param>
/// <returns>The result of the operator.</returns>
public static DVector3 operator -(in DVector3 left, in DVector3 right)
{
return new DVector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
}
/// <summary>
/// Implements the * operator.
/// </summary>
/// <param name="left">The left argument.</param>
/// <param name="right">The right argument.</param>
/// <returns>The result of the operator.</returns>
public static DVector3 operator *(in DVector3 left, double right)
{
return new DVector3(left.X * right, left.Y * right, left.Z * right);
}
/// <summary>
/// Scales a vector by the given value.
/// </summary>
/// <param name="value">The vector to scale.</param>
/// <param name="scale">The amount by which to scale the vector.</param>
/// <returns>The scaled vector.</returns>
public static DVector3 operator /(in DVector3 value, in double scale)
{
return new DVector3(value.X / scale, value.Y / scale, value.Z / scale);
}
/// <summary>
/// Scales a vector by the given value.
/// </summary>
/// <param name="value">The vector to scale.</param>
/// <param name="scale">The amount by which to scale the vector.</param>
/// <returns>The scaled vector.</returns>
public static DVector3 operator /(in DVector3 value, in DVector3 scale)
{
return new DVector3(value.X / scale.X, value.Y / scale.Y, value.Z / scale.Z);
}
/// <summary>
/// Implements the * operator.
/// </summary>
/// <param name="left">The left argument.</param>
/// <param name="right">The right argument.</param>
/// <returns>The result of the operator.</returns>
public static DVector3 operator *(double right, in DVector3 left)
{
return new DVector3(left.X * right, left.Y * right, left.Z * right);
}
/// <summary>
/// Implements the == operator.
/// </summary>
/// <param name="left">The left argument.</param>
/// <param name="right">The right argument.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(in DVector3 left, in DVector3 right)
{
return left.X == right.X && left.Y == right.Y && left.Z == right.Z;
}
/// <summary>
/// Implements the != operator.
/// </summary>
/// <param name="left">The left argument.</param>
/// <param name="right">The right argument.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(in DVector3 left, in DVector3 right)
{
return left.X != right.X || left.Y != right.Y || left.Z != right.Z;
}
/// <summary>
/// Returns a new vector with X maximum of X and Y maximum of Y and Z maximum of Z.
/// </summary>
/// <param name="a">A vector.</param>
/// <param name="b">Another vector.</param>
/// <returns>A new vector with X maximum of X and Y maximum of Y and Z maximum of Z.</returns>
public static DVector3 Max(in DVector3 a, in DVector3 b)
{
return new DVector3(Math.Max(a.X, b.X), Math.Max(a.Y, b.Y), Math.Max(a.Z, b.Z));
}
/// <summary>
/// Returns a new vector with X minimum of X and Y minimum of Y and Z minimum of Z.
/// </summary>
/// <param name="a">A vector.</param>
/// <param name="b">Another vector.</param>
/// <returns>A new vector with X minimum of X and Y minimum of Y and Z minimum of Z.</returns>
public static DVector3 Min(in DVector3 a, in DVector3 b)
{
return new DVector3(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Min(a.Z, b.Z));
}
/// <summary>
/// Scales a vector by the the values of another vector.
/// </summary>
/// <param name="a">The vector to scale.</param>
/// <param name="b">The vector to use as scale.</param>
/// <returns>The scaled vector.</returns>
public static DVector3 Scale(in DVector3 a, in DVector3 b)
{
return new DVector3(a.X * b.X, a.Y * b.Y, a.Z * b.Z);
}
/// <summary>
/// Scales a vector by the inverse of the values of another vector.
/// </summary>
/// <param name="a">The vector to scale.</param>
/// <param name="b">The vector to use as inverse scale.</param>
/// <returns>The scaled vector.</returns>
public static DVector3 InvScale(in DVector3 a, in DVector3 b)
{
return new DVector3(a.X / b.X, a.Y / b.Y, a.Z / b.Z);
}
/// <summary>
/// Computes the cross product of two vectors.
/// </summary>
/// <param name="a">Vector a.</param>
/// <param name="b">Vector b.</param>
/// <returns>The cross product.</returns>
public static DVector3 Cross(in DVector3 a, in DVector3 b)
{
return new DVector3(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X);
}
/// <summary>
/// Computes the dot product of two vectors.
/// </summary>
/// <param name="a">Vector a.</param>
/// <param name="b">Vector b.</param>
/// <returns>The dot product.</returns>
public static double Dot(in DVector3 a, in DVector3 b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
}
}
/// <summary>
/// DVector3 Extensions.
/// </summary>
public static class DVector3Extensions
{
/// <summary>
/// Returns the length of the vector.
/// </summary>
/// <returns>The length of the vector.</returns>
public static double Length(this in DVector3 _this)
{
return Math.Sqrt(_this.X * _this.X + _this.Y * _this.Y + _this.Z * _this.Z);
}
/// <summary>
/// Return the squared length of the vector.
/// </summary>
/// <returns>The squared length of the vector.</returns>
public static double LengthSq(this in DVector3 _this)
{
return _this.X * _this.X + _this.Y * _this.Y + _this.Z * _this.Z;
}
/// <summary>
/// Normalizes the vector and returns the old length.
/// </summary>
public static double Normalize(this ref DVector3 _this)
{
double len = _this.Length();
_this.X /= len;
_this.Y /= len;
_this.Z /= len;
return len;
}
}
}