Created
June 5, 2017 13:07
-
-
Save 2bbb/fe95222bca599f785f3e4eea6ee1b391 to your computer and use it in GitHub Desktop.
vec2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var bbb = {}; | |
| bbb.extend = function(dst, src) { | |
| for(var key in src) if(src.hasOwnProperty(key)) dst[key] = src[key]; | |
| return dst; | |
| }; | |
| bbb.math = {}; | |
| var vec2 = (function() { | |
| function vec2(v) { | |
| bbb.extend(this, { | |
| x: v.x, | |
| y: v.y, | |
| }); | |
| }; | |
| function is_vec2(v) { return v instanceof vec2; }; | |
| bbb.extend(vec2, { | |
| isVec2: is_vec2, | |
| add: function(v, w) { return new vec2(v.x + w.x, v.y + w.y); }, | |
| sub: function(v, w) { return new vec2(v.x - w.x, v.y - w.y); }, | |
| mul: function(v, s) { return new vec2(v.x * s, v.y * s); }, | |
| div: function(v, s) { return new vec2(v.x / s, v.y / s); }, | |
| eq: function(v, w) { return v.x == w.x && v.y == w.y; }, | |
| neq: function(v, w) { return v.x != w.x || v.y != w.y; }, | |
| dot: function(v, w) { return v.x * w.x + v.y * w.y; }, | |
| cross: function(w, w) { return v.x * w.y - v.y * w.x; }, | |
| abs: function(v) { return v.x * v.x + v.y * v.y; }, | |
| length: function(v) { return Math.sqrt(v.x * v.x + v.y * v.y); }, | |
| lerp: function(v, w, t) { var s = 1.0 - t; return new vec2(s * v.x + t * w.x, s * v.y + t * w.y); }, | |
| create: function(x, y) { return new vec2(is_vec2(x) ? x : {x: x, y: y}); }, | |
| cast: function(x, y) { return is_vec2(x) ? x : {x: x, y: y}; }, | |
| }); | |
| bbb.extend(vec2.prototype, { | |
| clone: function() { return new vec2(this); }, | |
| eq: function(v) { return this.x == v.x && this.y == v.y; }, | |
| translate: function(v) { | |
| this.x += v.x; | |
| this.y += v.y; | |
| return this; | |
| }, | |
| translated: function(v) { return this.clone().translate(this.x + v.x, this.y + v.y); }, | |
| scale: function(sx, sy) { | |
| this.x *= sx; | |
| this.y *= sy === undefined ? sx : sy; | |
| return this; | |
| }, | |
| scaled: function(sx, sy) { return this.clone().scale(sx, sy); }, | |
| reflct: function() { return this.scale(-1.0); }, | |
| reflected: function() { return this.scaled(-1.0); }, | |
| rotate: function(rad) { | |
| var c = Math.cos(rad), | |
| s = Math.sin(rad), | |
| x = c * this.x + s * this.y, | |
| y = -s * this.y + c * this.x; | |
| this.x = x; | |
| this.y = y; | |
| return this; | |
| }, | |
| rotated: function(rad) { return this.clone().rotate(rad); }, | |
| rotateTo: function(rad) { | |
| var len = this.length(); | |
| this.x = Math.cos(rad) * len; | |
| this.y = Math.sin(rad) * len; | |
| return this; | |
| }, | |
| rotatedTo: function(rad) { this.clone().rotateTo(rad); }, | |
| length: function() { return Math.sqrt(this.x * this.x + this.y + this.y); }, | |
| interpolate: function(v, t) { | |
| var s = 1.0 - t; | |
| this.x = s * this.x + t * v.x; | |
| this.y = s * this.y + t * v.y; | |
| return this; | |
| }, | |
| interpolated: function(v, t) { return this.clone().interpolated(v, t); }, | |
| middle: function(v) { return this.interpolate(v, 0.5); }, | |
| middled: function(v) { return this.clone().interpolate(v, 0.5); }, | |
| normalize: function(v) { | |
| var l = 1.0 / this.length(); | |
| this.x *= l; | |
| this.y *= l; | |
| return this; | |
| }, | |
| normalized: function(v) { this.clone().normalize(v); }, | |
| distance: function(v) { return v.reflected().translate(v).length(); }, | |
| dot: function(v) { return this.x * v.x + this.y * v.y; }, | |
| cross: function(v) { return this.x * v.y + this.y * v.x; }, | |
| angle: function() { return Math.atan(this.y, this.x); }, | |
| angleTo: function(v) { return v.reflected().translate(this).angle(); }, | |
| toString: function() { return "(" + this.x + ", " + this.y + ")"; }, | |
| }); | |
| return vec2; | |
| })(); | |
| bbb.extend(bbb.math, { | |
| vec2: vec2 | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment