Vector2.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. this.multiplyScalar( Math.max( min, Math.min( max, length ) ) / length );
  163. return this;
  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. lengthSq: function () {
  194. return this.x * this.x + this.y * this.y;
  195. },
  196. length: function () {
  197. return Math.sqrt( this.x * this.x + this.y * this.y );
  198. },
  199. lengthManhattan: function() {
  200. return Math.abs( this.x ) + Math.abs( this.y );
  201. },
  202. normalize: function () {
  203. return this.divideScalar( this.length() );
  204. },
  205. distanceTo: function ( v ) {
  206. return Math.sqrt( this.distanceToSquared( v ) );
  207. },
  208. distanceToSquared: function ( v ) {
  209. var dx = this.x - v.x, dy = this.y - v.y;
  210. return dx * dx + dy * dy;
  211. },
  212. setLength: function ( length ) {
  213. return this.multiplyScalar( length / this.length() );
  214. },
  215. lerp: function ( v, alpha ) {
  216. this.x += ( v.x - this.x ) * alpha;
  217. this.y += ( v.y - this.y ) * alpha;
  218. return this;
  219. },
  220. lerpVectors: function ( v1, v2, alpha ) {
  221. this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
  222. return this;
  223. },
  224. equals: function ( v ) {
  225. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  226. },
  227. fromArray: function ( array, offset ) {
  228. if ( offset === undefined ) offset = 0;
  229. this.x = array[ offset ];
  230. this.y = array[ offset + 1 ];
  231. return this;
  232. },
  233. toArray: function ( array, offset ) {
  234. if ( array === undefined ) array = [];
  235. if ( offset === undefined ) offset = 0;
  236. array[ offset ] = this.x;
  237. array[ offset + 1 ] = this.y;
  238. return array;
  239. },
  240. fromAttribute: function ( attribute, index, offset ) {
  241. if ( offset === undefined ) offset = 0;
  242. index = index * attribute.itemSize + offset;
  243. this.x = attribute.array[ index ];
  244. this.y = attribute.array[ index + 1 ];
  245. return this;
  246. },
  247. rotateAround: function ( center, angle ) {
  248. var c = Math.cos( angle ), s = Math.sin( angle );
  249. var x = this.x - center.x;
  250. var y = this.y - center.y;
  251. this.x = x * c - y * s + center.x;
  252. this.y = x * s + y * c + center.y;
  253. return this;
  254. }
  255. };
粤ICP备19079148号