CVector2.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function CVector2(iX, iY) {
  2. var x;
  3. var y;
  4. this._init = function (iX, iY) {
  5. x = iX;
  6. y = iY;
  7. };
  8. this.add = function (vx, vy) {
  9. x += vx;
  10. y += vy;
  11. };
  12. this.addV = function (v) {
  13. x += v.getX();
  14. y += v.getY();
  15. };
  16. this.scalarDivision = function (n) {
  17. x /= n;
  18. y /= n;
  19. };
  20. this.subV = function (v) {
  21. x -= v.getX();
  22. y -= v.getY();
  23. };
  24. this.scalarProduct = function (n) {
  25. x *= n;
  26. y *= n;
  27. };
  28. this.invert = function () {
  29. x *= -1;
  30. y *= -1;
  31. };
  32. this.dotProduct = function (v) {
  33. return (x * v.getX() + y * v.getY());
  34. };
  35. this.set = function (fx, fy) {
  36. x = fx;
  37. y = fy;
  38. };
  39. this.setV = function (v) {
  40. x = v.getX();
  41. y = v.getY();
  42. };
  43. this.length = function () {
  44. return Math.sqrt(x * x + y * y);
  45. };
  46. this.length2 = function () {
  47. return x * x + y * y;
  48. };
  49. this.normalize = function () {
  50. var len = this.length();
  51. if (len > 0) {
  52. x /= len;
  53. y /= len;
  54. }
  55. };
  56. this.angleBetweenVectors = function (v2) {
  57. var iAngle = Math.acos(this.dotProduct(v2) / (this.length() * v2.length()));
  58. if (isNaN(iAngle) === true) {
  59. return 0;
  60. } else {
  61. return iAngle;
  62. }
  63. };
  64. this.getNormalize = function (outV) {
  65. var len = this.length();
  66. outV.set(x, y);
  67. outV.normalize();
  68. };
  69. this.rot90CCW = function () {
  70. var a = x;
  71. x = -y;
  72. y = a;
  73. };
  74. this.rot90CW = function () {
  75. var a = x;
  76. x = y;
  77. y = -a;
  78. };
  79. this.getRotCCW = function (outV) {
  80. outV.set(x, y);
  81. outV.rot90CCW();
  82. };
  83. this.getRotCW = function (outV) {
  84. outV.set(x, y);
  85. outV.rot90CW();
  86. };
  87. this.ceil = function () {
  88. x = Math.ceil(x);
  89. y = Math.ceil(y);
  90. };
  91. this.round = function () {
  92. x = Math.round(x);
  93. y = Math.round(y);
  94. };
  95. this.toString = function () {
  96. return "Vector2: " + x + ", " + y;
  97. };
  98. this.print = function () {
  99. trace("Vector2: " + x + ", " + y + "");
  100. };
  101. this.getX = function () {
  102. return x;
  103. };
  104. this.getY = function () {
  105. return y;
  106. };
  107. this._init(iX, iY);
  108. }