Point.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var Point = (function () {
  2. function Point(x, y) {
  3. this.x = x || 0;
  4. this.y = y || 0;
  5. }
  6. Point.prototype.copy = function (other) {
  7. this.x = other.x;
  8. this.y = other.y;
  9. return this;
  10. };
  11. Point.prototype.clone = function () {
  12. return new Point(this.x, this.y);
  13. };
  14. Point.prototype.set = function (x, y) {
  15. this.x = x;
  16. this.y = y;
  17. return this;
  18. };
  19. Point.prototype.equal = function (other) {
  20. return other.x === this.x && other.y === this.y;
  21. };
  22. Point.prototype.add = function (other) {
  23. this.x += other.x;
  24. this.y += other.y;
  25. return this;
  26. };
  27. Point.prototype.scale = function (scalar) {
  28. this.x *= scalar;
  29. this.y *= scalar;
  30. };
  31. Point.prototype.scaleAndAdd = function (other, scalar) {
  32. this.x += other.x * scalar;
  33. this.y += other.y * scalar;
  34. };
  35. Point.prototype.sub = function (other) {
  36. this.x -= other.x;
  37. this.y -= other.y;
  38. return this;
  39. };
  40. Point.prototype.dot = function (other) {
  41. return this.x * other.x + this.y * other.y;
  42. };
  43. Point.prototype.len = function () {
  44. return Math.sqrt(this.x * this.x + this.y * this.y);
  45. };
  46. Point.prototype.lenSquare = function () {
  47. return this.x * this.x + this.y * this.y;
  48. };
  49. Point.prototype.normalize = function () {
  50. var len = this.len();
  51. this.x /= len;
  52. this.y /= len;
  53. return this;
  54. };
  55. Point.prototype.distance = function (other) {
  56. var dx = this.x - other.x;
  57. var dy = this.y - other.y;
  58. return Math.sqrt(dx * dx + dy * dy);
  59. };
  60. Point.prototype.distanceSquare = function (other) {
  61. var dx = this.x - other.x;
  62. var dy = this.y - other.y;
  63. return dx * dx + dy * dy;
  64. };
  65. Point.prototype.negate = function () {
  66. this.x = -this.x;
  67. this.y = -this.y;
  68. return this;
  69. };
  70. Point.prototype.transform = function (m) {
  71. if (!m) {
  72. return;
  73. }
  74. var x = this.x;
  75. var y = this.y;
  76. this.x = m[0] * x + m[2] * y + m[4];
  77. this.y = m[1] * x + m[3] * y + m[5];
  78. return this;
  79. };
  80. Point.prototype.toArray = function (out) {
  81. out[0] = this.x;
  82. out[1] = this.y;
  83. return out;
  84. };
  85. Point.prototype.fromArray = function (input) {
  86. this.x = input[0];
  87. this.y = input[1];
  88. };
  89. Point.set = function (p, x, y) {
  90. p.x = x;
  91. p.y = y;
  92. };
  93. Point.copy = function (p, p2) {
  94. p.x = p2.x;
  95. p.y = p2.y;
  96. };
  97. Point.len = function (p) {
  98. return Math.sqrt(p.x * p.x + p.y * p.y);
  99. };
  100. Point.lenSquare = function (p) {
  101. return p.x * p.x + p.y * p.y;
  102. };
  103. Point.dot = function (p0, p1) {
  104. return p0.x * p1.x + p0.y * p1.y;
  105. };
  106. Point.add = function (out, p0, p1) {
  107. out.x = p0.x + p1.x;
  108. out.y = p0.y + p1.y;
  109. };
  110. Point.sub = function (out, p0, p1) {
  111. out.x = p0.x - p1.x;
  112. out.y = p0.y - p1.y;
  113. };
  114. Point.scale = function (out, p0, scalar) {
  115. out.x = p0.x * scalar;
  116. out.y = p0.y * scalar;
  117. };
  118. Point.scaleAndAdd = function (out, p0, p1, scalar) {
  119. out.x = p0.x + p1.x * scalar;
  120. out.y = p0.y + p1.y * scalar;
  121. };
  122. Point.lerp = function (out, p0, p1, t) {
  123. var onet = 1 - t;
  124. out.x = onet * p0.x + t * p1.x;
  125. out.y = onet * p0.y + t * p1.y;
  126. };
  127. return Point;
  128. }());
  129. export default Point;