Vector2.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author philogb / http://blog.thejit.org/
  4. * @author egraether / http://egraether.com/
  5. * @author zz85 / http://www.lab4games.net/zz85/blog
  6. */
  7. THREE.Vector2 = function ( x, y ) {
  8. this.x = x || 0;
  9. this.y = y || 0;
  10. };
  11. THREE.Vector2.prototype = {
  12. constructor: THREE.Vector2,
  13. set: function ( x, y ) {
  14. this.x = x;
  15. this.y = y;
  16. return this;
  17. },
  18. setX: function ( x ) {
  19. this.x = x;
  20. return this;
  21. },
  22. setY: function ( y ) {
  23. this.y = y;
  24. return this;
  25. },
  26. copy: function ( v ) {
  27. this.x = v.x;
  28. this.y = v.y;
  29. return this;
  30. },
  31. addScalar: function ( s ) {
  32. this.x += s;
  33. this.y += s;
  34. return this;
  35. },
  36. add: function ( a, b ) {
  37. this.x = a.x + b.x;
  38. this.y = a.y + b.y;
  39. return this;
  40. },
  41. addSelf: function ( v ) {
  42. this.x += v.x;
  43. this.y += v.y;
  44. return this;
  45. },
  46. sub: function ( a, b ) {
  47. this.x = a.x - b.x;
  48. this.y = a.y - b.y;
  49. return this;
  50. },
  51. subSelf: function ( v ) {
  52. this.x -= v.x;
  53. this.y -= v.y;
  54. return this;
  55. },
  56. multiplyScalar: function ( s ) {
  57. this.x *= s;
  58. this.y *= s;
  59. return this;
  60. },
  61. divideScalar: function ( s ) {
  62. if ( s ) {
  63. this.x /= s;
  64. this.y /= s;
  65. } else {
  66. this.set( 0, 0 );
  67. }
  68. return this;
  69. },
  70. minSelf: function ( v ) {
  71. if ( this.x > v.x ) {
  72. this.x = v.x;
  73. }
  74. if ( this.y > v.y ) {
  75. this.y = v.y;
  76. }
  77. return this;
  78. },
  79. maxSelf: function ( v ) {
  80. if ( this.x < v.x ) {
  81. this.x = v.x;
  82. }
  83. if ( this.y < v.y ) {
  84. this.y = v.y;
  85. }
  86. return this;
  87. },
  88. clampSelf: function ( min, max ) {
  89. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  90. if ( this.x < min.x ) {
  91. this.x = min.x;
  92. } else if ( this.x > max.x ) {
  93. this.x = max.x;
  94. }
  95. if ( this.y < min.y ) {
  96. this.y = min.y;
  97. } else if ( this.y > max.y ) {
  98. this.y = max.y;
  99. }
  100. return this;
  101. },
  102. negate: function() {
  103. return this.multiplyScalar( - 1 );
  104. },
  105. dot: function ( v ) {
  106. return this.x * v.x + this.y * v.y;
  107. },
  108. lengthSq: function () {
  109. return this.x * this.x + this.y * this.y;
  110. },
  111. length: function () {
  112. return Math.sqrt( this.lengthSq() );
  113. },
  114. normalize: function () {
  115. return this.divideScalar( this.length() );
  116. },
  117. distanceTo: function ( v ) {
  118. return Math.sqrt( this.distanceToSquared( v ) );
  119. },
  120. distanceToSquared: function ( v ) {
  121. var dx = this.x - v.x, dy = this.y - v.y;
  122. return dx * dx + dy * dy;
  123. },
  124. setLength: function ( l ) {
  125. return this.normalize().multiplyScalar( l );
  126. },
  127. lerpSelf: function ( v, alpha ) {
  128. this.x += ( v.x - this.x ) * alpha;
  129. this.y += ( v.y - this.y ) * alpha;
  130. return this;
  131. },
  132. equals: function( v ) {
  133. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  134. },
  135. clone: function () {
  136. return new THREE.Vector2( this.x, this.y );
  137. }
  138. };