67 lines
1.5 KiB
Java
67 lines
1.5 KiB
Java
import java.awt.Point;
|
|
|
|
public class Vector2 extends Point {
|
|
public Vector2(int x, int y) {
|
|
super(x, y);
|
|
}
|
|
|
|
public void add(Point p) {
|
|
this.x += p.x;
|
|
this.y += p.y;
|
|
}
|
|
|
|
public void sub(Point p) {
|
|
this.x -= p.x;
|
|
this.y -= p.y;
|
|
}
|
|
|
|
public void mul(Point p) {
|
|
this.x *= p.x;
|
|
this.y *= p.y;
|
|
}
|
|
|
|
public void mul(int n) {
|
|
this.x *= n;
|
|
this.y *= n;
|
|
}
|
|
|
|
public static Vector2 lerp(Point a, Point b, double t) {
|
|
double ax = (double)a.x;
|
|
double bx = (double)b.x;
|
|
double ay = (double)a.y;
|
|
double by = (double)b.y;
|
|
|
|
return new Vector2((int)(ax + (bx - ax) * t), (int)(ay + (by - ay) * t));
|
|
}
|
|
|
|
public void lerpTo(Point a, double t) {
|
|
double ax = (double)this.x;
|
|
double bx = (double)a.x;
|
|
double ay = (double)this.y;
|
|
double by = (double)a.y;
|
|
|
|
this.x = (int)(ax + (bx - ax) * t);
|
|
this.y = (int)(ay + (by - ay) * t);
|
|
}
|
|
|
|
public double length() {
|
|
double x = (double)this.x;
|
|
double y = (double)this.y;
|
|
|
|
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
|
|
}
|
|
|
|
public void normalize() {
|
|
double x = (double)this.x;
|
|
double y = (double)this.y;
|
|
double length = this.length();
|
|
|
|
this.x = (int)(x / length);
|
|
this.y = (int)(y / length);
|
|
}
|
|
|
|
public Vector2 toLocal(Point p) {
|
|
return new Vector2(p.x - this.x, p.y - this.y);
|
|
}
|
|
}
|