summaryrefslogtreecommitdiff
path: root/assets/javascripts/math/vec2.js
blob: 75fd8591cdd32c813b3913cdcaa4ecc082fe671b (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
(function(){
	var point
	if ('window' in this) {
		point = window.point
	}
	else {
		point = require('./point')
	}

	var vec2 = function (x0,y0, x1,y1){
		if (x0 instanceof point) {
			this.a = x0
			this.b = y0
		}
		else if (x1 === undefined) {
			this.a = new point(x0,y0)
			this.b = new point(x0,y0)
		}
		else {
			this.a = new point(x0,y0)
			this.b = new point(x1,y1)
		}
	}
	vec2.prototype.clone = function(){
		return new vec2( this.a.clone(), this.b.clone() )
	}

	vec2.prototype.delta = function(){ return new point( this.width(), this.height() ) }
	vec2.prototype.width = function(){ return this.b.x - this.a.x }
	vec2.prototype.height = function(){ return this.b.y - this.a.y }


	vec2.prototype.toString = function(){
		var s = "[" + this.a.toString() + " " + this.b.toString() + "] "
		return s
	}
	vec2.prototype.exactString = function(){
		var s = "[" + this.a.exactString() + " " + this.b.exactString() + "] "
		return s
	}
	
	vec2.prototype.serialize = function(){
		return { a: this.a.serialize(), b: this.b.serialize() }
	}
	vec2.prototype.quantize = function(n){
		this.a.quantize(n)
		this.b.quantize(n)
		return this
	}
	
	if ('window' in this) {
		window.vec2 = vec2
	}
	else {
		module.exports = vec2
	}

})()