vector3

Benchmark created by ponysteps on


Setup

var PVector = (function() {
        function PVector(x, y, z) {
          this.x = x || 0;
          this.y = y || 0;
          this.z = z || 0;
        }
    
        PVector.dist = function(v1, v2) {
          return v1.dist(v2);
        };
    
        PVector.dot = function(v1, v2) {
          return v1.dot(v2);
        };
    
        PVector.cross = function(v1, v2) {
          return v1.cross(v2);
        };
    
        PVector.angleBetween = function(v1, v2) {
          return Math.acos(v1.dot(v2) / (v1.mag() * v2.mag()));
        };
    
        // Common vector operations for PVector
        PVector.prototype = {
          set: function(v, y, z) {
            if (arguments.length === 1) {
              this.set(v.x || v[0] || 0,
                       v.y || v[1] || 0,
                       v.z || v[2] || 0);
            } else {
              this.x = v;
              this.y = y;
              this.z = z;
            }
          },
          get: function() {
            return new PVector(this.x, this.y, this.z);
          },
          mag: function() {
            var x = this.x,
                y = this.y,
                z = this.z;
            return Math.sqrt(x * x + y * y + z * z);
          },
          add: function(v, y, z) {
            if (arguments.length === 1) {
              this.x += v.x;
              this.y += v.y;
              this.z += v.z;
            } else {
              this.x += v;
              this.y += y;
              this.z += z;
            }
          },
          sub: function(v, y, z) {
            if (arguments.length === 1) {
              this.x -= v.x;
              this.y -= v.y;
              this.z -= v.z;
            } else {
              this.x -= v;
              this.y -= y;
              this.z -= z;
            }
          },
          mult: function(v) {
            if (typeof v === 'number') {
              this.x *= v;
              this.y *= v;
              this.z *= v;
            } else {
              this.x *= v.x;
              this.y *= v.y;
              this.z *= v.z;
            }
          },
          div: function(v) {
            if (typeof v === 'number') {
              this.x /= v;
              this.y /= v;
              this.z /= v;
            } else {
              this.x /= v.x;
              this.y /= v.y;
              this.z /= v.z;
            }
          },
          dist: function(v) {
            var dx = this.x - v.x,
                dy = this.y - v.y,
                dz = this.z - v.z;
            return Math.sqrt(dx * dx + dy * dy + dz * dz);
          },
          dot: function(v, y, z) {
            if (arguments.length === 1) {
              return (this.x * v.x + this.y * v.y + this.z * v.z);
            }
            return (this.x * v + this.y * y + this.z * z);
          },
          cross: function(v) {
            var x = this.x,
                y = this.y,
                z = this.z;
            return new PVector(y * v.z - v.y * z,
                               z * v.x - v.z * x,
                               x * v.y - v.x * y);
          },
          normalize: function() {
            var m = this.mag();
            if (m > 0) {
              this.div(m);
            }
          },
          limit: function(high) {
            if (this.mag() > high) {
              this.normalize();
              this.mult(high);
            }
          },
          heading2D: function() {
            return (-Math.atan2(-this.y, this.x));
          },
          toString: function() {
            return "[" + this.x + ", " + this.y + ", " + this.z + "]";
          },
          array: function() {
            return [this.x, this.y, this.z];
          }
        };
    
        function createPVectorMethod(method) {
          return function(v1, v2) {
            var v = v1.get();
            v[method](v2);
            return v;
          };
        }
    
        for (var method in PVector.prototype) {
          if (PVector.prototype.hasOwnProperty(method) && !PVector.hasOwnProperty(method)) {
            PVector[method] = createPVectorMethod(method);
          }
        }
    
        return PVector;
      }());
    
    
    
    
    
    
      var distance = function(v) {
        var dx, dy, dz;
        dx = this.x - v.x;
        dy = this.y - v.y;
        dz = this.z - v.z;
        return Math.sqrt(dx * dx + dy * dy + dz * dz);
      };
    
    
      /*********
      ** V3r1 **
      *********/
    
      function V3r1(x, y, z) {
        this.x = x != null ? x : 0;
        this.y = y != null ? y : 0;
        this.z = z != null ? z : 0;
      }
    
      V3r1.prototype.add = function(v) {};
      V3r1.prototype.distanceSq = function(v) {};
      V3r1.prototype.distance = distance;
      V3r1.prototype.normalize = function() {};
      V3r1.prototype.setMagnitude = function(s) {};
      V3r1.prototype.capMagnitude = function(s) {};
      V3r1.prototype.sub = function(v) {};
      V3r1.prototype.mul = function(v) {};
      V3r1.prototype.div = function(v) {};
      V3r1.prototype.mulScalar = function(s) {};
      V3r1.prototype.divScalar = function(s) {};
      V3r1.prototype.negate = function() {};
      V3r1.prototype.cross = function(v) {};
    
    
      /*********
      ** V3r2 **
      *********/
    
      function V3r2(x, y, z) {
        this.x = x != null ? x : 0;
        this.y = y != null ? y : 0;
        this.z = z != null ? z : 0;
      }
    
    
      V3r2.prototype = {
        constructor : V3r2,
        add : function(v) {},
        distanceSq : function(v) {},
        distance : distance,
        normalize : function() {},
        setMagnitude : function(s) {},
        capMagnitude : function(s) {},
        sub : function(v) {},
        mul : function(v) {},
        div : function(v) {},
        mulScalar : function(s) {},
        divScalar : function(s) {},
        negate : function() {},
        cross : function(v) {}
      }
    
    
    
      /*********
      ** V3r3 **
      *********/
    
      function V3r3(x, y, z) {
        this.x = x != null ? x : 0;
        this.y = y != null ? y : 0;
        this.z = z != null ? z : 0;
      }
    
    
      V3r3.prototype = {
        constructor : V3r3,
        distance : distance
      }
    
    
    
    
    var p1,p2,r11,r12,r21,r22,r31,r32;
    
    p1  = new PVector(3,4,5);
    p2  = new PVector(-4,2,-4);
    r11 = new V3r1(3,4,5);
    r12 = new V3r1(-4,2,-4);
    r21 = new V3r2(3,4,5);
    r22 = new V3r2(-4,2,-4);
    r31 = new V3r3(3,4,5);
    r32 = new V3r3(-4,2,-4);

Test runner

Ready to run.

Testing in
TestOps/sec
r3
r31.distance(r32);
ready
r1
r11.distance(r12);
ready
r2
r21.distance(r22);
ready
pv
p1.dist(p2);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by ponysteps on