A collection of games I worked on in high school.
1
2//input: an object with x and y properties
3//output: a normalized vector
4var normal = function(vector) {
5 var length = Math.pow(Math.pow(vector.x, 2) + Math.pow(vector.y, 2), 0.5);
6 if (length === 0) {
7 return {x:0, y:0};
8 } else {
9 return {x:(vector.x / length), y:(vector.y / length)};
10 }
11};
12
13//input: x and y
14//output: the angle in radians
15var angle = function(x, y) {
16 var theta = Math.atan(y / x);
17 if (x < 0) {
18 theta += Math.PI;
19 } else if (y < 0) {
20 theta += 2*Math.PI;
21 }
22 return theta;
23};
24
25//remove all instances of input value from array
26Array.prototype.cleanse = function (value) {
27 for (var i = 0; i < this.length - 1;) {
28 if (this[i] === value) {
29 this.splice(i, 1);
30 } else {
31 ++i;
32 }
33 }
34};
35
36//creates the Array.isArray(obj) if it's not supported
37if (!Array.isArray) {
38 Array.isArray = function (vArg) {
39 return Object.prototype.toString.call(vArg) === "[object Array]";
40 };
41}
42
43Array.prototype.removegiven = function (key, values) {
44 if (Array.isArray(values)) {
45 for (var j = 0; j <= values.length - 1; j++) {
46 for (var k = 0; k <= this.length - 1; k++) {
47 if (this[k][key] === values[j]) {
48 this.splice(k, 1);
49 }
50 }
51 }
52 }
53 else {
54 for (var l = 0; l <= this.length - 1;) {
55 if (this[l][key] === values) {
56 this.splice(l, 1);
57 }
58 else l++;
59 }
60 }
61}