Files
2022NoWayHome/src/main/java/frc4388/utility/Vector2D.java
T

159 lines
3.6 KiB
Java
Raw Normal View History

2022-03-16 14:33:40 -06:00
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc4388.utility;
2022-03-16 15:05:33 -06:00
import java.security.InvalidParameterException;
import org.opencv.core.Point;
2022-03-16 15:13:34 -06:00
import edu.wpi.first.math.geometry.Translation2d;
2022-03-16 14:33:40 -06:00
import edu.wpi.first.wpilibj.drive.Vector2d;
2022-03-16 15:19:29 -06:00
/** Aarav's good vector class (better than WPILib)
2022-03-16 14:35:55 -06:00
* @author Aarav Shah */
2022-03-16 14:33:40 -06:00
public class Vector2D extends Vector2d {
public double x;
public double y;
public double angle;
public Vector2D() {
2022-03-16 15:13:34 -06:00
this(0, 0);
2022-03-16 14:33:40 -06:00
}
public Vector2D(double x, double y) {
super(x, y);
this.x = x;
this.y = y;
this.angle = Math.atan2(this.y, this.x);
}
2022-03-16 15:05:33 -06:00
public Vector2D(double[] vec) {
2022-03-16 15:13:34 -06:00
this(vec[0], vec[1]);
2022-03-16 15:05:33 -06:00
if (vec.length != 2) {
throw new InvalidParameterException();
}
}
public Vector2D(Point p) {
2022-03-16 15:13:34 -06:00
this(p.x, p.y);
}
2022-03-16 15:05:33 -06:00
2022-03-16 15:13:34 -06:00
public Vector2D(Translation2d t) {
this(t.getX(), t.getY());
2022-03-16 15:05:33 -06:00
}
2022-03-16 14:33:40 -06:00
/**
* Add two vectors, component-wise.
* @param v1 First vector in the addition.
* @param v2 Second vector in the addition.
* @return New vector which is the sum.
*/
public static Vector2D add(Vector2D v1, Vector2D v2) {
return new Vector2D(v1.x + v2.x, v1.y + v2.y);
}
2022-03-16 15:46:39 -06:00
/**
* Adds vector to current object
* @param v Vector to add
*/
public void add(Vector2D v) {
2022-03-17 12:44:15 -06:00
this.x += v.x;
this.y += v.x;
2022-03-16 15:46:39 -06:00
}
2022-03-16 14:33:40 -06:00
/**
* Subtract two vectors, component-wise.
* @param v1 First vector in the subtraction.
* @param v2 Second vector in the subtraction.
* @return New vector which is the difference.
*/
public static Vector2D subtract(Vector2D v1, Vector2D v2) {
return new Vector2D(v1.x - v2.x, v1.y - v2.y);
}
2022-03-16 15:46:39 -06:00
/**
* Subtracts vector from current object
* @param v Vector to subtract
*/
public void subtract(Vector2D v) {
2022-03-17 12:44:15 -06:00
this.x -= v.x;
this.y -= v.x;
2022-03-16 15:46:39 -06:00
}
2022-03-16 14:33:40 -06:00
/**
* Multiply a vector with a scalar, component-wise.
* @param v1 Vector to multiply.
* @param v2 Scalar to multiply.
* @return New vector which is the product.
*/
public static Vector2D multiply(Vector2D v1, double scalar) {
return new Vector2D(scalar * v1.x, scalar * v1.y);
}
2022-03-16 15:46:39 -06:00
/**
* Multiply a vector with a scalar, component-wise.
* @param scalar Scalar to multiply
*/
public void multiply(double scalar) {
2022-03-17 12:44:15 -06:00
this.x *= scalar;
this.y *= scalar;
2022-03-16 15:46:39 -06:00
}
2022-03-16 14:33:40 -06:00
/**
* Divide a vector with a scalar, component-wise.
* @param v1 Vector to divide.
* @param v2 Scalar to divide.
* @return New vector which is the division.
*/
public static Vector2D divide(Vector2D v1, double scalar) {
return new Vector2D(v1.x / scalar, v1.y / scalar);
}
2022-03-16 15:46:39 -06:00
/**
* Divide a vector with a scalar, component-wise.
* @param scalar Scalar to divide
*/
public void divide(double scalar) {
2022-03-17 12:44:15 -06:00
this.x /= scalar;
this.y /= scalar;
2022-03-16 15:46:39 -06:00
}
2022-03-16 14:33:40 -06:00
/**
* Find unit vector.
* @return The unit vector.
*/
public Vector2D unit() {
return new Vector2D(this.x / this.magnitude(), this.y / this.magnitude());
}
/**
* Round a vector to a certain number of places, component-wise.
* @param v Vector to round.
* @param places Number of places to round to.
* @return New rounded vector.
*/
public static Vector2D round(Vector2D v, int places) {
int scale = (int) Math.pow(10, places);
v = Vector2D.multiply(v, scale);
v.x = Math.round(v.x);
v.y = Math.round(v.y);
v.x = v.x / scale;
v.y = v.y / scale;
return v;
}
@Override
public String toString() {
2022-03-16 18:58:58 -06:00
return "<" + this.x + ", " + this.y + ">";
2022-03-16 14:33:40 -06:00
}
}