-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2d.java
More file actions
84 lines (67 loc) · 1.12 KB
/
Vector2d.java
File metadata and controls
84 lines (67 loc) · 1.12 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
public class Vector2d {
public double x, y;
Vector2d(double x, double y) {
this.x = x;
this.y = y;
}
Vector2d() {
this.x = 0.0;
this.y = 0.0;
}
public void set(double x, double y) {
this.x = x;
this.y = y;
}
public void set(Vector2d o) {
this.x = o.x;
this.y = o.y;
}
public double magSqr() {
return x*x + y*y;
}
public double mag() {
return Math.sqrt(x*x + y*y);
}
public void normalize() {
double len = mag();
x /= len;
y /= len;
}
public void mult(double m) {
x *= m;
y *= m;
}
public void setMag(double m) {
normalize();
mult(m);
}
public void limit(double maxMag) {
double m = magSqr();
if (m > maxMag*maxMag) {
setMag(maxMag);
}
}
public double angTo(Vector2d o) {
return Math.atan2(y - o.y , x - o.x);
}
public void add(Vector2d o) {
x += o.x;
y += o.y;
}
public void add(double x, double y) {
this.x += x;
this.y += y;
}
public void sub(Vector2d o) {
x -= o.x;
y -= o.y;
}
public void sub(double x, double y) {
this.x -= x;
this.y -= y;
}
@Override
public String toString() {
return "" + x + "," + y;
}
}