vector.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. export function create(x, y) {
  2. if (x == null) {
  3. x = 0;
  4. }
  5. if (y == null) {
  6. y = 0;
  7. }
  8. return [x, y];
  9. }
  10. export function copy(out, v) {
  11. out[0] = v[0];
  12. out[1] = v[1];
  13. return out;
  14. }
  15. export function clone(v) {
  16. return [v[0], v[1]];
  17. }
  18. export function set(out, a, b) {
  19. out[0] = a;
  20. out[1] = b;
  21. return out;
  22. }
  23. export function add(out, v1, v2) {
  24. out[0] = v1[0] + v2[0];
  25. out[1] = v1[1] + v2[1];
  26. return out;
  27. }
  28. export function scaleAndAdd(out, v1, v2, a) {
  29. out[0] = v1[0] + v2[0] * a;
  30. out[1] = v1[1] + v2[1] * a;
  31. return out;
  32. }
  33. export function sub(out, v1, v2) {
  34. out[0] = v1[0] - v2[0];
  35. out[1] = v1[1] - v2[1];
  36. return out;
  37. }
  38. export function len(v) {
  39. return Math.sqrt(lenSquare(v));
  40. }
  41. export var length = len;
  42. export function lenSquare(v) {
  43. return v[0] * v[0] + v[1] * v[1];
  44. }
  45. export var lengthSquare = lenSquare;
  46. export function mul(out, v1, v2) {
  47. out[0] = v1[0] * v2[0];
  48. out[1] = v1[1] * v2[1];
  49. return out;
  50. }
  51. export function div(out, v1, v2) {
  52. out[0] = v1[0] / v2[0];
  53. out[1] = v1[1] / v2[1];
  54. return out;
  55. }
  56. export function dot(v1, v2) {
  57. return v1[0] * v2[0] + v1[1] * v2[1];
  58. }
  59. export function scale(out, v, s) {
  60. out[0] = v[0] * s;
  61. out[1] = v[1] * s;
  62. return out;
  63. }
  64. export function normalize(out, v) {
  65. var d = len(v);
  66. if (d === 0) {
  67. out[0] = 0;
  68. out[1] = 0;
  69. }
  70. else {
  71. out[0] = v[0] / d;
  72. out[1] = v[1] / d;
  73. }
  74. return out;
  75. }
  76. export function distance(v1, v2) {
  77. return Math.sqrt((v1[0] - v2[0]) * (v1[0] - v2[0])
  78. + (v1[1] - v2[1]) * (v1[1] - v2[1]));
  79. }
  80. export var dist = distance;
  81. export function distanceSquare(v1, v2) {
  82. return (v1[0] - v2[0]) * (v1[0] - v2[0])
  83. + (v1[1] - v2[1]) * (v1[1] - v2[1]);
  84. }
  85. export var distSquare = distanceSquare;
  86. export function negate(out, v) {
  87. out[0] = -v[0];
  88. out[1] = -v[1];
  89. return out;
  90. }
  91. export function lerp(out, v1, v2, t) {
  92. out[0] = v1[0] + t * (v2[0] - v1[0]);
  93. out[1] = v1[1] + t * (v2[1] - v1[1]);
  94. return out;
  95. }
  96. export function applyTransform(out, v, m) {
  97. var x = v[0];
  98. var y = v[1];
  99. out[0] = m[0] * x + m[2] * y + m[4];
  100. out[1] = m[1] * x + m[3] * y + m[5];
  101. return out;
  102. }
  103. export function min(out, v1, v2) {
  104. out[0] = Math.min(v1[0], v2[0]);
  105. out[1] = Math.min(v1[1], v2[1]);
  106. return out;
  107. }
  108. export function max(out, v1, v2) {
  109. out[0] = Math.max(v1[0], v2[0]);
  110. out[1] = Math.max(v1[1], v2[1]);
  111. return out;
  112. }