Guest User

Untitled

a guest
Jan 20th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. var Vector = function(x, y) {
  2. this.x = x || 0;
  3. this.y = y || 0;
  4. };
  5.  
  6. // return the angle of the vector in radians
  7. Vector.prototype.getDirection = function() {
  8. return Math.atan2(this.y, this.x);
  9. };
  10.  
  11. // set the direction of the vector in radians
  12. Vector.prototype.setDirection = function(direction) {
  13. var magnitude = this.getMagnitude();
  14. this.x = Math.cos(angle) * magnitude;
  15. this.y = Math.sin(angle) * magnitude;
  16. };
  17.  
  18. // get the magnitude of the vector
  19. Vector.prototype.getMagnitude = function() {
  20. // use pythagoras theorem to work out the magnitude of the vector
  21. return Math.sqrt(this.x * this.x + this.y * this.y);
  22. };
  23.  
  24. // set the magnitude of the vector
  25. Vector.prototype.setMagnitude = function(magnitude) {
  26. var direction = this.getDirection();
  27. this.x = Math.cos(direction) * magnitude;
  28. this.y = Math.sin(direction) * magnitude;
  29. };
  30.  
  31. // add two vectors together and return a new one
  32. Vector.prototype.add = function(v2) {
  33. return new Vector(this.x + v2.x, this.y + v2.y);
  34. };
  35.  
  36. // add a vector to this one
  37. Vector.prototype.addTo = function(v2) {
  38. this.x += v2.x;
  39. this.y += v2.y;
  40. };
  41.  
  42. // subtract two vectors and reutn a new one
  43. Vector.prototype.subtract = function(v2) {
  44. return new Vector(this.x - v2.x, this.y - v2.y);
  45. };
  46.  
  47. // subtract a vector from this one
  48. Vector.prototype.subtractFrom = function(v2) {
  49. this.x -= v2.x;
  50. this.y -= v2.y;
  51. };
  52.  
  53. // multiply this vector by a scalar and return a new one
  54. Vector.prototype.multiply = function(scalar) {
  55. return new Vector(this.x * scalar, this.y * scalar);
  56. };
  57.  
  58. // multiply this vector by the scalar
  59. Vector.prototype.multiplyBy = function(scalar) {
  60. this.x *= scalar;
  61. this.y *= scalar;
  62. };
  63.  
  64. // scale this vector by scalar and return a new vector
  65. Vector.prototype.divide = function(scalar) {
  66. return new Vector(this.x / scalar, this.y / scalar);
  67. };
  68.  
  69. // scale this vector by scalar
  70. Vector.prototype.divideBy = function(scalar) {
  71. this.x /= scalar;
  72. this.y /= scalar;
  73. };
  74.  
  75. // Aliases
  76. Vector.prototype.getLength = Vector.prototype.getMagnitude;
  77. Vector.prototype.setLength = Vector.prototype.setMagnitude;
  78.  
  79. Vector.prototype.getAngle = Vector.prototype.getDirection;
  80. Vector.prototype.setAngle = Vector.prototype.setDirection;
  81.  
  82. // Utilities
  83. Vector.prototype.copy = function() {
  84. return new Vector(this.x, this.y);
  85. };
  86.  
  87. Vector.prototype.toString = function() {
  88. return 'x: ' + this.x + ', y: ' + this.y;
  89. };
  90.  
  91. Vector.prototype.toArray = function() {
  92. return [this.x, this.y];
  93. };
  94.  
  95. Vector.prototype.toObject = function() {
  96. return {x: this.x, y: this.y};
  97. };
  98.  
  99. // dot product of two vectors
  100. Vector.prototype.dotProduct = function(v2) {
  101. return this.x * v2.x + this.y *v2.y;
  102. }
  103.  
  104. // normalize a given vector
  105. Vector.prototype.normalize = function(){
  106. return new Vector(this.x/(Math.sqrt(this.x * this.x + this.y * this.y)), this.y/(Math.sqrt(this.x * this.x + this.y * this.y)));
  107. }
  108.  
  109. // To add
  110. // Scale
Add Comment
Please, Sign In to add comment