forked from sldsmkd/vector3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2d.cs
More file actions
executable file
·329 lines (290 loc) · 7.33 KB
/
Vector2d.cs
File metadata and controls
executable file
·329 lines (290 loc) · 7.33 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
namespace UnityEngine
{
public struct Vector2d
{
const double EPSILON_MAGNITUDE = 9.99999974737875E-06; // ~= 1e-5
const double EPSILON_MAGNITUDE_SQR = EPSILON_MAGNITUDE*EPSILON_MAGNITUDE;
public double x;
public double y;
public double this[int index]
{
get
{
switch (index)
{
case 0:
return this.x;
case 1:
return this.y;
default:
throw new IndexOutOfRangeException("Invalid Vector2d index!");
}
}
set
{
switch (index) {
case 0:
this.x = value;
break;
case 1:
this.y = value;
break;
default:
throw new IndexOutOfRangeException("Invalid Vector2d index!");
}
}
}
public Vector2d normalized
{
get
{
Vector2d vector2d = new Vector2d(this.x, this.y);
vector2d.Normalize();
return vector2d;
}
}
public double magnitude
{
get
{
return Mathd.Sqrt(this.x * this.x + this.y * this.y);
}
}
public double sqrMagnitude
{
get
{
return this.x * this.x + this.y * this.y;
}
}
public static Vector2d zero
{
get
{
return new Vector2d(0.0d, 0.0d);
}
}
public static Vector2d one
{
get
{
return new Vector2d(1d, 1d);
}
}
public static Vector2d up
{
get
{
return new Vector2d(0.0d, 1d);
}
}
public static Vector2d right
{
get
{
return new Vector2d(1d, 0.0d);
}
}
public Vector2d(double x, double y)
{
this.x = x;
this.y = y;
}
public static explicit operator Vector2d(Vector3d v)
{
//I don't like the conversion pattern here. Maybe (v.x, v.z) would be better in Y-up orientation? Check how Unith 3D handles this conversion
//also changed conversion to explicit, as it has a potential data loss and/or unexpected result
return new Vector2d(v.x, v.y);
}
public static explicit operator Vector3d(Vector2d v)
{
//same as for the above conversion
return new Vector3d(v.x, v.y, 0.0d);
}
public static implicit operator Vector2(Vector2d v)
{
return new Vector2((float)v.x, (float)v.y);
}
public static implicit operator Vector2d(Vector2 v)
{
return new Vector2d((double)v.x, (double)v.y);
}
public static Vector2d operator +(Vector2d a, Vector2d b)
{
return new Vector2d(a.x + b.x, a.y + b.y);
}
public static Vector2d operator +(Vector2 a, Vector2d b)
{
return new Vector2d((double)a.x + b.x, (double)a.y + b.y);
}
public static Vector2d operator +(Vector2d a, Vector2 b)
{
return new Vector2d(a.x + (double)b.x, a.y + (double)b.y);
}
public static Vector2d operator -(Vector2d a, Vector2d b)
{
return new Vector2d(a.x - b.x, a.y - b.y);
}
public static Vector2d operator -(Vector2 a, Vector2d b)
{
return new Vector2d((double)a.x - b.x, (double)a.y - b.y);
}
public static Vector2d operator -(Vector2d a, Vector2 b)
{
return new Vector2d(a.x - (double)b.x, a.y - (double)b.y);
}
public static Vector2d operator -(Vector2d a)
{
return new Vector2d(-a.x, -a.y);
}
public static Vector2d operator *(Vector2d a, double d)
{
return new Vector2d(a.x * d, a.y * d);
}
public static Vector2d operator *(double d, Vector2d a)
{
return new Vector2d(a.x * d, a.y * d);
}
public static Vector2d operator /(Vector2d a, double d)
{
return new Vector2d(a.x / d, a.y / d);
}
public static bool operator ==(Vector2d lhs, Vector2d rhs)
{
// Implementation similar to Vector3
return Vector2d.SqrMagnitude(lhs - rhs) < EPSILON_MAGNITUDE_SQR;
}
public static bool operator ==(Vector2 lhs, Vector2d rhs)
{
return Vector2d.SqrMagnitude(lhs - rhs) < EPSILON_MAGNITUDE_SQR;
}
public static bool operator ==(Vector2d lhs, Vector2 rhs)
{
return Vector2d.SqrMagnitude(lhs - rhs) < EPSILON_MAGNITUDE_SQR;
}
public static bool operator !=(Vector2d lhs, Vector2d rhs)
{
return !(lhs==rhs);
}
public static bool operator !=(Vector2 lhs, Vector2d rhs)
{
return !(lhs==rhs);
}
public static bool operator !=(Vector2d lhs, Vector2 rhs)
{
return !(lhs==rhs);
}
public void Set(double new_x, double new_y)
{
this.x = new_x;
this.y = new_y;
}
public static Vector2d Lerp(Vector2d from, Vector2d to, double t)
{
t = Mathd.Clamp01(t);
return new Vector2d(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t);
}
public static Vector2d MoveTowards(Vector2d current, Vector2d target, double maxDistanceDelta)
{
Vector2d vector2 = target - current;
double magnitude = vector2.magnitude;
if (magnitude <= maxDistanceDelta || magnitude == 0.0d)
{
return target;
}
else
{
return current + vector2 / magnitude * maxDistanceDelta;
}
}
public static Vector2d Scale(Vector2d a, Vector2d b)
{
return new Vector2d(a.x * b.x, a.y * b.y);
}
public void Scale(Vector2d scale)
{
this.x *= scale.x;
this.y *= scale.y;
}
public void Normalize()
{
double magnitude = this.magnitude;
if (magnitude > EPSILON_MAGNITUDE)
{
this = this / magnitude;
}
else
{
this = Vector2d.zero;
}
}
public override string ToString()
{
return "(" + this.x + ", " + this.y + ")";
}
public string ToString(string format)
{
return "not implemented";
}
public override int GetHashCode()
{
return this.x.GetHashCode() ^ this.y.GetHashCode() << 2;
}
public override bool Equals(object other)
{
if (!(other is Vector2d))
{
return false;
}
Vector2d vector2d = (Vector2d)other;
if (this.x.Equals(vector2d.x))
{
return this.y.Equals(vector2d.y);
}
else
{
return false;
}
}
public static double Dot(Vector2d lhs, Vector2d rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y;
}
public static double Angle(Vector2d from, Vector2d to)
{
return Mathd.Acos(Mathd.Clamp(Vector2d.Dot(from.normalized, to.normalized), -1d, 1d)) * 57.29578d;
}
public static double Distance(Vector2d a, Vector2d b)
{
return (a - b).magnitude;
}
public static Vector2d ClampMagnitude(Vector2d vector, double maxLength)
{
if (vector.sqrMagnitude > maxLength * maxLength)
{
return vector.normalized * maxLength;
}
else
{
return vector;
}
}
public static double SqrMagnitude(Vector2d a)
{
return (a.x * a.x + a.y * a.y);
}
public double SqrMagnitude()
{
return (this.x * this.x + this.y * this.y);
}
public static Vector2d Min(Vector2d lhs, Vector2d rhs)
{
return new Vector2d(Mathd.Min(lhs.x, rhs.x), Mathd.Min(lhs.y, rhs.y));
}
public static Vector2d Max(Vector2d lhs, Vector2d rhs)
{
return new Vector2d(Mathd.Max(lhs.x, rhs.x), Mathd.Max(lhs.y, rhs.y));
}
}
}