-
Notifications
You must be signed in to change notification settings - Fork 0
/
DRVector.as
158 lines (124 loc) · 3.5 KB
/
DRVector.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class DRVector {
/************
* *
* Constants *
* *
************/
static var CLOCKWISE = 1000;
static var COUNTERCLOCKWISE = 2000;
/*********************
* *
* Instance Variables *
* *
*********************/
var _x:Number;
var _y:Number;
/**********************
* *
* Vector Constructors *
* *
**********************/
function DRVector(x:Number, y:Number) {
this._x = x;
this._y = y;
}
static function mouse():DRVector {
return new DRVector(_xmouse, _ymouse);
}
static function zero():DRVector {
return new DRVector(0.0, 0.0);
}
static function unitX():DRVector {
return new DRVector(1.0, 0.0);
}
static function unitY():DRVector {
return new DRVector(0.0, 1.0);
}
static function inDirection(degrees:Number, scale:Number):DRVector {
var factor = scale ? scale : 1.0;
var rads = degrees * Math.PI / 180.0;
return new DRVector( factor * Math.cos(rads), factor * Math.sin(rads) );
}
/*******************
* *
* Atomic Functions *
* *
*******************/
function x():Number {
return _x;
}
function y():Number {
return _y;
}
function norm():Number {
return Math.sqrt(x() * x() + y() * y());
}
function normal():DRVector {
if (norm() == 0.0)
return DRVector.zero();
else
return new DRVector(x() / norm(), y() / norm());
}
/*************
* *
* Operations *
* *
*************/
function inverse():DRVector {
return new DRVector( -x(), -y() );
}
function plus(force:DRVector):DRVector {
return new DRVector(x() + force.x(), y() + force.y());
}
function plus_bang(force:DRVector):DRVector {
_x += force.x();
_y += force.y();
return this;
}
function minus(force:DRVector):DRVector {
return new DRVector(x() - force.x(), y() - force.y());
}
function scale(scalar:Number):DRVector {
return new DRVector(x() * scalar, y() * scalar);
}
function scale_bang(scalar:Number):DRVector {
_x *= scalar;
_y *= scalar;
return this;
}
/******************
* *
* Angle Functions *
* *
******************/
function degrees():Number {
var degs = radians() * 180.0 / Math.PI;
while (degs >= 360.0) { degs -= 360.0 }
while (degs < 0.0) { degs += 360.0 }
return degs;
}
function radians():Number {
return Math.atan2(y(), x());
}
/*******************
* *
* Helper Functions *
* *
*******************/
function directionTo(force:DRVector):Number {
var to = force.degrees();
if (to < degrees()) { to += 360.0 }
if (to - degrees() < 180.0) {
return DRVector.COUNTERCLOCKWISE;
} else {
return DRVector.CLOCKWISE;
}
}
function cropNorm(max:Number):DRVector {
var n = norm();
if (n > max)
return scale_bang( max / n );
else
return this;
}
}