Vector2.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. function Vector2( x = 0, y = 0 ) {
  2. this.x = x;
  3. this.y = y;
  4. }
  5. Object.defineProperties( Vector2.prototype, {
  6. "width": {
  7. get: function () {
  8. return this.x;
  9. },
  10. set: function ( value ) {
  11. this.x = value;
  12. }
  13. },
  14. "height": {
  15. get: function () {
  16. return this.y;
  17. },
  18. set: function ( value ) {
  19. this.y = value;
  20. }
  21. }
  22. } );
  23. Object.assign( Vector2.prototype, {
  24. isVector2: true,
  25. set: function ( x, y ) {
  26. this.x = x;
  27. this.y = y;
  28. return this;
  29. },
  30. setScalar: function ( scalar ) {
  31. this.x = scalar;
  32. this.y = scalar;
  33. return this;
  34. },
  35. setX: function ( x ) {
  36. this.x = x;
  37. return this;
  38. },
  39. setY: function ( y ) {
  40. this.y = y;
  41. return this;
  42. },
  43. setComponent: function ( index, value ) {
  44. switch ( index ) {
  45. case 0: this.x = value; break;
  46. case 1: this.y = value; break;
  47. default: throw new Error( 'index is out of range: ' + index );
  48. }
  49. return this;
  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. this.x *= scalar;
  116. this.y *= scalar;
  117. return this;
  118. },
  119. divide: function ( v ) {
  120. this.x /= v.x;
  121. this.y /= v.y;
  122. return this;
  123. },
  124. divideScalar: function ( scalar ) {
  125. return this.multiplyScalar( 1 / scalar );
  126. },
  127. applyMatrix3: function ( m ) {
  128. const x = this.x, y = this.y;
  129. const e = m.elements;
  130. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ];
  131. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ];
  132. return this;
  133. },
  134. min: function ( v ) {
  135. this.x = Math.min( this.x, v.x );
  136. this.y = Math.min( this.y, v.y );
  137. return this;
  138. },
  139. max: function ( v ) {
  140. this.x = Math.max( this.x, v.x );
  141. this.y = Math.max( this.y, v.y );
  142. return this;
  143. },
  144. clamp: function ( min, max ) {
  145. // assumes min < max, componentwise
  146. this.x = Math.max( min.x, Math.min( max.x, this.x ) );
  147. this.y = Math.max( min.y, Math.min( max.y, this.y ) );
  148. return this;
  149. },
  150. clampScalar: function ( minVal, maxVal ) {
  151. this.x = Math.max( minVal, Math.min( maxVal, this.x ) );
  152. this.y = Math.max( minVal, Math.min( maxVal, this.y ) );
  153. return this;
  154. },
  155. clampLength: function ( min, max ) {
  156. const length = this.length();
  157. return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
  158. },
  159. floor: function () {
  160. this.x = Math.floor( this.x );
  161. this.y = Math.floor( this.y );
  162. return this;
  163. },
  164. ceil: function () {
  165. this.x = Math.ceil( this.x );
  166. this.y = Math.ceil( this.y );
  167. return this;
  168. },
  169. round: function () {
  170. this.x = Math.round( this.x );
  171. this.y = Math.round( this.y );
  172. return this;
  173. },
  174. roundToZero: function () {
  175. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  176. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  177. return this;
  178. },
  179. negate: function () {
  180. this.x = - this.x;
  181. this.y = - this.y;
  182. return this;
  183. },
  184. dot: function ( v ) {
  185. return this.x * v.x + this.y * v.y;
  186. },
  187. cross: function ( v ) {
  188. return this.x * v.y - this.y * v.x;
  189. },
  190. lengthSq: function () {
  191. return this.x * this.x + this.y * this.y;
  192. },
  193. length: function () {
  194. return Math.sqrt( this.x * this.x + this.y * this.y );
  195. },
  196. manhattanLength: function () {
  197. return Math.abs( this.x ) + Math.abs( this.y );
  198. },
  199. normalize: function () {
  200. return this.divideScalar( this.length() || 1 );
  201. },
  202. angle: function () {
  203. // computes the angle in radians with respect to the positive x-axis
  204. const angle = Math.atan2( - this.y, - this.x ) + Math.PI;
  205. return angle;
  206. },
  207. distanceTo: function ( v ) {
  208. return Math.sqrt( this.distanceToSquared( v ) );
  209. },
  210. distanceToSquared: function ( v ) {
  211. const dx = this.x - v.x, dy = this.y - v.y;
  212. return dx * dx + dy * dy;
  213. },
  214. manhattanDistanceTo: function ( v ) {
  215. return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y );
  216. },
  217. setLength: function ( length ) {
  218. return this.normalize().multiplyScalar( 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. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  227. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  228. return this;
  229. },
  230. equals: function ( v ) {
  231. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  232. },
  233. fromArray: function ( array, offset ) {
  234. if ( offset === undefined ) offset = 0;
  235. this.x = array[ offset ];
  236. this.y = array[ offset + 1 ];
  237. return this;
  238. },
  239. toArray: function ( array, offset ) {
  240. if ( array === undefined ) array = [];
  241. if ( offset === undefined ) offset = 0;
  242. array[ offset ] = this.x;
  243. array[ offset + 1 ] = this.y;
  244. return array;
  245. },
  246. fromBufferAttribute: function ( attribute, index, offset ) {
  247. if ( offset !== undefined ) {
  248. console.warn( 'THREE.Vector2: offset has been removed from .fromBufferAttribute().' );
  249. }
  250. this.x = attribute.getX( index );
  251. this.y = attribute.getY( index );
  252. return this;
  253. },
  254. rotateAround: function ( center, angle ) {
  255. const c = Math.cos( angle ), s = Math.sin( angle );
  256. const x = this.x - center.x;
  257. const y = this.y - center.y;
  258. this.x = x * c - y * s + center.x;
  259. this.y = x * s + y * c + center.y;
  260. return this;
  261. },
  262. random: function () {
  263. this.x = Math.random();
  264. this.y = Math.random();
  265. return this;
  266. }
  267. } );
  268. export { Vector2 };
粤ICP备19079148号