APL/DEV 3.2/TP02/Flocon/Vector2.java
2022-10-12 13:29:36 +02:00

103 lines
2.5 KiB
Java

import java.awt.Point;
public class Vector2 extends Point {
public Vector2(int x, int y) {
super(x, y);
}
public Vector2 add(Point p) {
this.x += p.x;
this.y += p.y;
return this;
}
public Vector2 sub(Point p) {
this.x -= p.x;
this.y -= p.y;
return this;
}
public Vector2 mul(Point p) {
this.x *= p.x;
this.y *= p.y;
return this;
}
public Vector2 mul(int n) {
this.x *= n;
this.y *= n;
return this;
}
public Vector2 div(double n) {
this.x = (int)((double)this.x / n);
this.y = (int)((double)this.y / n);
return this;
}
public static Vector2 add(Vector2 a, Vector2 b) {
return new Vector2(a.x + b.x, a.y + b.y);
}
public static Vector2 rotate(Vector2 v, double a) {
a = Math.toRadians(a);
double x1 = (double)v.x;
double y1 = (double)v.y;
return new Vector2((int)(Math.cos(a) * x1 - Math.sin(a) * y1), (int)(Math.sin(a) * x1 + Math.cos(a) * y1));
}
public void rotate(double a) {
a = Math.toRadians(a);
double x1 = (double)this.x;
double y1 = (double)this.y;
this.x = (int)(Math.cos(a) * x1 - Math.sin(a) * y1);
this.y = (int)(Math.sin(a) * x1 + Math.cos(a) * y1);
}
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);
}
@Override
public String toString() {
return "[" + this.x + ", " + this.y + "]";
}
}