This repository has been archived on 2025-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
aud-2021-h07-gruphi/src/main/java/de/oshgnacknak/gruphi/Vector.java

65 lines
1.1 KiB
Java
Raw Normal View History

2021-06-30 18:23:49 +02:00
package de.oshgnacknak.gruphi;
public class Vector {
2021-06-30 18:44:08 +02:00
public double x, y;
2021-06-30 18:23:49 +02:00
public Vector(double x, double y) {
this.x = x;
this.y = y;
}
2021-06-30 18:44:08 +02:00
public Vector copy() {
2021-06-30 18:23:49 +02:00
return new Vector(x, y);
}
2021-06-30 18:44:08 +02:00
public Vector add(Vector v) {
2021-06-30 18:23:49 +02:00
x += v.x;
y += v.y;
return this;
}
public Vector sub(Vector v) {
x -= v.x;
y -= v.y;
return this;
}
2021-06-30 18:44:08 +02:00
public Vector mul(double d) {
2021-06-30 18:23:49 +02:00
x *= d;
y *= d;
return this;
}
double dot(Vector v) {
return x*v.x + y*v.y;
}
2021-06-30 18:44:08 +02:00
public double mag() {
2021-06-30 18:23:49 +02:00
return Math.sqrt(dot(this));
}
2021-06-30 18:44:08 +02:00
public Vector norm() {
2021-06-30 18:23:49 +02:00
return mul(1 / mag());
}
Vector setMag(double mag) {
return mul(mag/this.mag());
}
2021-06-30 18:44:08 +02:00
public double dist(Vector v) {
2021-06-30 18:23:49 +02:00
return dist(v.x, v.y);
}
2021-06-30 18:44:08 +02:00
public double dist(double x, double y) {
2021-06-30 18:23:49 +02:00
var dx = this.x - x;
var dy = this.y - y;
return Math.sqrt(dx*dx + dy*dy);
}
2021-06-30 18:44:08 +02:00
public double angle() {
2021-06-30 18:23:49 +02:00
return Math.atan2(y, x);
}
}