forked from libgdx/libgdx
/
Vector2.java
600 lines (510 loc) · 17.6 KB
/
Vector2.java
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.math;
import java.io.Serializable;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.NumberUtils;
/** Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself
* @author badlogicgames@gmail.com */
public class Vector2 implements Serializable, Vector<Vector2> {
private static final long serialVersionUID = 913902788239530931L;
public final static Vector2 X = new Vector2(1, 0);
public final static Vector2 Y = new Vector2(0, 1);
public final static Vector2 Zero = new Vector2(0, 0);
/** the x-component of this vector **/
public float x;
/** the y-component of this vector **/
public float y;
/** Constructs a new vector at (0,0) */
public Vector2 () {
}
/** Constructs a vector with the given components
* @param x The x-component
* @param y The y-component */
public Vector2 (float x, float y) {
this.x = x;
this.y = y;
}
/** Constructs a vector from the given vector
* @param v The vector */
public Vector2 (Vector2 v) {
set(v);
}
@Override
public Vector2 cpy () {
return new Vector2(this);
}
public static float len (float x, float y) {
return (float)Math.sqrt(x * x + y * y);
}
@Override
public float len () {
return (float)Math.sqrt(x * x + y * y);
}
public static float len2 (float x, float y) {
return x * x + y * y;
}
@Override
public float len2 () {
return x * x + y * y;
}
@Override
public Vector2 set (Vector2 v) {
x = v.x;
y = v.y;
return this;
}
/** Sets the components of this vector
* @param x The x-component
* @param y The y-component
* @return This vector for chaining */
public Vector2 set (float x, float y) {
this.x = x;
this.y = y;
return this;
}
@Override
public Vector2 sub (Vector2 v) {
x -= v.x;
y -= v.y;
return this;
}
/** Substracts the other vector from this vector.
* @param x The x-component of the other vector
* @param y The y-component of the other vector
* @return This vector for chaining */
public Vector2 sub (float x, float y) {
this.x -= x;
this.y -= y;
return this;
}
@Override
public Vector2 nor () {
float len = len();
if (len != 0) {
x /= len;
y /= len;
}
return this;
}
@Override
public Vector2 add (Vector2 v) {
x += v.x;
y += v.y;
return this;
}
/** Adds the given components to this vector
* @param x The x-component
* @param y The y-component
* @return This vector for chaining */
public Vector2 add (float x, float y) {
this.x += x;
this.y += y;
return this;
}
public static float dot (float x1, float y1, float x2, float y2) {
return x1 * x2 + y1 * y2;
}
@Override
public float dot (Vector2 v) {
return x * v.x + y * v.y;
}
public float dot (float ox, float oy) {
return x * ox + y * oy;
}
@Override
public Vector2 scl (float scalar) {
x *= scalar;
y *= scalar;
return this;
}
/** Multiplies this vector by a scalar
* @return This vector for chaining */
public Vector2 scl (float x, float y) {
this.x *= x;
this.y *= y;
return this;
}
@Override
public Vector2 scl (Vector2 v) {
this.x *= v.x;
this.y *= v.y;
return this;
}
@Override
public Vector2 mulAdd (Vector2 vec, float scalar) {
this.x += vec.x * scalar;
this.y += vec.y * scalar;
return this;
}
@Override
public Vector2 mulAdd (Vector2 vec, Vector2 mulVec) {
this.x += vec.x * mulVec.x;
this.y += vec.y * mulVec.y;
return this;
}
public static float dst (float x1, float y1, float x2, float y2) {
final float x_d = x2 - x1;
final float y_d = y2 - y1;
return (float)Math.sqrt(x_d * x_d + y_d * y_d);
}
@Override
public float dst (Vector2 v) {
final float x_d = v.x - x;
final float y_d = v.y - y;
return (float)Math.sqrt(x_d * x_d + y_d * y_d);
}
/** @param x The x-component of the other vector
* @param y The y-component of the other vector
* @return the distance between this and the other vector */
public float dst (float x, float y) {
final float x_d = x - this.x;
final float y_d = y - this.y;
return (float)Math.sqrt(x_d * x_d + y_d * y_d);
}
public static float dst2 (float x1, float y1, float x2, float y2) {
final float x_d = x2 - x1;
final float y_d = y2 - y1;
return x_d * x_d + y_d * y_d;
}
@Override
public float dst2 (Vector2 v) {
final float x_d = v.x - x;
final float y_d = v.y - y;
return x_d * x_d + y_d * y_d;
}
/** @param x The x-component of the other vector
* @param y The y-component of the other vector
* @return the squared distance between this and the other vector */
public float dst2 (float x, float y) {
final float x_d = x - this.x;
final float y_d = y - this.y;
return x_d * x_d + y_d * y_d;
}
@Override
public Vector2 limit (float limit) {
return limit2(limit * limit);
}
@Override
public Vector2 limit2 (float limit2) {
float len2 = len2();
if (len2 > limit2) {
return scl((float)Math.sqrt(limit2 / len2));
}
return this;
}
@Override
public Vector2 clamp (float min, float max) {
final float len2 = len2();
if (len2 == 0f) return this;
float max2 = max * max;
if (len2 > max2) return scl((float)Math.sqrt(max2 / len2));
float min2 = min * min;
if (len2 < min2) return scl((float)Math.sqrt(min2 / len2));
return this;
}
@Override
public Vector2 setLength (float len) {
return setLength2(len * len);
}
@Override
public Vector2 setLength2 (float len2) {
float oldLen2 = len2();
return (oldLen2 == 0 || oldLen2 == len2) ? this : scl((float)Math.sqrt(len2 / oldLen2));
}
/** Converts this {@code Vector2} to a string in the format {@code (x,y)}.
* @return a string representation of this object. */
@Override
public String toString () {
return "(" + x + "," + y + ")";
}
/** Sets this {@code Vector2} to the value represented by the specified string according to the format of {@link #toString()}.
* @param v the string.
* @return this vector for chaining */
public Vector2 fromString (String v) {
int s = v.indexOf(',', 1);
if (s != -1 && v.charAt(0) == '(' && v.charAt(v.length() - 1) == ')') {
try {
float x = Float.parseFloat(v.substring(1, s));
float y = Float.parseFloat(v.substring(s + 1, v.length() - 1));
return this.set(x, y);
} catch (NumberFormatException ex) {
// Throw a GdxRuntimeException
}
}
throw new GdxRuntimeException("Malformed Vector2: " + v);
}
/** Left-multiplies this vector by the given matrix
* @param mat the matrix
* @return this vector */
public Vector2 mul (Matrix3 mat) {
float x = this.x * mat.val[0] + this.y * mat.val[3] + mat.val[6];
float y = this.x * mat.val[1] + this.y * mat.val[4] + mat.val[7];
this.x = x;
this.y = y;
return this;
}
/** Calculates the 2D cross product between this and the given vector.
* @param v the other vector
* @return the cross product */
public float crs (Vector2 v) {
return this.x * v.y - this.y * v.x;
}
/** Calculates the 2D cross product between this and the given vector.
* @param x the x-coordinate of the other vector
* @param y the y-coordinate of the other vector
* @return the cross product */
public float crs (float x, float y) {
return this.x * y - this.y * x;
}
/** @return the angle in degrees of this vector (point) relative to the x-axis. Angles are towards the positive y-axis
* (typically counter-clockwise) and between 0 and 360.
* @deprecated use {@link #angleDeg()} instead. */
@Deprecated
public float angle () {
float angle = (float)Math.atan2(y, x) * MathUtils.radiansToDegrees;
if (angle < 0) angle += 360;
return angle;
}
/** @return the angle in degrees of this vector (point) relative to the given vector. Angles are towards the negative y-axis
* (typically clockwise) between -180 and +180
* @deprecated use {@link #angleDeg(Vector2)} instead. Be ware of the changes in returned angle to counter-clockwise and the range. */
@Deprecated
public float angle (Vector2 reference) {
return (float)Math.atan2(crs(reference), dot(reference)) * MathUtils.radiansToDegrees;
}
/** @return the angle in degrees of this vector (point) relative to the x-axis. Angles are towards the positive y-axis
* (typically counter-clockwise) and in the [0, 360) range. */
public float angleDeg () {
float angle = (float)Math.atan2(y, x) * MathUtils.radiansToDegrees;
if (angle < 0) angle += 360;
return angle;
}
/** @return the angle in degrees of this vector (point) relative to the given vector. Angles are towards the positive y-axis
* (typically counter-clockwise.) in the [0, 360) range */
public float angleDeg (Vector2 reference) {
float angle = (float)Math.atan2(reference.crs(this), reference.dot(this)) * MathUtils.radiansToDegrees;
if (angle < 0) angle += 360;
return angle;
}
/** @return the angle in radians of this vector (point) relative to the x-axis. Angles are towards the positive y-axis.
* (typically counter-clockwise) */
public float angleRad () {
return (float)Math.atan2(y, x);
}
/** @return the angle in radians of this vector (point) relative to the given vector. Angles are towards the positive y-axis.
* (typically counter-clockwise.) */
public float angleRad (Vector2 reference) {
return (float)Math.atan2(reference.crs(this), reference.dot(this));
}
/** Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
* @param degrees The angle in degrees to set.
* @deprecated use {@link #setAngleDeg(float)} instead. */
@Deprecated
public Vector2 setAngle (float degrees) {
return setAngleRad(degrees * MathUtils.degreesToRadians);
}
/** Sets the angle of the vector in degrees relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
* @param degrees The angle in degrees to set. */
public Vector2 setAngleDeg (float degrees) {
return setAngleRad(degrees * MathUtils.degreesToRadians);
}
/** Sets the angle of the vector in radians relative to the x-axis, towards the positive y-axis (typically counter-clockwise).
* @param radians The angle in radians to set. */
public Vector2 setAngleRad (float radians) {
this.set(len(), 0f);
this.rotateRad(radians);
return this;
}
/** Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
* @param degrees the angle in degrees
* @deprecated use {@link #rotateDeg(float)} instead. */
@Deprecated
public Vector2 rotate (float degrees) {
return rotateRad(degrees * MathUtils.degreesToRadians);
}
/** Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
* @param degrees the angle in degrees */
public Vector2 rotateDeg (float degrees) {
return rotateRad(degrees * MathUtils.degreesToRadians);
}
/** Rotates the Vector2 by the given angle, counter-clockwise assuming the y-axis points up.
* @param radians the angle in radians */
public Vector2 rotateRad (float radians) {
float cos = (float)Math.cos(radians);
float sin = (float)Math.sin(radians);
float newX = this.x * cos - this.y * sin;
float newY = this.x * sin + this.y * cos;
this.x = newX;
this.y = newY;
return this;
}
/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
* @param degrees the angle in degrees
* @param reference center Vector2
* @deprecated use {@link #rotateAroundDeg(Vector2, float)} instead. */
@Deprecated
public Vector2 rotateAround (Vector2 reference, float degrees) {
return this.sub(reference).rotateDeg(degrees).add(reference);
}
/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
* @param degrees the angle in degrees
* @param reference center Vector2 */
public Vector2 rotateAroundDeg (Vector2 reference, float degrees) {
return this.sub(reference).rotateDeg(degrees).add(reference);
}
/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up.
* @param radians the angle in radians
* @param reference center Vector2 */
public Vector2 rotateAroundRad (Vector2 reference, float radians) {
return this.sub(reference).rotateRad(radians).add(reference);
}
/** Rotates the Vector2 by 90 degrees in the specified direction, where >= 0 is counter-clockwise and < 0 is clockwise. */
public Vector2 rotate90 (int dir) {
float x = this.x;
if (dir >= 0) {
this.x = -y;
y = x;
} else {
this.x = y;
y = -x;
}
return this;
}
@Override
public Vector2 lerp (Vector2 target, float alpha) {
final float invAlpha = 1.0f - alpha;
this.x = (x * invAlpha) + (target.x * alpha);
this.y = (y * invAlpha) + (target.y * alpha);
return this;
}
@Override
public Vector2 interpolate (Vector2 target, float alpha, Interpolation interpolation) {
return lerp(target, interpolation.apply(alpha));
}
@Override
public Vector2 setToRandomDirection () {
float theta = MathUtils.random(0f, MathUtils.PI2);
return this.set(MathUtils.cos(theta), MathUtils.sin(theta));
}
@Override
public int hashCode () {
final int prime = 31;
int result = 1;
result = prime * result + NumberUtils.floatToIntBits(x);
result = prime * result + NumberUtils.floatToIntBits(y);
return result;
}
@Override
public boolean equals (Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Vector2 other = (Vector2)obj;
if (NumberUtils.floatToIntBits(x) != NumberUtils.floatToIntBits(other.x)) return false;
if (NumberUtils.floatToIntBits(y) != NumberUtils.floatToIntBits(other.y)) return false;
return true;
}
@Override
public boolean epsilonEquals (Vector2 other, float epsilon) {
if (other == null) return false;
if (Math.abs(other.x - x) > epsilon) return false;
if (Math.abs(other.y - y) > epsilon) return false;
return true;
}
/** Compares this vector with the other vector, using the supplied epsilon for fuzzy equality testing.
* @return whether the vectors are the same. */
public boolean epsilonEquals (float x, float y, float epsilon) {
if (Math.abs(x - this.x) > epsilon) return false;
if (Math.abs(y - this.y) > epsilon) return false;
return true;
}
/** Compares this vector with the other vector using MathUtils.FLOAT_ROUNDING_ERROR for fuzzy equality testing
* @param other other vector to compare
* @return true if vector are equal, otherwise false */
public boolean epsilonEquals (final Vector2 other) {
return epsilonEquals(other, MathUtils.FLOAT_ROUNDING_ERROR);
}
/** Compares this vector with the other vector using MathUtils.FLOAT_ROUNDING_ERROR for fuzzy equality testing
* @param x x component of the other vector to compare
* @param y y component of the other vector to compare
* @return true if vector are equal, otherwise false */
public boolean epsilonEquals (float x, float y) {
return epsilonEquals(x, y, MathUtils.FLOAT_ROUNDING_ERROR);
}
@Override
public boolean isUnit () {
return isUnit(0.000000001f);
}
@Override
public boolean isUnit (final float margin) {
return Math.abs(len2() - 1f) < margin;
}
@Override
public boolean isZero () {
return x == 0 && y == 0;
}
@Override
public boolean isZero (final float margin) {
return len2() < margin;
}
@Override
public boolean isOnLine (Vector2 other) {
return MathUtils.isZero(x * other.y - y * other.x);
}
@Override
public boolean isOnLine (Vector2 other, float epsilon) {
return MathUtils.isZero(x * other.y - y * other.x, epsilon);
}
@Override
public boolean isCollinear (Vector2 other, float epsilon) {
return isOnLine(other, epsilon) && dot(other) > 0f;
}
@Override
public boolean isCollinear (Vector2 other) {
return isOnLine(other) && dot(other) > 0f;
}
@Override
public boolean isCollinearOpposite (Vector2 other, float epsilon) {
return isOnLine(other, epsilon) && dot(other) < 0f;
}
@Override
public boolean isCollinearOpposite (Vector2 other) {
return isOnLine(other) && dot(other) < 0f;
}
@Override
public boolean isPerpendicular (Vector2 vector) {
return MathUtils.isZero(dot(vector));
}
@Override
public boolean isPerpendicular (Vector2 vector, float epsilon) {
return MathUtils.isZero(dot(vector), epsilon);
}
@Override
public boolean hasSameDirection (Vector2 vector) {
return dot(vector) > 0;
}
@Override
public boolean hasOppositeDirection (Vector2 vector) {
return dot(vector) < 0;
}
@Override
public Vector2 setZero () {
this.x = 0;
this.y = 0;
return this;
}
}