-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.js
More file actions
106 lines (83 loc) · 2.26 KB
/
vector.js
File metadata and controls
106 lines (83 loc) · 2.26 KB
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
function makeV(x,y) {
return new Vector({x: x, y: y})
}
function randomNum(x) {
return Math.round(Math.random() * x)
}
function randomV(x,y) {
return makeV(randomNum(x),randomNum(y))
}
var Vector = Backbone.Model.extend4000({
defaults: { name: 'vector' },
x: function() { return this.get('x') },
y: function() { return this.get('y') },
isub: function(other){
this.set({ x : this.x() - other.x(),
y : this.y() - other.y() })
return this;
},
sub: function(other){
return makeV( this.x() - other.x(), this.y() - other.y() );
},
iadd: function(other){
this.set({ x : this.x() + other.x(),
y : this.y() + other.y() })
return this;
},
add: function(other){
return makeV( this.x() + other.x(), this.y() + other.y() );
},
imul: function(scalar){
this.set({ x : this.x() * scalar,
y : this.y() * scalar })
return this;
},
mul: function(scalar){
return makeV(this.x() * scalar, this.y() * scalar)
},
idiv: function(scalar){
this.set({ x : this.x() / scalar,
y : this.y() / scalar })
return this;
},
div: function(scalar){
return makeV(this.x() / scalar, this.y() / scalar)
},
normalized: function(){
var x=this.x(), y=this.y();
var length = Math.sqrt(x*x + y*y);
if(length > 0){
return makeV(x/length, y/length);
}
else{
return makeV(0, 0);
}
},
normalize: function(){
var x=this.x(), y=this.y();
var length = Math.sqrt(x*x + y*y);
if(length > 0){
this.set({ x : x/length,
y : y/length })
}
return this;
},
length: function(){
var x = this.x(), y = this.y(); return Math.sqrt(x * x + y * y);
},
distance: function(other){
var x = this.x() - other.x();
var y = this.y() - other.y();
return Math.sqrt(x*x + y*y);
},
copy: function(){
return makeV(this.x(),this.y())
},
zero: function(){
this.set({x:0,y:0})
return this
},
show: function() {
return [ this.x(), this.y() ]
}
})