/
Vector2D.js
174 lines (155 loc) · 3.1 KB
/
Vector2D.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
2D Vector Library
@author Bart Van Beurden
@date 6 oktober 2014
Memory layout:
[0, 1] == [a, b]
**/
// dependencies
var Vector2D = module.exports = {};
/**
Create 2D Vector
**/
Vector2D.create = Float32Array
? function() { return new Float32Array(2); }
: function() { return [0, 0]; }
/**
Set vector quanta
@param {out}
@param {v} Vector to copy
**/
Vector2D.setVector = function(out, v) {
out[0] = v[0];
out[1] = v[1];
return out;
};
/**
Set vector quanta
@param {out}
@param {x} X scalar value
@param {y} Y scalar value
**/
Vector2D.setScalar = function(out, x, y) {
out[0] = x;
out[1] = y;
return out;
};
/**
Add 2 vectors
@param {out}
@param {v} Left vector
@param {w} Right vector
**/
Vector2D.add = function(out, v, w) {
out[0] = v[0] + w[0];
out[1] = v[1] + w[1];
return out;
};
/**
Subtract 2 vectors
@param {out}
@param {v} Left vector
@param {w} Right vector
**/
Vector2D.subtract = function(out, v, w) {
out[0] = v[0] - w[0];
out[1] = v[1] - w[1];
return out;
};
/**
Multiply vector with scalars
@param {out}
@param {v} Vector to multiply
@param {c} Y scalar to multiply with
**/
Vector2D.multiplyScalar = function(out, v, c) {
out[0] = v[0] * c;
out[1] = v[1] * c;
return out;
};
/**
Multiply vectors (dot product)
@param {v} Left vector
@param {w} Right vector
**/
Vector2D.multiplyVector =
Vector2D.dot = function(v, w) {
return v[0] * w[0] + v[1] * w[1];
};
/**
Calculate square length of vector
@param {v}
**/
Vector2D.length2 = function(v) {
return v[0] * v[0] + v[1] * v[1];
};
/**
Calculate length of vector
@param {v}
**/
Vector2D.length = function(v) {
return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
};
/**
Calculate square distance between vectors
@param {v} Left vector
@param {w} Right vector
**/
Vector2D.distance2 = function(v, w) {
var x = w[0] - v[0];
var y = w[1] - v[1];
return x * x + y * y;
};
/**
Calculate distance between vectors
@param {v} Left vector
@param {w} Right vector
**/
Vector2D.distance = function(v, w) {
var x = w[0] - v[0];
var y = w[1] - v[1];
return Math.sqrt(x * x + y * y);
};
/**
Normalize vector
@param {out}
@param {v}
**/
Vector2D.normalize = function(out, v) {
var len = 1 / Vector2D.length(v);
out[0] = v[0] * len;
out[1] = v[1] * len;
return out;
};
/**
Offset towards angle
@param {out}
@param {vertex} Vertex to offset
@param {distance} Distance to offset
@param {angle} Angle to offset towards
**/
Vector2D.offsetTowardsAngle = function(out, vertex, distance, angle) {
var cos = Math.cos(angle);
var sin = Math.sin(angle);
out[0] = vertex[0] + distance * cos;
out[1] = vertex[1] + distance * sin;
return out;
};
/**
Offset towards point
@param {out}
@param {vertex} Vertex to offset
@param {distance} Distance to offset
@param {target} Vertex to offset towards
**/
Vector2D.offsetTowardsPoint =
Vector2D.offsetTowardsVertex = function(out, vertex, distance, target) {
var x = target[0] - vertex[0];
var y = target[1] - vertex[1];
var len = Math.sqrt(x * x + y * y);
var cos = x / len;
var sin = y / len;
out[0] = vertex[0] + distance * cos;
out[1] = vertex[1] + distance * sin;
return out;
};