summaryrefslogtreecommitdiff
path: root/public/js/util.js
blob: dbb37a771a23a3cb8e25425e961c34390e6de109 (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
function trim (s) {
	return s.replace(/^\s+/, "").replace(/\s+$/, "");
}
function strip (s) {
	return trim(s).replace(/\W+/, "");
}
function sanitize (s) {
	return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;");
}
function scrollToBottom (el) {
	try { $(el).scrollTop( $(el)[0].scrollHeight ) }
	catch (err) { }
}
var Trig = {
  distanceBetween2Points: function ( point1, point2 ) {
    var dx = point2.x - point1.x;
    var dy = point2.y - point1.y;
    return Math.sqrt( Math.pow( dx, 2 ) + Math.pow( dy, 2 ) );
  },
  angleBetween2Points: function ( point1, point2 ) {
    var dx = point2.x - point1.x;
    var dy = point2.y - point1.y;
    return Math.atan2( dx, dy );
  },
  magnitude: function (point) {
    return Math.sqrt( Math.pow( point.x, 2 ) + Math.pow( point.y, 2 ) );
  }
}

function fib (f) { for (var i = 1, n = 1; n < 200; i++, n += i) f(n); }
function rand(x) { return Math.random() * x; }
function randInt(x) { return Math.floor(Math.random() * x); }
function clamp(x,a,b) { return Math.min(Math.max(x,a),b); }