-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
VectorE.cs
332 lines (292 loc) · 10.9 KB
/
VectorE.cs
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
330
331
332
using System;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Vector extensions
/// </summary>
public static class VectorE
{
#region Vector2
/// <summary>
/// Returns a new vector with zero Y component
/// </summary>
public static Vector2 ToVector2X(this Vector2 vector)
{
return new Vector2(vector.x, 0);
}
/// <summary>
/// Returns a new vector with zero X component
/// </summary>
public static Vector2 ToVector2Y(this Vector2 vector)
{
return new Vector2(0, vector.y);
}
/// <summary>
/// Projects the vector onto the three dimensional XY plane
/// </summary>
public static Vector3 ToVector3XY(this Vector2 vector)
{
return new Vector3(vector.x, vector.y, 0);
}
/// <summary>
/// Projects the vector onto the three dimensional XZ plane
/// </summary>
public static Vector3 ToVector3XZ(this Vector2 vector)
{
return new Vector3(vector.x, 0, vector.y);
}
/// <summary>
/// Projects the vector onto the three dimensional YZ plane
/// </summary>
public static Vector3 ToVector3YZ(this Vector2 vector)
{
return new Vector3(0, vector.x, vector.y);
}
/// <summary>
/// Returns true if vectors lie on the same line, false otherwise
/// </summary>
public static bool IsCollinear(this Vector2 vector, Vector2 other)
{
return Mathf.Abs(PerpDot(vector, other)) < Geometry.Epsilon;
}
/// <summary>
/// Returns a new vector rotated counterclockwise by 90°
/// </summary>
/// <remarks>
/// Hill, F. S. Jr. "The Pleasures of 'Perp Dot' Products."
/// Ch. II.5 in Graphics Gems IV (Ed. P. S. Heckbert). San Diego: Academic Press, pp. 138-148, 1994
/// </remarks>
public static Vector2 Perp(this Vector2 vector)
{
return new Vector2(-vector.y, vector.x);
}
/// <summary>
/// Returns a perp dot product of vectors
/// </summary>
/// <remarks>
/// Hill, F. S. Jr. "The Pleasures of 'Perp Dot' Products."
/// Ch. II.5 in Graphics Gems IV (Ed. P. S. Heckbert). San Diego: Academic Press, pp. 138-148, 1994
/// </remarks>
public static float PerpDot(Vector2 a, Vector2 b)
{
return a.x*b.y - a.y*b.x;
}
/// <summary>
/// Returns a signed clockwise angle in degrees [-180, 180] between from and to
/// </summary>
/// <param name="from">The angle extends round from this vector</param>
/// <param name="to">The angle extends round to this vector</param>
public static float SignedAngle(Vector2 from, Vector2 to)
{
return Mathf.Atan2(PerpDot(to, from), Vector2.Dot(to, from))*Mathf.Rad2Deg;
}
/// <summary>
/// Returns a clockwise angle in degrees [0, 360] between from and to
/// </summary>
/// <param name="from">The angle extends round from this vector</param>
/// <param name="to">The angle extends round to this vector</param>
public static float Angle360(Vector2 from, Vector2 to)
{
float angle = SignedAngle(from, to);
while (angle < 0)
{
angle += 360;
}
return angle;
}
/// <summary>
/// Calculates the linear parameter t that produces the interpolant value within the range [a, b].
/// </summary>
public static Vector2 InverseLerp(Vector2 a, Vector2 b, Vector2 value)
{
return new Vector2(
Mathf.InverseLerp(a.x, b.x, value.x),
Mathf.InverseLerp(a.y, b.y, value.y));
}
/// <summary>
/// Returns a new vector rotated clockwise by the specified angle
/// </summary>
public static Vector2 RotateCW(this Vector2 vector, float degrees)
{
float radians = degrees*Mathf.Deg2Rad;
float sin = Mathf.Sin(radians);
float cos = Mathf.Cos(radians);
return new Vector2(
vector.x*cos + vector.y*sin,
vector.y*cos - vector.x*sin);
}
/// <summary>
/// Returns a new vector rotated counterclockwise by the specified angle
/// </summary>
public static Vector2 RotateCCW(this Vector2 vector, float degrees)
{
float radians = degrees*Mathf.Deg2Rad;
float sin = Mathf.Sin(radians);
float cos = Mathf.Cos(radians);
return new Vector2(
vector.x*cos - vector.y*sin,
vector.y*cos + vector.x*sin);
}
/// <summary>
/// Returns a new vector rotated clockwise by 45°
/// </summary>
public static Vector2 RotateCW45(this Vector2 vector)
{
return new Vector2((vector.x + vector.y)*PTUtils.Sqrt05, (vector.y - vector.x)*PTUtils.Sqrt05);
}
/// <summary>
/// Returns a new vector rotated counterclockwise by 45°
/// </summary>
public static Vector2 RotateCCW45(this Vector2 vector)
{
return new Vector2((vector.x - vector.y)*PTUtils.Sqrt05, (vector.y + vector.x)*PTUtils.Sqrt05);
}
/// <summary>
/// Returns a new vector rotated clockwise by 90°
/// </summary>
public static Vector2 RotateCW90(this Vector2 vector)
{
return new Vector2(vector.y, -vector.x);
}
/// <summary>
/// Returns a new vector rotated counterclockwise by 90°
/// </summary>
public static Vector2 RotateCCW90(this Vector2 vector)
{
return new Vector2(-vector.y, vector.x);
}
public static string ToString(this Vector2 vector, string format, IFormatProvider formatProvider)
{
return string.Format("({0}, {1})", vector.x.ToString(format, formatProvider), vector.y.ToString(format, formatProvider));
}
#endregion Vector2
#region Vector2Int
/// <summary>
/// Returns a perp of vector
/// </summary>
/// <remarks>
/// Hill, F. S. Jr. "The Pleasures of 'Perp Dot' Products."
/// Ch. II.5 in Graphics Gems IV (Ed. P. S. Heckbert). San Diego: Academic Press, pp. 138-148, 1994
/// </remarks>
public static Vector2Int Perp(this Vector2Int vector)
{
return new Vector2Int(-vector.y, vector.x);
}
/// <summary>
/// Returns a perp dot product of vectors
/// </summary>
/// <remarks>
/// Hill, F. S. Jr. "The Pleasures of 'Perp Dot' Products."
/// Ch. II.5 in Graphics Gems IV (Ed. P. S. Heckbert). San Diego: Academic Press, pp. 138-148, 1994
/// </remarks>
public static int PerpDot(Vector2Int a, Vector2Int b)
{
return a.x*b.y - a.y*b.x;
}
#endregion Vector2Int
#region Vector3
/// <summary>
/// Returns a new vector with zero Y and Z components
/// </summary>
public static Vector3 ToVector3X(this Vector3 vector)
{
return new Vector3(vector.x, 0, 0);
}
/// <summary>
/// Returns a new vector with zero X and Z components
/// </summary>
public static Vector3 ToVector3Y(this Vector3 vector)
{
return new Vector3(0, vector.y, 0);
}
/// <summary>
/// Returns a new vector with zero X and Y components
/// </summary>
public static Vector3 ToVector3Z(this Vector3 vector)
{
return new Vector3(0, 0, vector.z);
}
/// <summary>
/// Returns a new vector with zero Z component
/// </summary>
public static Vector3 ToVector3XY(this Vector3 vector)
{
return new Vector3(vector.x, vector.y, 0);
}
/// <summary>
/// Returns a new vector with zero Y component
/// </summary>
public static Vector3 ToVector3XZ(this Vector3 vector)
{
return new Vector3(vector.x, 0, vector.z);
}
/// <summary>
/// Returns a new vector with zero X component
/// </summary>
public static Vector3 ToVector3YZ(this Vector3 vector)
{
return new Vector3(0, vector.y, vector.z);
}
/// <summary>
/// Returns a new Vector2 made from X and Y components of this vector
/// </summary>
public static Vector2 ToVector2XY(this Vector3 vector)
{
return new Vector2(vector.x, vector.y);
}
/// <summary>
/// Returns a new Vector2 made from X and Z components of this vector
/// </summary>
public static Vector2 ToVector2XZ(this Vector3 vector)
{
return new Vector2(vector.x, vector.z);
}
/// <summary>
/// Returns a new Vector2 made from Y and Z components of this vector
/// </summary>
public static Vector2 ToVector2YZ(this Vector3 vector)
{
return new Vector2(vector.y, vector.z);
}
/// <summary>
/// Returns an angle in degrees [0, 360] between from and to
/// </summary>
/// <param name="from">The angle extends round from this vector</param>
/// <param name="to">The angle extends round to this vector</param>
/// <param name="normal">Up direction of the clockwise axis</param>
public static float Angle360(Vector3 from, Vector3 to, Vector3 normal)
{
float angle = Vector3.SignedAngle(from, to, normal);
while (angle < 0)
{
angle += 360;
}
return angle;
}
/// <summary>
/// Calculates the linear parameter t that produces the interpolant value within the range [a, b].
/// </summary>
public static Vector3 InverseLerp(Vector3 a, Vector3 b, Vector3 value)
{
return new Vector3(
Mathf.InverseLerp(a.x, b.x, value.x),
Mathf.InverseLerp(a.y, b.y, value.y),
Mathf.InverseLerp(a.z, b.z, value.z));
}
#endregion Vector3
#region Vector4
/// <summary>
/// Calculates the linear parameter t that produces the interpolant value within the range [a, b].
/// </summary>
public static Vector4 InverseLerp(Vector4 a, Vector4 b, Vector4 value)
{
return new Vector4(
Mathf.InverseLerp(a.x, b.x, value.x),
Mathf.InverseLerp(a.y, b.y, value.y),
Mathf.InverseLerp(a.z, b.z, value.z),
Mathf.InverseLerp(a.w, b.w, value.w));
}
#endregion Vector4
}
}