You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hitTests.ts 1015B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Bounds } from "types"
  2. import * as vec from "./vec"
  3. /**
  4. * Get whether a point is inside of a bounds.
  5. * @param A
  6. * @param b
  7. * @returns
  8. */
  9. export function pointInBounds(A: number[], b: Bounds) {
  10. return !(A[0] < b.minX || A[0] > b.maxX || A[1] < b.minY || A[1] > b.maxY)
  11. }
  12. /**
  13. * Get whether a point is inside of a circle.
  14. * @param A
  15. * @param b
  16. * @returns
  17. */
  18. export function pointInCircle(A: number[], C: number[], r: number) {
  19. return vec.dist(A, C) <= r
  20. }
  21. /**
  22. * Get whether a point is inside of an ellipse.
  23. * @param point
  24. * @param center
  25. * @param rx
  26. * @param ry
  27. * @param rotation
  28. * @returns
  29. */
  30. export function pointInEllipse(
  31. A: number[],
  32. C: number[],
  33. rx: number,
  34. ry: number,
  35. rotation = 0
  36. ) {
  37. rotation = rotation || 0
  38. const cos = Math.cos(rotation)
  39. const sin = Math.sin(rotation)
  40. const delta = vec.sub(A, C)
  41. const tdx = cos * delta[0] + sin * delta[1]
  42. const tdy = sin * delta[0] - cos * delta[1]
  43. return (tdx * tdx) / (rx * rx) + (tdy * tdy) / (ry * ry) <= 1
  44. }