vec2.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. /*
  7. * This code has been modified from https://github.com/toji/gl-matrix/,
  8. * copyright (c) 2015, Brandon Jones, Colin MacKenzie IV.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. */
  17. interface Vec2 extends Array<number> { [d: number]: number, '@type': 'vec2', length: 2 }
  18. namespace Vec2 {
  19. export function zero(): Vec2 {
  20. // force double backing array by 0.1.
  21. const ret = [0.1, 0];
  22. ret[0] = 0.0;
  23. return ret as any;
  24. }
  25. export function clone(a: Vec2) {
  26. const out = zero();
  27. out[0] = a[0];
  28. out[1] = a[1];
  29. return out;
  30. }
  31. export function create(x: number, y: number) {
  32. const out = zero();
  33. out[0] = x;
  34. out[1] = y;
  35. return out;
  36. }
  37. export function toArray(a: Vec2, out: Helpers.NumberArray, offset: number) {
  38. out[offset + 0] = a[0];
  39. out[offset + 1] = a[1];
  40. }
  41. export function fromArray(a: Vec2, array: Helpers.NumberArray, offset: number) {
  42. a[0] = array[offset + 0]
  43. a[1] = array[offset + 1]
  44. return a
  45. }
  46. export function copy(out: Vec2, a: Vec2) {
  47. out[0] = a[0];
  48. out[1] = a[1];
  49. return out;
  50. }
  51. export function set(out: Vec2, x: number, y: number) {
  52. out[0] = x;
  53. out[1] = y;
  54. return out;
  55. }
  56. export function add(out: Vec2, a: Vec2, b: Vec2) {
  57. out[0] = a[0] + b[0];
  58. out[1] = a[1] + b[1];
  59. return out;
  60. }
  61. export function sub(out: Vec2, a: Vec2, b: Vec2) {
  62. out[0] = a[0] - b[0];
  63. out[1] = a[1] - b[1];
  64. return out;
  65. }
  66. export function mul(out: Vec2, a: Vec2, b: Vec2) {
  67. out[0] = a[0] * b[0];
  68. out[1] = a[1] * b[1];
  69. return out;
  70. }
  71. export function div(out: Vec2, a: Vec2, b: Vec2) {
  72. out[0] = a[0] / b[0];
  73. out[1] = a[1] / b[1];
  74. return out;
  75. }
  76. export function scale(out: Vec2, a: Vec2, b: number) {
  77. out[0] = a[0] * b;
  78. out[1] = a[1] * b;
  79. return out;
  80. }
  81. /**
  82. * Math.round the components of a Vec2
  83. */
  84. export function round(out: Vec2, a: Vec2) {
  85. out[0] = Math.round(a[0]);
  86. out[1] = Math.round(a[1]);
  87. return out;
  88. }
  89. /**
  90. * Math.ceil the components of a Vec2
  91. */
  92. export function ceil(out: Vec2, a: Vec2) {
  93. out[0] = Math.ceil(a[0]);
  94. out[1] = Math.ceil(a[1]);
  95. return out;
  96. }
  97. /**
  98. * Math.floor the components of a Vec2
  99. */
  100. export function floor(out: Vec2, a: Vec2) {
  101. out[0] = Math.floor(a[0]);
  102. out[1] = Math.floor(a[1]);
  103. return out;
  104. }
  105. export function distance(a: Vec2, b: Vec2) {
  106. const x = b[0] - a[0],
  107. y = b[1] - a[1];
  108. return Math.sqrt(x * x + y * y);
  109. }
  110. export function squaredDistance(a: Vec2, b: Vec2) {
  111. const x = b[0] - a[0],
  112. y = b[1] - a[1];
  113. return x * x + y * y;
  114. }
  115. export function magnitude(a: Vec2) {
  116. const x = a[0],
  117. y = a[1];
  118. return Math.sqrt(x * x + y * y);
  119. }
  120. export function squaredMagnitude(a: Vec2) {
  121. const x = a[0],
  122. y = a[1];
  123. return x * x + y * y;
  124. }
  125. /**
  126. * Returns the inverse of the components of a Vec2
  127. */
  128. export function inverse(out: Vec2, a: Vec2) {
  129. out[0] = 1.0 / a[0];
  130. out[1] = 1.0 / a[1];
  131. return out;
  132. }
  133. }
  134. export default Vec2