Vector2.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. set: function ( x, y ) {
  14. this.x = x;
  15. this.y = y;
  16. return this;
  17. },
  18. copy: function ( v ) {
  19. this.x = v.x;
  20. this.y = v.y;
  21. return this;
  22. },
  23. add: function ( a, b ) {
  24. this.x = a.x + b.x;
  25. this.y = a.y + b.y;
  26. return this;
  27. },
  28. addSelf: function ( v ) {
  29. this.x += v.x;
  30. this.y += v.y;
  31. return this;
  32. },
  33. sub: function ( a, b ) {
  34. this.x = a.x - b.x;
  35. this.y = a.y - b.y;
  36. return this;
  37. },
  38. subSelf: function ( v ) {
  39. this.x -= v.x;
  40. this.y -= v.y;
  41. return this;
  42. },
  43. multiplyScalar: function ( s ) {
  44. this.x *= s;
  45. this.y *= s;
  46. return this;
  47. },
  48. divideScalar: function ( s ) {
  49. if ( s ) {
  50. this.x /= s;
  51. this.y /= s;
  52. } else {
  53. this.set( 0, 0 );
  54. }
  55. return this;
  56. },
  57. negate: function() {
  58. return this.multiplyScalar( - 1 );
  59. },
  60. dot: function ( v ) {
  61. return this.x * v.x + this.y * v.y;
  62. },
  63. lengthSq: function () {
  64. return this.x * this.x + this.y * this.y;
  65. },
  66. length: function () {
  67. return Math.sqrt( this.lengthSq() );
  68. },
  69. normalize: function () {
  70. return this.divideScalar( this.length() );
  71. },
  72. distanceTo: function ( v ) {
  73. return Math.sqrt( this.distanceToSquared( v ) );
  74. },
  75. distanceToSquared: function ( v ) {
  76. var dx = this.x - v.x, dy = this.y - v.y;
  77. return dx * dx + dy * dy;
  78. },
  79. setLength: function ( l ) {
  80. return this.normalize().multiplyScalar( l );
  81. },
  82. lerpSelf: function ( v, alpha ) {
  83. this.x += ( v.x - this.x ) * alpha;
  84. this.y += ( v.y - this.y ) * alpha;
  85. return this;
  86. },
  87. equals: function( v ) {
  88. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  89. },
  90. isZero: function ( v ) {
  91. return this.lengthSq() < ( v !== undefined ? v : 0.0001 );
  92. },
  93. clone: function () {
  94. return new THREE.Vector2( this.x, this.y );
  95. }
  96. };
粤ICP备19079148号