vec2.ts 4.4 KB

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