-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.java
More file actions
91 lines (70 loc) · 2.4 KB
/
Copy pathVector2.java
File metadata and controls
91 lines (70 loc) · 2.4 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
85
86
87
88
89
90
91
public class Vector2 {
public static final Vector2 ZERO = new Vector2(0,0);
public double x;
public double y;
public Vector2() {
this.x = 0;
this.y = 0;
}
public Vector2(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2(Vector2 that) {
this.x = that.x;
this.y = that.y;
}
@Override
public String toString() {
return "<"+String.valueOf(x)+","+String.valueOf(y)+">";
}
public static Vector2 add(Vector2 v, Vector2 u) {
return new Vector2(v.x+u.x, v.y+u.y);
}
public static Vector2 minus(Vector2 v, Vector2 u) {
return new Vector2(v.x-u.x, v.y-u.y);
}
public static Vector2 hadamard(Vector2 v, Vector2 u) {
return new Vector2(v.x*u.x, v.y*u.y);
}
public static double dot(Vector2 v, Vector2 u) {
return v.x*u.x+v.y*u.y;
}
public static double cross(Vector2 v, Vector2 u) {
return v.x*u.y-v.y*u.x;
}
public static double comp(Vector2 v, Vector2 u) {
return v.dot(u) / v.magnitude();
}
public static Vector2 proj(Vector2 v, Vector2 u) {
return v.normalize().scale(v.comp(u));
}
public Vector2 add(Vector2 that) {return Vector2.add(this, that);}
public Vector2 minus(Vector2 that) {return Vector2.minus(this, that);}
public Vector2 hadamard(Vector2 that) {return Vector2.hadamard(this, that);}
public double dot(Vector2 that) {return Vector2.dot(this, that);}
public double cross(Vector2 that) {return Vector2.cross(this, that);}
public double comp(Vector2 that) {return Vector2.comp(this, that);}
public Vector2 proj(Vector2 that) {return Vector2.proj(this, that);}
public static Vector2 lerp(Vector2 v, Vector2 u, double t) {
return Vector2.add(v.scale(1-t), u.scale(t));
}
public Vector2 scale(double k) {
return new Vector2(this.x*k, this.y*k);
}
public Vector2 rotate(double theta) {
double magnitude = Math.hypot(x, y);
theta += Math.atan2(y, x);
return new Vector2(magnitude * Math.cos(theta), magnitude * Math.sin(theta));
}
public double magnitude() {
return Math.hypot(x, y);
}
public Vector2 normalize() {
double theta = Math.atan2(y, x);
return new Vector2(Math.cos(theta), Math.sin(theta));
}
public Vector2 normal() {
return new Vector2(y, -x);
}
}