summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/vec2.js
blob: 9233aece7f65021eb0b570a5f35f1b9c18537983 (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
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
(function(){
	var vec2 = function (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.dist = function(){
		return dist(0,this.a,0,this.b)
	}
	vec2.prototype.clone = function(){
		return new vec2(this.a, this.b)
	}
	vec2.prototype.abs = function(){
		if (this.b < this.a) {
			this.invert()
		}
		return this
	}
	vec2.prototype.invert = function(){
		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.eq = function(v){
		return this.a == v.a && this.b == v.b
	}
	vec2.prototype.add = function(n){
		this.a += n
		this.b += n
		return this
	}
	vec2.prototype.sub = function(n){
		this.a -= n
		this.b -= n
		return this
	}
	vec2.prototype.mul = function(n){
		this.a *= n
		this.b *= n
		return this
	}
	vec2.prototype.div = function(n){
		this.a /= n
		this.b /= n
		return this
	}
	vec2.prototype.setPosition = function(n){
		var len = this.length()
		this.a = n
		this.b = n + len
	}
	vec2.prototype.setLength = function(n){
		this.b = this.a + n
	}
	vec2.prototype.normalize = function(){
		var dim = max(this.a, this.b)
		this.a = this.a/dim
		this.b = this.b/dim
		return this
	}
	vec2.prototype.contains = function(n){
		return this.a <= n && n <= this.b
	}
	vec2.prototype.containsDisc = function(n,r){
		return this.a <= n-r && n+r <= this.b
	}
	vec2.prototype.clamp = function(n){
		return clamp(n, this.a, this.b)
	}
	vec2.prototype.clampDisc = function(n,r){
		return clamp(n, this.a+r, this.b-r)
	}
	vec2.prototype.intersects = function(v){
		if (this.a < v.a) {
			return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b)
		}
		else if (this.a == v.a) {
			return true
		}
		else if (this.a > v.a) {
			return (this.a < v.b && v.b <= this.b) || (v.a < this.b && this.b <= v.b)
		}
	}
	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.serialize = 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)
	}
	
	if ('window' in this) {
		window.vec2 = vec2
	}
	else if ('module' in this) {
		module.exports = vec2
	}
	
})()