Vector2.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author philogb / http://blog.thejit.org/
  4. */
  5. THREE.Vector2 = function ( x, y ) {
  6. this.x = x || 0;
  7. this.y = y || 0;
  8. };
  9. THREE.Vector2.prototype = {
  10. set: function ( x, y ) {
  11. this.x = x;
  12. this.y = y;
  13. },
  14. copy: function ( v ) {
  15. this.x = v.x;
  16. this.y = v.y;
  17. },
  18. addSelf: function ( v ) {
  19. this.x += v.x;
  20. this.y += v.y;
  21. return this;
  22. },
  23. add: function ( v1, v2 ) {
  24. this.x = v1.x + v2.x;
  25. this.y = v1.y + v2.y;
  26. },
  27. subSelf: function ( v ) {
  28. this.x -= v.x;
  29. this.y -= v.y;
  30. return this;
  31. },
  32. sub: function ( v1, v2 ) {
  33. this.x = v1.x - v2.x;
  34. this.y = v1.y - v2.y;
  35. },
  36. multiplyScalar: function ( s ) {
  37. this.x *= s;
  38. this.y *= s;
  39. return this;
  40. },
  41. unit: function () {
  42. this.multiplyScalar( 1 / this.length() );
  43. },
  44. length: function () {
  45. return Math.sqrt( this.x * this.x + this.y * this.y );
  46. },
  47. lengthSq: function () {
  48. return this.x * this.x + this.y * this.y;
  49. },
  50. negate: function() {
  51. this.x = - this.x;
  52. this.y = - this.y;
  53. },
  54. clone: function () {
  55. return new THREE.Vector2( this.x, this.y );
  56. },
  57. toString: function () {
  58. return 'THREE.Vector2 (' + this.x + ', ' + this.y + ')';
  59. }
  60. };
粤ICP备19079148号