Vector2.js 1.3 KB

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