-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2.java
158 lines (125 loc) · 3.11 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
package game.vectors;
/**
* Vector Library
* CSCI 5611 Vector 2 Library [Example]
* Stephen J. Guy <[email protected]>
* Adjusted for this project by Tobias Moszer <[email protected]>
*/
public class Vector2 {
public double x, y;
public Vector2(double x, double y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + x+ "," + y +")";
}
public void reset() {
x = 0;
y = 0;
}
public double length() {
return Math.sqrt(x*x+y*y);
}
public double lengthSqr() {
return x*x+y*y;
}
public Vector2 plus(Vector2 rhs) {
return new Vector2(x+rhs.x, y+rhs.y);
}
public void add(Vector2 rhs) {
x += rhs.x;
y += rhs.y;
}
public Vector2 minus(Vector2 rhs) {
return new Vector2(x-rhs.x, y-rhs.y);
}
public void subtract(Vector2 rhs) {
x -= rhs.x;
y -= rhs.y;
}
public Vector2 times(double rhs) {
return new Vector2(x*rhs, y*rhs);
}
public void mul(double rhs) {
x *= rhs;
y *= rhs;
}
public Vector2 divide(double rhs) {
return new Vector2(x/rhs, y/rhs);
}
public void over(double rhs) {
x /= rhs;
y /= rhs;
}
public void clampToLength(double maxL) {
double magnitude = Math.sqrt(x*x + y*y);
if (magnitude > maxL){
x *= maxL/magnitude;
y *= maxL/magnitude;
}
}
public void setToLength(double newL) {
double magnitude = Math.sqrt(x*x + y*y);
x *= newL/magnitude;
y *= newL/magnitude;
}
public void normalize() {
double magnitude = Math.sqrt(x*x + y*y);
x /= magnitude;
y /= magnitude;
}
public Vector2 normalized() {
double magnitude = Math.sqrt(x*x + y*y);
return new Vector2(x/magnitude, y/magnitude);
}
public double distanceTo(Vector2 rhs) {
double dx = rhs.x - x;
double dy = rhs.y - y;
return (double) Math.sqrt(dx*dx + dy*dy);
}
public Vector2 copy() {
return new Vector2(x, y);
}
public boolean equals(Vector2 other) {
return x == other.x && y == other.y;
}
public boolean isZero() {
return x == 0 && y == 0;
}
public boolean exists() {
return !(Double.isNaN(x) || Double.isNaN(y));
}
public double getAngle() {
return Math.atan(y/x);
}
public static Vector2 interpolate(Vector2 a, Vector2 b, double t) {
return a.plus((b.minus(a)).times(t));
}
public static double interpolate(double a, double b, double t) {
return a + ((b-a)*t);
}
public static double dot(Vector2 a, Vector2 b) {
return a.x*b.x + a.y*b.y;
}
public static Vector2 projAB(Vector2 a, Vector2 b) {
return b.times(a.x*b.x + a.y*b.y);
}
public static Vector2 randomVector(double size) {
Vector2 vec = new Vector2(
Math.random() - .5f,
Math.random() - .5f
);
vec.setToLength(size);
return vec;
}
public static Vector2 zero() {
return new Vector2(0, 0);
}
public static Vector2 rotate(Vector2 vector, double angle) {
return new Vector2(
vector.x * Math.cos(angle) - vector.y * Math.sin(angle),
vector.x * Math.sin(angle) + vector.y * Math.cos(angle)
);
}
}