-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvector.coffee
70 lines (49 loc) · 1.22 KB
/
vector.coffee
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
class Vector
x: 0
y: 0
z: 0
constructor: (x,y,z)-> @set x, y, z
add: (v)->
@x += v.x
@y += v.y
@z += v.z
get: -> new Vector @x, @y, @z
cross: (v)->
new Vector @y * v.z - v.y * @z, @z * v.x - v.z * @x, @x * v.y - v.x * @y
div: (v)->
v = new Vector v, v, v if typeof v is 'number'
@x /= v.x
@y /= v.y
@z /= v.z
dist: (v)->
v = new Vector v, v, v if typeof v is 'number'
dx = v.x - @x
dy = v.y - @y
dz = v.z - @z
Math.sqrt dx*dx + dy*dy + dz*dz
dot: (v)-> @x * v.x + @y * v.y + @z * v.z
heading2D: -> -Math.atan2 -@y, @x
limit: (high)->
if @mag() > high
@normalize()
@mult(high)
mag: -> Math.sqrt @x * @x + @y * @y + @z * @z
mult: (v)->
@x *= v.x
@y *= v.y
@z *= v.z
normalize: ->
m = @mag()
@div m if m > 0
set: (x, y, z)-> [ @x, @y, @z ] = [ x, y, z ]
sub: (v)->
@x -= v.z
@y -= v.y
@z -= v.z
toArray: -> [@x, @y, @z]
toString: -> "x:@x, y:@y, z:@z"
Vector.dist = (v1, v2)-> v1.dist v2
Vector.dot = (v1, v2)-> v1.dot(v2)
Vector.cross = (v1, v2)-> v1.cross(v2)
Vector.angleBetween = (v1, v2)-> Math.acos v1.dot(v2) / (v1.mag() * v2.mag())
exports.Vector = Vector if exports?