forked from konatakun/powerbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2.java
100 lines (78 loc) · 1.93 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
package org.powerbot.script;
import java.awt.Point;
/**
* Vector2
* An object representing a mathematical vector in 2D cartesian coordinate space.
*/
public class Vector2 implements Comparable<Vector2> {
public final int x;
public final int y;
public Vector2() {
this(0, 0);
}
public Vector2(final Point p) {
this(p.x, p.y);
}
public Vector2(final int x, final int y) {
this.x = x;
this.y = y;
}
public Vector2 add(final Vector2 u) {
return new Vector2(x + u.x, y + u.y);
}
public Vector2 mul(final double u) {
return new Vector2((int) (x * u), (int) (y * u));
}
public double cross(final Vector2 U, final double a) {
return magnitude() * U.magnitude() * Math.sin(a);
}
public double dot(final Vector2 U) {
return x * U.x + y * U.y;
}
public final double distanceTo(final Vector2 v) {
return Math.sqrt(Math.pow(v.x - x, 2) + Math.pow(v.y - y, 2));
}
public final double gradientTo(final Vector2 v) {
return (double) (v.y - y) / (v.x - x);
}
public final double angleTo(final Vector2 v) {
double a = Math.atan2(v.y - y, v.x - x);
if (a < 0) {
a = Math.abs(a);
} else {
a = 2 * Math.PI - a;
}
return a;
}
public long toLong2D() {
return (long) x << 32 | y & 0xffffffffL;
}
public int[] toMatrix() {
return new int[]{x, y};
}
public Point toPoint() {
return new Point(x, y);
}
public double magnitude() {
return Math.sqrt(x * x + y * y);
}
@Override
public int compareTo(final Vector2 o) {
return y < o.y ? -1 : y > o.y ? 1 : x < o.x ? -1 : x > o.x ? 1 : 0;
}
@Override
public String toString() {
return String.format("(%s, %s)", x, y);
}
@Override
public boolean equals(final Object o) {
final Vector2 v2;
final Vector3 v3;
return o instanceof Vector3 ? (v3 = (Vector3) o).z == 0 && v3.y == y && v3.x == x
: o instanceof Vector2 && (v2 = (Vector2) o).y == y && v2.x == x;
}
@Override
public int hashCode() {
return (y & 0xffff) << 16 | (x & 0xffff);
}
}