misc.ts 997 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export const halfPI = Math.PI / 2;
  7. export function degToRad (deg: number) {
  8. return deg * 0.01745; // deg * Math.PI / 180
  9. }
  10. export function radToDeg (rad: number) {
  11. return rad * 57.29578; // rad * 180 / Math.PI
  12. }
  13. export function isPowerOfTwo (x: number) {
  14. return (x !== 0) && (x & (x - 1)) === 0;
  15. }
  16. /** return the value that has the largest absolute value */
  17. export function absMax(...values: number[]) {
  18. let max = 0;
  19. let absMax = 0;
  20. for (let i = 0, il = values.length; i < il; ++i) {
  21. const value = values[i];
  22. const abs = Math.abs(value);
  23. if (abs > absMax) {
  24. max = value;
  25. absMax = abs;
  26. }
  27. }
  28. return max;
  29. }
  30. /** Length of an arc with angle in radians */
  31. export function arcLength(angle: number, radius: number) {
  32. return angle * radius;
  33. }