-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2D.java
55 lines (43 loc) · 1.04 KB
/
Vector2D.java
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
public class Vector2D {
public double x, y;
public Vector2D() {
this.x = 0;
this.y = 0;
}
public Vector2D(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2D add(Vector2D v) {
this.x += v.x;
this.y += v.y;
return this;
}
public Vector2D sub(Vector2D v) {
this.x -= v.x;
this.y -= v.y;
return this;
}
public Vector2D mul(double scale) {
this.x *= scale;
this.y *= scale;
return this;
}
public Vector2D div(double scale) {
this.x /= scale;
this.y /= scale;
return this;
}
public double dot(Vector2D v) {
return this.x * v.x + this.y * v.y;
}
public double mag() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
}
public Vector2D norm() {
return this.div(this.mag());
}
public Vector2D copy() {
return new Vector2D(this.x, this.y);
}
}