Vector2.js 6.5 KB

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