-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3D.cs
More file actions
222 lines (171 loc) · 5.48 KB
/
Copy pathVector3D.cs
File metadata and controls
222 lines (171 loc) · 5.48 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
using System;
using System.Globalization;
namespace VectorMath.Vector
{
public struct Vector3D : IEquatable<Vector3D>
{
public static Vector3D UnitVector => new Vector3D(1d, 1d, 1d);
public static Vector3D ZeroVector => new Vector3D(0d, 0d, 0d);
public Vector3D(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
#region ADD
public void Add(Vector3D vector)
{
X += vector.X;
Y += vector.Y;
Z += vector.Z;
}
public void Add(double number)
{
X += number;
Y += number;
Z += number;
}
public static Vector3D operator +(Vector3D left, Vector3D right)
{
return new Vector3D(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
}
public static Vector3D operator +(Vector3D left, double right)
{
return new Vector3D(left.X + right, left.Y + right, left.Z + right);
}
#endregion ADD
#region SUBTRACT
public void Subtract(Vector3D vector)
{
X -= vector.X;
Y -= vector.Y;
Z -= vector.Z;
}
public void Subtract(double number)
{
X -= number;
Y -= number;
Z -= number;
}
public static Vector3D operator -(Vector3D left, Vector3D right)
{
return new Vector3D(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
}
public static Vector3D operator -(Vector3D left, double right)
{
return new Vector3D(left.X - right, left.Y - right, left.Z - right);
}
#endregion SUBTRACT
#region MULTIPLY
public void Multiply(double number)
{
X *= number;
Y *= number;
Z *= number;
}
public static Vector3D operator *(Vector3D left, double right)
{
return new Vector3D(left.X * right, left.Y * right, left.Z * right);
}
#endregion MULTIPLY
#region DIVIDE
public void Divide(double number)
{
var divisor = 1d / number;
X *= divisor;
Y *= divisor;
Z *= divisor;
}
public static Vector3D operator /(Vector3D left, double right)
{
var divisor = 1d / right;
return new Vector3D(left.X * divisor, left.Y * divisor, left.Z * divisor);
}
#endregion DIVIDE
#region FUNCTIONS
/// <summary>
/// rotating around the z axis
/// </summary>
public Vector3D RotateVector(double rotationAngle)
{
var sin = Math.Sin(rotationAngle);
var cos = Math.Cos(rotationAngle);
var x = cos * X - sin * Y;
var y = sin * X + cos * Y;
return new Vector3D(x, y, Z);
}
/// <summary>
/// rotating around a given axis
/// </summary>
public Vector3D RotateVector(Vector3D axis, double rotationAngle)
{
var sin = Math.Sin(rotationAngle);
var cos = Math.Cos(rotationAngle);
var klammer = 1d - cos;
var n1 = axis.X;
var n2 = axis.Y;
var n3 = axis.Z;
var n1Sq = n1 * n1;
var n2Sq = n2 * n2;
var n3Sq = n3 * n3;
var x = X * (n1Sq * klammer + cos) + Y * (n1 * n2 * klammer - n3 * sin) + Z * (n1 * n3 * klammer + n2 * sin);
var y = X * (n2 * n1 * klammer + n3 * sin) + Y * (n2Sq * klammer + cos) + Z * (n2 * n3 * klammer - n1 * sin);
var z = X * (n3 * n1 * klammer - n2 * sin) + Y * (n3 * n2 * klammer + n1 * sin) + Z * (n3Sq * klammer + cos);
return new Vector3D(x, y, z);
}
public void Normalize()
{
var factor = 1d / Length();
X *= factor;
Y *= factor;
Z *= factor;
}
public Vector3D CrossProduct(Vector3D right)
{
var x = Y * right.Z - Z * right.Y;
var y = Z * right.X - X * right.Z;
var z = X * right.Y - Y * right.X;
return new Vector3D(x, y, z);
}
public double ScalarProduct(Vector3D right)
{
return X * right.X + Y * right.Y + Z * right.Z;
}
public double LengthSquared()
{
return X * X + Y * Y + Z * Z;
}
public double Length()
{
return Math.Sqrt(LengthSquared());
}
public override bool Equals(object obj)
{
if (obj is Vector3D vector)
{
return Equals(vector);
}
return false;
}
public bool Equals(Vector3D other)
{
if (ReferenceEquals(this, other))
{
return true;
}
return X == other.X && Y == other.Y && Z == other.Z;
}
public override int GetHashCode()
{
return HashCode.Combine(X, Y, Z);
}
public override string ToString()
{
return $"Vector3D with X: {X.ToString(CultureInfo.InvariantCulture)}, Y: {Y.ToString(CultureInfo.InvariantCulture)}, Z: {Z.ToString(CultureInfo.InvariantCulture)}";
}
#endregion FUNCTIONS
}
}