summaryrefslogtreecommitdiff
path: root/assets/javascripts/math/point.js
blob: bb2dc4f2c9a5cf896d0ef97c9eeaa02eaf95cc45 (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
(function(){
	function clamp(n,a,b){ return n<a?a:n<b?n:b }

	var point = function (x, y){
		this.x = x
		this.y = y
	}

	point.prototype.distanceTo = function(p){
	  return Math.sqrt(this.dot(p))
	}
	point.prototype.dot = function(p){
	  return Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2)
	}
	point.prototype.clone = function(){
	  return new point(this.x, this.y)
	}

	point.prototype.toString = function(){
		return "[" + round(this.x) + " " + round(this.y) + "]"
	}
	point.prototype.exactString = function(){
		return "[" + this.x + " " + this.y + "]"
	}
	point.prototype.serialize = function(){
		return [ round(this.x), round(this.y) ]
	}
	point.prototype.deserialize = function(data){
		this.x = data[0]
		this.y = data[1]
	}
	point.prototype.quantize = function(n){
		n = n || 10
		this.x = quantize(this.x, n)
		this.y = quantize(this.y, n)
	}
	
	if ('window' in this) {
		window.point = point
	}
	else {
		module.exports = point
	}
})()