blob: 8bc84102db759b131987430765fcde70ea517489 (
plain)
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
|
function vec2(a,b){
this.a = a
this.b = b
}
vec2.prototype.magnitude = function(){
return this.b-this.a
}
vec2.prototype.length = function(){
return abs(this.b-this.a)
}
vec2.prototype.clone = function(){
return new vec2(this.a, this.b)
}
vec2.prototype.abs = function(){
if (this.b < this.a) {
this.a = this.a ^ this.b
this.b = this.a ^ this.b
this.a = this.a ^ this.b
}
return this
}
vec2.prototype.midpoint = function(){
return lerp(0.5, this.a, this.b)
}
vec2.prototype.add = function(n){
this.a += n
this.b += n
}
vec2.prototype.sub = function(n){
this.a -= n
this.b -= n
}
vec2.prototype.mul = function(n){
this.a *= n
this.b *= n
}
vec2.prototype.div = function(n){
this.a /= n
this.b /= n
}
vec2.prototype.contains = function(n){
return this.a <= n && n <= this.b
}
// assumes (vec2)v falls to the right of (vec2)this
vec2.prototype.intersects = function(v){
return (this.b > v.a) || (v.b > this.a)
}
vec2.prototype.union = function(v){
if (this.intersects(v)) {
return new vec2( min(this.a,v.a), max(this.b, v.b) )
}
}
vec2.prototype.intersection = function(v){
if (this.intersects(v)) {
return new vec2( max(this.a,v.a), min(this.b, v.b) )
}
}
vec2.prototype.toString = function(){
return "[" + this.a + " " + this.b + "]"
}
vec2.prototype.quantize = function(n){
n = n || 10
this.a = quantize(this.a, n)
this.b = quantize(this.b, n)
}
|