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 123 124 125 126 127 128 129 130 131 132 | 1 1 108 108 1 25 1 1 3 3 3 7 1 1 5 4 15 3 3 3 4 4 4 8 7 7 7 7 1 1 1 1 1 1 3 3 3 4 4 4 1 1 1 1 1 1 3 3 3 3 5 5 5 1 3 1 2 1 1 | 'use strict'; module.exports = Point; function Point(x, y) { this.x = x; this.y = y; } Point.prototype = { clone: function() { return new Point(this.x, this.y); }, add: function(p) { return this.clone()._add(p); }, sub: function(p) { return this.clone()._sub(p); }, mult: function(k) { return this.clone()._mult(k); }, div: function(k) { return this.clone()._div(k); }, rotate: function(a) { return this.clone()._rotate(a); }, matMult: function(m) { return this.clone()._matMult(m); }, unit: function() { return this.clone()._unit(); }, perp: function() { return this.clone()._perp(); }, round: function() { return this.clone()._round(); }, mag: function() { return Math.sqrt(this.x * this.x + this.y * this.y); }, equals: function(p) { return this.x === p.x && this.y === p.y; }, dist: function(p) { return Math.sqrt(this.distSqr(p)); }, distSqr: function(p) { var dx = p.x - this.x, dy = p.y - this.y; return dx * dx + dy * dy; }, angle: function() { return Math.atan2(this.y, this.x); }, angleTo: function(b) { return Math.atan2(this.y - b.y, this.x - b.x); }, angleWith: function(b) { return this.angleWithSep(b.x, b.y); }, // Find the angle of the two vectors, solving the formula for the cross product a x b = |a||b|sin(θ) for θ. angleWithSep: function(x, y) { return Math.atan2( this.x * y - this.y * x, this.x * x + this.y * y); }, _matMult: function(m) { var x = m[0] * this.x + m[1] * this.y, y = m[2] * this.x + m[3] * this.y; this.x = x; this.y = y; return this; }, _add: function(p) { this.x += p.x; this.y += p.y; return this; }, _sub: function(p) { this.x -= p.x; this.y -= p.y; return this; }, _mult: function(k) { this.x *= k; this.y *= k; return this; }, _div: function(k) { this.x /= k; this.y /= k; return this; }, _unit: function() { this._div(this.mag()); return this; }, _perp: function() { var y = this.y; this.y = this.x; this.x = -y; return this; }, _rotate: function(angle) { var cos = Math.cos(angle), sin = Math.sin(angle), x = cos * this.x - sin * this.y, y = sin * this.x + cos * this.y; this.x = x; this.y = y; return this; }, _round: function() { this.x = Math.round(this.x); this.y = Math.round(this.y); return this; } }; // constructs Point from an array if necessary Point.convert = function (a) { if (a instanceof Point) { return a; } if (Array.isArray(a)) { return new Point(a[0], a[1]); } return a; }; |