summaryrefslogtreecommitdiff
path: root/client/assets/javascripts/rectangles/models/vec2.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-06-03 16:24:10 -0400
committerJules Laplace <jules@okfoc.us>2014-06-03 16:24:28 -0400
commit607f69c67a5b4dc72d2754192e3cdf67d0ad11d0 (patch)
tree6556e7922c5bedb274bb1650e5dd100643a7895d /client/assets/javascripts/rectangles/models/vec2.js
parentd31259291d807c851de4396921e0c26b6dd8dce2 (diff)
partitioning client and serveR
Diffstat (limited to 'client/assets/javascripts/rectangles/models/vec2.js')
-rw-r--r--client/assets/javascripts/rectangles/models/vec2.js99
1 files changed, 99 insertions, 0 deletions
diff --git a/client/assets/javascripts/rectangles/models/vec2.js b/client/assets/javascripts/rectangles/models/vec2.js
new file mode 100644
index 0000000..9b0447c
--- /dev/null
+++ b/client/assets/javascripts/rectangles/models/vec2.js
@@ -0,0 +1,99 @@
+function vec2(a,b){
+ this.a = a
+ this.b = b
+}
+vec2.prototype.magnitude = function(){
+ return this.b-this.a
+}
+vec2.prototype.length = function(){
+ return abs(this.b-this.a)
+}
+vec2.prototype.clone = function(){
+ return new vec2(this.a, this.b)
+}
+vec2.prototype.abs = function(){
+ if (this.b < this.a) {
+ this.invert()
+ }
+ return this
+}
+vec2.prototype.invert = function(){
+ this.a = this.a ^ this.b
+ this.b = this.a ^ this.b
+ this.a = this.a ^ this.b
+ return this
+}
+vec2.prototype.midpoint = function(){
+ return lerp(0.5, this.a, this.b)
+}
+vec2.prototype.eq = function(v){
+ return this.a == v.a && this.b == v.b
+}
+vec2.prototype.add = function(n){
+ this.a += n
+ this.b += n
+ return this
+}
+vec2.prototype.sub = function(n){
+ this.a -= n
+ this.b -= n
+ return this
+}
+vec2.prototype.mul = function(n){
+ this.a *= n
+ this.b *= n
+ return this
+}
+vec2.prototype.div = function(n){
+ this.a /= n
+ this.b /= n
+ return this
+}
+vec2.prototype.normalize = function(){
+ var dim = max(this.a, this.b)
+ this.a = this.a/dim
+ this.b = this.b/dim
+ return this
+}
+vec2.prototype.contains = function(n){
+ return this.a <= n && n <= this.b
+}
+vec2.prototype.containsDisc = function(n,r){
+ return this.a <= n-r && n+r <= this.b
+}
+vec2.prototype.clamp = function(n){
+ return clamp(n, this.a, this.b)
+}
+vec2.prototype.clampDisc = function(n,r){
+ return clamp(n, this.a+r, this.b-r)
+}
+vec2.prototype.intersects = function(v){
+ if (this.a < v.a) {
+ return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b)
+ }
+ else if (this.a == v.a) {
+ return true
+ }
+ else if (this.a > v.a) {
+ return (this.a < v.b && v.b <= this.b) || (v.a < this.b && this.b <= v.b)
+ }
+}
+vec2.prototype.union = function(v){
+ if (this.intersects(v)) {
+ return new vec2( min(this.a,v.a), max(this.b, v.b) )
+ }
+}
+vec2.prototype.intersection = function(v){
+ if (this.intersects(v)) {
+ return new vec2( max(this.a,v.a), min(this.b, v.b) )
+ }
+}
+vec2.prototype.toString = function(){
+ return "[" + ~~this.a + " " + ~~this.b + "]"
+}
+vec2.prototype.quantize = function(n){
+ n = n || 10
+ this.a = quantize(this.a, n)
+ this.b = quantize(this.b, n)
+}
+