Vector2.js 5.8 KB

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