-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2D.dart
36 lines (23 loc) · 1.02 KB
/
Vector2D.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
class Vector2D {
num x, y;
Vector2D([num this.x = 0, num this.y = 0]);
void rotate(num deg) {
num rad = deg * Math.PI/180;
num tmp = x*Math.cos(rad) - y*Math.sin(rad);
y = x*Math.sin(rad) + y*Math.cos(rad);
x = tmp;
}
void normalize() {
num length = magnitude;
x /= length;
y /= length;
}
num get magnitude() => Math.sqrt(x*x+y*y);
num cross(Vector2D v) => x*v.y - y*v.x;
Vector2D negate() => new Vector2D(-x, -y);
Vector2D operator+(d) => (d is num) ? new Vector2D(x + d, y + d) : new Vector2D(x + d.x, y + d.y);
Vector2D operator-(d) => (d is num) ? new Vector2D(x - d, y - d) : new Vector2D(x - d.x, y - d.y);
Vector2D operator*(d) => (d is num) ? new Vector2D(x * d, y * d) : new Vector2D(x * d.x, y * d.y);
Vector2D operator/(d) => (d is num) ? new Vector2D(x / d, y / d) : new Vector2D(x / d.x, y / d.y);
String toString() => '(${x.toStringAsFixed(3)}, ${y.toStringAsFixed(3)})';
}