-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector2.pde
49 lines (48 loc) · 1.03 KB
/
Vector2.pde
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
class Vector2 {
float x, y, d;
Vector2(float x, float y) {
this.x = x;
this.y = y;
d = sqrt(x*x + y*y);
}
Vector2() {
this.x = 0;
this.y = 0;
d = 0;
}
Vector2 normalized() {
return new Vector2(x/d, y/d);
}
Vector2 subtract(Vector2 b) {
return new Vector2(x - b.x, y - b.y);
}
Vector2 add(Vector2 b) {
return new Vector2(x + b.x, y + b.y);
}
Vector2 multiply(float b) {
return new Vector2(x*b, y*b);
}
Vector2 divide(float b) {
return new Vector2(x/b, y/b);
}
float dot(Vector2 b) {
return x*b.x + y*b.y;
}
Vector2 reflect(Vector2 wallN) { // input: wallNormal
wallN = wallN.normalized();
//Vector2 wallN = new Vector2(-wall.y, wall.x);
return this.subtract(wallN.multiply(2*wallN.dot(this)));
}
Vector2 project(Vector2 b) {
return b.multiply(dot(b)/(b.d*b.d));
}
Vector2 copy() {
return new Vector2(x, y);
}
String toString() {
return "<" + x + "," + y + ">";
}
}
Vector2 pt(float x, float y) {
return new Vector2(x, y);
}