Advertisement
dim4o

Vector

Mar 12th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Vector = (function () {
  2.     // Constructor
  3.     function Vector(components) {
  4.         this._components = components;
  5.         if ( this._components === undefined || this._components.length === 0) {
  6.             throw new Error("Error");
  7.         }
  8.         this._vectorLength = this._components.length;
  9.     }
  10.    
  11.     // Private method
  12.     function componentsCompatibilityCheck(currVector, otherVector) {
  13.         var currVectorLen = currVector._components.length;
  14.         var otherVectorLen = otherVector._components.length;
  15.  
  16.         if (currVectorLen != otherVectorLen) {
  17.             throw new Error();
  18.         }
  19.     }
  20.     // Prototypes
  21.     Vector.prototype = {
  22.  
  23.         add: function addVector(vector) {
  24.  
  25.             componentsCompatibilityCheck(this, vector);
  26.             var newComponents = [];
  27.  
  28.             for (var i = 0; i < this._vectorLength; i++) {
  29.                 newComponents[i] = this._components[i] + vector._components[i];
  30.             }
  31.  
  32.             return new Vector(newComponents);
  33.         },
  34.  
  35.         subtract: function subtractVector(vector) {
  36.  
  37.             componentsCompatibilityCheck(this, vector);
  38.             var newComponents = [];
  39.             var len = this._components.length;
  40.  
  41.             for (var i = 0; i < this._vectorLength; i++) {
  42.                 newComponents[i] = this._components[i] - vector._components[i];
  43.             }
  44.  
  45.             return new Vector(newComponents);
  46.         },
  47.  
  48.         dot: function dot(vector) {
  49.  
  50.             componentsCompatibilityCheck(this, vector);
  51.  
  52.             var dotResult = 0;
  53.  
  54.             for (var i = 0; i < this._vectorLength; i++) {
  55.                 dotResult += this._components[i] * vector._components[i];
  56.             }
  57.  
  58.             return dotResult;
  59.         },
  60.  
  61.         norm: function normVector(vector) {
  62.             var normResult = 0;
  63.  
  64.             for (var i = 0; i < this._vectorLength; i++) {
  65.                 normResult += this._components[i] * this._components[i];
  66.             }
  67.  
  68.             return Math.sqrt(normResult);
  69.         },
  70.  
  71.         toString: function vectorToString() {
  72.             if (this._components instanceof Array) {
  73.                 var components = this._components;
  74.                 return "(" + components.join(",") + ")";
  75.             } else {
  76.                 return this;
  77.             }
  78.         }
  79.     };
  80.     return Vector;
  81. }());
  82. var a1 = new Vector([1, 2, 3]);
  83. var b1 = new Vector([4, 5, 6]);
  84. var c1 = new Vector([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
  85. console.log(a1.toString());
  86. console.log(c1.toString());
  87.  
  88. // The following throw errors
  89. //var wrong = new Vector();
  90. //var anotherWrong = new Vector([]);
  91.  
  92.  
  93. var a2 = new Vector([1, 2, 3]);
  94. var b2 = new Vector([4, 5, 6]);
  95. var c2 = new Vector([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
  96. var result1 = a2.add(b2);
  97. console.log(result1.toString());
  98.  
  99. //a2.add(c2); // Error
  100.  
  101. var a3 = new Vector([1, 2, 3]);
  102. var b3 = new Vector([4, 5, 6]);
  103. var c3 = new Vector([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
  104. var result2 = a3.subtract(b3);
  105. console.log(result2.toString());
  106.  
  107. //a3.subtract(c3); // Error
  108.  
  109. var a4 = new Vector([1, 2, 3]);
  110. var b4 = new Vector([4, 5, 6]);
  111. var c4 = new Vector([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
  112. var result3 = a4.dot(b4);
  113. console.log(result3.toString());
  114.  
  115. //a4.dot(c4); // Error
  116.  
  117. var a = new Vector([1, 2, 3]);
  118. var b = new Vector([4, 5, 6]);
  119. var c = new Vector([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
  120. console.log(a.norm());
  121. console.log(b.norm());
  122. console.log(c.norm());
  123. console.log(a.add(b).norm());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement