Guest User

Untitled

a guest
Dec 9th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. var Vector2 = function( )
  2. {
  3. this._x = 0;
  4. this._y = 0;
  5. this.DEGRAD = 0;
  6. };
  7.  
  8. Vector2.prototype.initialize = function( x, y )
  9. {
  10. _x = x;
  11. _y = y;
  12. };
  13.  
  14. Vector2.prototype.getX = function( )
  15. {
  16. return _x;
  17. };
  18.  
  19. Vector2.prototype.setX = function( x )
  20. {
  21. _x = x;
  22. };
  23.  
  24. Vector2.prototypegetY = function( )
  25. {
  26. return _y;
  27. };
  28.  
  29. Vector2.prototype.setY = function( y )
  30. {
  31. _y = y;
  32. };
  33.  
  34. // ------------------- public methods ------------------- //
  35.  
  36. Vector2.prototype.add = function( vector )
  37. {
  38. return new Vector2( _x + vector.x, _y + vector.y );
  39. };
  40.  
  41. Vector2.prototype.subtract = function( vector )
  42. {
  43. return new Vector2( _x - vector.x, _y - vector.y );
  44. };
  45.  
  46. Vector2.prototype.multiply = function( vector )
  47. {
  48. return new Vector2( _x * vector.x, _y * vector.y );
  49. };
  50.  
  51. Vector2.prototype.divide = function( vector )
  52. {
  53. return new Vector2( _x / vector.x, _y / vector.y );
  54. };
  55.  
  56. Vector2.prototype.distance = function( vector )
  57. {
  58. var deltaX = _x - vector.x;
  59. var deltaY = _y - vector.y;
  60. return Math.sqrt( deltaX * deltaX + deltaY * deltaY );
  61. };
  62.  
  63. Vector2.prototype.distanceSqr = function( vector )
  64. {
  65. var deltaX = _x - vector.x;
  66. var deltaY = _y - vector.y;
  67. return ( deltaX * deltaX + deltaY * deltaY );
  68. };
  69.  
  70. Vector2.prototype.magnitude = function( )
  71. {
  72. return Math.sqrt( _x * _x + _y * _y );
  73. };
  74.  
  75. Vector2.prototype.normalize = function( )
  76. {
  77. var mag = Math.sqrt( _x * _x + _y * _y );
  78.  
  79. if ( mag === 0 )
  80. {
  81. _x = 0;
  82. _y = 0;
  83. }
  84. else
  85. {
  86. _x = _x / mag;
  87. _y = _y / mag;
  88. }
  89. };
  90.  
  91. Vector2.prototype.getNormalized = function( )
  92. {
  93. var mag = Matn.sqrt( _x * _x + _y * _y );
  94. return new Vector2( _x / mag, _y / mag );
  95. };
  96.  
  97. Vector2.prototype.getAngle = function( )
  98. {
  99. return Math.atan2( _y, _x ) * 180 / Math.PI;
  100. };
  101.  
  102. Vector2.prototype.degToVec = function( deg )
  103. {
  104. var rad = deg * DEGRAD;
  105. return new Vector2( Math.cos( rad ), Math.sin( rad ) );
  106. };
  107.  
  108. Vector2.prototype.radToVec = function( )
  109. {
  110. return new Vector2( Math.sin( rad ), Math.cos( rad ) );
  111. };
  112.  
  113. // ---------------- additional vector methods ---------------- //
  114.  
  115. Vector2.prototype.dot = function( vector )
  116. {
  117. return ( _x * vector.x + _y * vector.y );
  118. };
  119.  
  120. Vector2.prototype.rotate = function( deg )
  121. {
  122. var rad = deg * DEGRAD;
  123. var cos = Math.cos( rad );
  124. var sin = Math.sin( rad );
  125. _x = _x * cos - _y * sin;
  126. _y = _y * cos + _x * sin;
  127. };
  128.  
  129. Vector2.prototype.perpRight = function( )
  130. {
  131. return new Vector2( -_y, _x );
  132. };
  133.  
  134. Vector2.prototype.toString = function( )
  135. {
  136. return ( "x : " + int( _x * 100 ) / 100 + ", \ty : " + int( _y * 100 ) / 100 );
  137. };
Add Comment
Please, Sign In to add comment