-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector.java
More file actions
163 lines (141 loc) · 4 KB
/
Copy pathVector.java
File metadata and controls
163 lines (141 loc) · 4 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class helps with vector math operations
*/
public class Vector {
// x and y components of the vector
public float x;
public float y;
// Small number to prevent errors with very small floats
public static final float EPS = 1e-6f;
// Default constructor initializes vector to (0, 0)
public Vector() {
this(0f, 0f);
}
// Constructor to initialize the vector with specific x and y components
public Vector(float x, float y) {
this.x = x;
this.y = y;
}
// Change the x and y to new values
public Vector set(float x, float y) {
this.x = x;
this.y = y;
return this;
}
// Copies the vector
public Vector copy() {
return new Vector(x, y);
}
// Calculates and returns vector length using sqrt for performance
public float length() {
return (float) Math.sqrt(x * x + y * y);
}
// Calculates and returns the squared length of the vector
public float lengthSquared() {
return x * x + y * y;
}
// Makes vector length 1 if its length is greater
// than EPS
public Vector normalize() {
float len = 0f;
len = length();
if (len > EPS) {
x /= len;
y /= len;
}
return this;
}
// Adds another vector to this vector
public Vector add(Vector o) {
x += o.x;
y += o.y;
return this;
}
// Adds specific x and y values to this vector and returns the updated vector
public Vector add(float ox, float oy) {
x += ox;
y += oy;
return this;
}
// Subtracts another vector from this vector and returns the updated vector
public Vector sub(Vector o) {
x -= o.x;
y -= o.y;
return this;
}
// Scales the vector by a scalar value and returns the updated vector
public Vector scale(float s) {
x *= s;
y *= s;
return this;
}
// Finds the dot product of this vector with another vector
public float dot(Vector o) {
return x * o.x + y * o.y;
}
// Finds the cross product of this vector with another vector
public float cross(Vector o) {
return x * o.y - y * o.x;
}
// Moves this vector partway to another vector based on t
public Vector lerp(Vector o, float t) {
float t1 = 0f;
t1 = 1f - t;
x = x * t1 + o.x * t;
y = y * t1 + o.y * t;
return this;
}
// Limits the length of the vector to a maximum value
public Vector limit(float max) {
float len = 0f;
float ratio = 0f;
len = length();
if (len > max && len > EPS) {
ratio = max / len;
x *= ratio;
y *= ratio;
}
return this;
}
// Rotates the vector by specified radians
public Vector rotate(float radians) {
float cos = 0f;
float sin = 0f;
float nx = 0f;
float ny = 0f;
cos = (float) Math.cos(radians);
sin = (float) Math.sin(radians);
nx = x * cos - y * sin;
ny = x * sin + y * cos;
x = nx;
y = ny;
return this;
}
// Gets the angle from vector to x-axis
public float angle() {
return (float) Math.atan2(y, x);
}
// Finds the distance between this vector and another vector
public float distance(Vector o) {
float dx = 0f;
float dy = 0f;
dx = x - o.x;
dy = y - o.y;
return (float) Math.sqrt(dx * dx + dy * dy);
}
// Finds the squared distance between this vector and another vector
public float distanceSquared(Vector o) {
float dx = 0f;
float dy = 0f;
dx = x - o.x;
dy = y - o.y;
return dx * dx + dy * dy;
}
// Returns a string representation of the vector in form "Vector(x, y)"
@Override
public String toString() {
return "Vector(" + x + ", " + y + ")";
}
}