-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2D.java
More file actions
166 lines (148 loc) · 4.32 KB
/
Vector2D.java
File metadata and controls
166 lines (148 loc) · 4.32 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
164
165
166
package frc.excalib.control.math;
import edu.wpi.first.epilogue.Logged;
import edu.wpi.first.math.geometry.Rotation2d;
/**
* A class representing a Vector in two dimensions;
* the Vector is defined by x value and y value
*
* @author Itay
*/
@Logged
public class Vector2D {
private double m_x, m_y;
/**
* A constructor that takes two doubles representing the x and y components:
*
* @param x The x component of the Vector
* @param y The y component of the Vector
*/
public Vector2D(double x, double y) {
this.m_x = x;
this.m_y = y;
}
/**
* A constructor that takes a double representing the distance,
* and a Rotation2D representing the angle:
*
* @param magnitude The distance from the origin
* @param direction The angle of the Vector
*/
public Vector2D(double magnitude, Rotation2d direction) {
this.m_x = magnitude * direction.getCos();
this.m_y = magnitude * direction.getSin();
}
/**
* A function to get the x component of the Vector
*
* @return x component
*/
public double getX() {
return m_x;
}
/**
* A function to get the y component of the Vector
*
* @return y component
*/
public double getY() {
return m_y;
}
/**
* A function to get the distance from the origin
*
* @return distance
*/
public double getDistance() {
return Math.hypot(m_x, m_y);
}
/**
* A function to get the direction of the Vector
*
* @return direction
*/
public Rotation2d getDirection() {
return new Rotation2d(Math.atan2(m_y, m_x));
}
/**
* A function that adds another Vector to this Vector
*
* @param other The other vector to add
* @return a new Vector2D that represents the sum of the Vectors
*/
public Vector2D plus(Vector2D other) {
return new Vector2D(m_x + other.m_x, m_y + other.m_y);
}
/**
* A function that multiplies the Vector by a scalar
*
* @param scalar The scalar to multiply by
* @return a new Vector2D that represents the scaled Vector
*/
public Vector2D mul(double scalar) {
return new Vector2D(m_x * scalar, m_y * scalar);
}
/**
* A function that rotates the Vector by a given angle
*
* @param deltaDirection The angle to rotate by
* @return a new Vector2D that represents the rotated Vector
*/
public Vector2D rotate(Rotation2d deltaDirection) {
double cosTheta = deltaDirection.getCos();
double sinTheta = deltaDirection.getSin();
double newX = m_x * cosTheta - m_y * sinTheta;
double newY = m_x * sinTheta + m_y * cosTheta;
return new Vector2D(newX, newY);
}
/**
* Sets the x component of the Vector
*
* @param x The new x component
*/
public void setX(double x) {
this.m_x = x;
}
/**
* Sets the y component of the Vector
*
* @param y The new y component
*/
public void setY(double y) {
this.m_y = y;
}
/**
* Sets the magnitude of the Vector while keeping its direction
*
* @param magnitude The new magnitude
*/
public void setMagnitude(double magnitude) {
double currentMagnitude = getDistance();
if (currentMagnitude != 0) {
double scale = magnitude / currentMagnitude;
this.m_x *= scale;
this.m_y *= scale;
} else {
// When the current magnitude is zero, direction is undefined.
// Default to setting x to the magnitude and y to zero.
this.m_x = magnitude;
this.m_y = 0;
}
}
/**
* Sets the direction of the Vector while keeping its magnitude
*
* @param direction The new direction
*/
public void setDirection(Rotation2d direction) {
double magnitude = getDistance();
this.m_x = magnitude * direction.getCos();
this.m_y = magnitude * direction.getSin();
}
public Vector2D limit(Vector2D limit) {
Vector2D output = new Vector2D(m_x, m_y);
output.rotate(limit.getDirection().unaryMinus());
output.setX(MathUtils.limitTo(limit.getDistance(), output.m_x));
output.setDirection(this.getDirection());
return output;
}
}