Advertisement
EntropyStarRover

4_5. Vector Math

Oct 3rd, 2019
174
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function main(){
  3.     function add(v1,v2){
  4.         let r=[v1[0] + v2[0], v1[1] + v2[1]];
  5.           return r;  
  6.     }
  7.  
  8.     function multiply(v1,v2){
  9.         let r=[v1[0]*v2, v1[1]*v2];
  10.           return r;
  11.     }
  12.  
  13.     function length(v1){
  14.         let r=Math.sqrt((v1[0]*v1[0])+(v1[1]*v1[1]));
  15.          return r;
  16.     }
  17.  
  18.     function dot(v1,v2){
  19.         let r=(v1[0]*v1[1])+(v2[0]*v2[1]);
  20.         return r;
  21.     }
  22.  
  23.     function cross(v1,v2){
  24.         let r=(v1[0]*v2[1])-(v1[1]*v2[0]);
  25.          return r;
  26.     }
  27.  
  28.     return{add, multiply, length, dot, cross}
  29. }
  30.  
  31. let result=main();
  32.  
  33. console.log(result.add([1, 1], [1, 0]));
  34. console.log(result.multiply([3.5, -2], 2));
  35. console.log(result.length([3, -4]));
  36. console.log(result.dot([1, 0], [0, -1]));
  37. console.log(result.cross([3, 7], [1, 0]));
Advertisement
RAW Paste Data Copied
Advertisement