forked from shwang/VectorFieldsStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWstPoint.as
79 lines (58 loc) · 1.79 KB
/
WstPoint.as
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
package {
public class WstPoint {
public var x:Number;
public var y:Number;
public function WstPoint(x:Number, y:Number) {
this.x = x;
this.y = y;
}
public function addTo(other:WstPoint):void {
x += other.x;
y += other.y;
}
public function multiplyBy(k:Number):void {
x *= k;
y *= k;
}
public function negative():WstPoint {
return new WstPoint(-x, -y);
}
public function unit(length:Number = 1):WstPoint {
return WstPoint.multiply(this, length/norm());
}
public function norm():Number {
return Math.sqrt(x*x + y*y);
}
public function rotate(degrees:Number):WstPoint {
var cos:Number = Math.cos(degrees*Math.PI/180);
var sin:Number = Math.sin(degrees*Math.PI/180);
return new WstPoint(x*cos - y*sin, x*sin + y*cos);
}
public function equals(o:WstPoint):Boolean {
return o.x == x && o.y == y;
}
public static function add(a:WstPoint, b:WstPoint):WstPoint {
return new WstPoint(a.x + b.x, a.y + b.y);
}
public static function subtract(a:WstPoint, b:WstPoint):WstPoint {
return new WstPoint(a.x - b.x, a.y - b.y);
}
public static function multiply(a:WstPoint, k:Number):WstPoint {
return new WstPoint(a.x*k, a.y*k);
}
public static function dotProduct(a:WstPoint, b:WstPoint):Number {
return a.x * b.x + a.y * b.y;
}
public static function distance(a:WstPoint, b:WstPoint) {
return Math.sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) *(a.y - b.y));
}
public static function get zero():WstPoint {
return new WstPoint(0,0);
}
public static function rand(magnitude:Number, origin:WstPoint = null):WstPoint {
if (origin == null)
origin = WstPoint.zero;
return add(origin, (new WstPoint(magnitude * Math.random(), 0)).rotate(Math.random() * 360));
}
}
}