Vector2.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. get width() {
  14. return this.x;
  15. },
  16. set width( value ) {
  17. this.x = value;
  18. },
  19. get height() {
  20. return this.y;
  21. },
  22. set height( value ) {
  23. this.y = value;
  24. },
  25. //
  26. set: function ( x, y ) {
  27. this.x = x;
  28. this.y = y;
  29. return this;
  30. },
  31. setScalar: function ( scalar ) {
  32. this.x = scalar;
  33. this.y = scalar;
  34. return this;
  35. },
  36. setX: function ( x ) {
  37. this.x = x;
  38. return this;
  39. },
  40. setY: function ( y ) {
  41. this.y = y;
  42. return this;
  43. },
  44. setComponent: function ( index, value ) {
  45. switch ( index ) {
  46. case 0: this.x = value; break;
  47. case 1: this.y = value; break;
  48. default: throw new Error( 'index is out of range: ' + index );
  49. }
  50. },
  51. getComponent: function ( index ) {
  52. switch ( index ) {
  53. case 0: return this.x;
  54. case 1: return this.y;
  55. default: throw new Error( 'index is out of range: ' + index );
  56. }
  57. },
  58. clone: function () {
  59. return new this.constructor( this.x, this.y );
  60. },
  61. copy: function ( v ) {
  62. this.x = v.x;
  63. this.y = v.y;
  64. return this;
  65. },
  66. add: function ( v, w ) {
  67. if ( w !== undefined ) {
  68. console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  69. return this.addVectors( v, w );
  70. }
  71. this.x += v.x;
  72. this.y += v.y;
  73. return this;
  74. },
  75. addScalar: function ( s ) {
  76. this.x += s;
  77. this.y += s;
  78. return this;
  79. },
  80. addVectors: function ( a, b ) {
  81. this.x = a.x + b.x;
  82. this.y = a.y + b.y;
  83. return this;
  84. },
  85. addScaledVector: function ( v, s ) {
  86. this.x += v.x * s;
  87. this.y += v.y * s;
  88. return this;
  89. },
  90. sub: function ( v, w ) {
  91. if ( w !== undefined ) {
  92. console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  93. return this.subVectors( v, w );
  94. }
  95. this.x -= v.x;
  96. this.y -= v.y;
  97. return this;
  98. },
  99. subScalar: function ( s ) {
  100. this.x -= s;
  101. this.y -= s;
  102. return this;
  103. },
  104. subVectors: function ( a, b ) {
  105. this.x = a.x - b.x;
  106. this.y = a.y - b.y;
  107. return this;
  108. },
  109. multiply: function ( v ) {
  110. this.x *= v.x;
  111. this.y *= v.y;
  112. return this;
  113. },
  114. multiplyScalar: function ( scalar ) {
  115. if ( isFinite( scalar ) ) {
  116. this.x *= scalar;
  117. this.y *= scalar;
  118. } else {
  119. this.x = 0;
  120. this.y = 0;
  121. }
  122. return this;
  123. },
  124. divide: function ( v ) {
  125. this.x /= v.x;
  126. this.y /= v.y;
  127. return this;
  128. },
  129. divideScalar: function ( scalar ) {
  130. return this.multiplyScalar( 1 / scalar );
  131. },
  132. min: function ( v ) {
  133. this.x = Math.min( this.x, v.x );
  134. this.y = Math.min( this.y, v.y );
  135. return this;
  136. },
  137. max: function ( v ) {
  138. this.x = Math.max( this.x, v.x );
  139. this.y = Math.max( this.y, v.y );
  140. return this;
  141. },
  142. clamp: function ( min, max ) {
  143. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  144. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  145. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  146. return this;
  147. },
  148. clampScalar: function () {
  149. var min, max;
  150. return function clampScalar( minVal, maxVal ) {
  151. if ( min === undefined ) {
  152. min = new THREE.Vector2();
  153. max = new THREE.Vector2();
  154. }
  155. min.set( minVal, minVal );
  156. max.set( maxVal, maxVal );
  157. return this.clamp( min, max );
  158. };
  159. }(),
  160. clampLength: function ( min, max ) {
  161. var length = this.length();
  162. return this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
  163. },
  164. floor: function () {
  165. this.x = Math.floor( this.x );
  166. this.y = Math.floor( this.y );
  167. return this;
  168. },
  169. ceil: function () {
  170. this.x = Math.ceil( this.x );
  171. this.y = Math.ceil( this.y );
  172. return this;
  173. },
  174. round: function () {
  175. this.x = Math.round( this.x );
  176. this.y = Math.round( this.y );
  177. return this;
  178. },
  179. roundToZero: function () {
  180. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  181. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  182. return this;
  183. },
  184. negate: function () {
  185. this.x = - this.x;
  186. this.y = - this.y;
  187. return this;
  188. },
  189. dot: function ( v ) {
  190. return this.x * v.x + this.y * v.y;
  191. },
  192. lengthSq: function () {
  193. return this.x * this.x + this.y * this.y;
  194. },
  195. length: function () {
  196. return Math.sqrt( this.x * this.x + this.y * this.y );
  197. },
  198. lengthManhattan: function() {
  199. return Math.abs( this.x ) + Math.abs( this.y );
  200. },
  201. normalize: function () {
  202. return this.divideScalar( this.length() );
  203. },
  204. angle: function () {
  205. // computes the angle in radians with respect to the positive x-axis
  206. var angle = Math.atan2( this.y, this.x );
  207. if ( angle < 0 ) angle += 2 * Math.PI;
  208. return angle;
  209. },
  210. distanceTo: function ( v ) {
  211. return Math.sqrt( this.distanceToSquared( v ) );
  212. },
  213. distanceToSquared: function ( v ) {
  214. var dx = this.x - v.x, dy = this.y - v.y;
  215. return dx * dx + dy * dy;
  216. },
  217. setLength: function ( length ) {
  218. return this.multiplyScalar( length / this.length() );
  219. },
  220. lerp: function ( v, alpha ) {
  221. this.x += ( v.x - this.x ) * alpha;
  222. this.y += ( v.y - this.y ) * alpha;
  223. return this;
  224. },
  225. lerpVectors: function ( v1, v2, alpha ) {
  226. return this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
  227. },
  228. equals: function ( v ) {
  229. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  230. },
  231. fromArray: function ( array, offset ) {
  232. if ( offset === undefined ) offset = 0;
  233. this.x = array[ offset ];
  234. this.y = array[ offset + 1 ];
  235. return this;
  236. },
  237. toArray: function ( array, offset ) {
  238. if ( array === undefined ) array = [];
  239. if ( offset === undefined ) offset = 0;
  240. array[ offset ] = this.x;
  241. array[ offset + 1 ] = this.y;
  242. return array;
  243. },
  244. fromAttribute: function ( attribute, index, offset ) {
  245. if ( offset === undefined ) offset = 0;
  246. index = index * attribute.itemSize + offset;
  247. this.x = attribute.array[ index ];
  248. this.y = attribute.array[ index + 1 ];
  249. return this;
  250. },
  251. rotateAround: function ( center, angle ) {
  252. var c = Math.cos( angle ), s = Math.sin( angle );
  253. var x = this.x - center.x;
  254. var y = this.y - center.y;
  255. this.x = x * c - y * s + center.x;
  256. this.y = x * s + y * c + center.y;
  257. return this;
  258. }
  259. };