Vector2.js 1.2 KB

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