Vector2.js 6.8 KB

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