-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.dart
83 lines (66 loc) · 1.38 KB
/
vector.dart
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
import 'dart:math';
class Vector2 {
double x, y;
Vector2(double x, double y) {
this.x = x;
this.y = y;
}
}
class Vector3 {
double x, y, z;
Vector3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
Vector3.zero() {
this.x = 0;
this.y = 0;
this.z = 0;
}
String toString() {
return "Vector3(${x}, ${y}, ${z})";
}
Vector3 setTo(Vector3 vec) {
x = vec.x;
y = vec.y;
z = vec.z;
return this;
}
double norm() {
return sqrt(x * x + y * y + z * z);
}
Vector3 normalise({double l = 1.0}) {
double t = l / norm();
x *= t;
y *= t;
z *= t;
return this;
}
Vector3 timesDouble(double d) {
x *= d;
y *= d;
z *= d;
return this;
}
Vector3 operator -(Vector3 vec) {
return Vector3(x - vec.x, y - vec.y, z - vec.z);
}
Vector3 operator +(Vector3 vec) {
return Vector3(x + vec.x, y + vec.y, z + vec.z);
}
Vector3 operator *(Vector3 vec) {
return Vector3(x * vec.x, y * vec.y, z * vec.z);
}
Vector3 operator ^(double d) {
return Vector3(x * d, y * d, z * d);
}
// We can override according to return type in C++, not sure how to do that in Dart, so I'm using a different symbol
double operator &(Vector3 dir) {
double result = 0.0;
result += x * dir.x;
result += y * dir.y;
result += z * dir.z;
return result;
}
}