pointInsideTriangle.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import barycentricCoordinates from "./barycentricCoordinates.js";
  2. import Cartesian3 from "./Cartesian3.js";
  3. import defined from "./defined.js";
  4. const scratchBarycentricCoords = new Cartesian3();
  5. /**
  6. * Determines if a point is inside a triangle.
  7. *
  8. * @function pointInsideTriangle
  9. *
  10. * @param {Cartesian2|Cartesian3} point The point to test.
  11. * @param {Cartesian2|Cartesian3} p0 The first point of the triangle.
  12. * @param {Cartesian2|Cartesian3} p1 The second point of the triangle.
  13. * @param {Cartesian2|Cartesian3} p2 The third point of the triangle.
  14. * @returns {boolean} <code>true</code> if the point is inside the triangle; otherwise, <code>false</code>.
  15. *
  16. * @example
  17. * // Returns true
  18. * const p = new Cesium.Cartesian2(0.25, 0.25);
  19. * const b = Cesium.pointInsideTriangle(p,
  20. * new Cesium.Cartesian2(0.0, 0.0),
  21. * new Cesium.Cartesian2(1.0, 0.0),
  22. * new Cesium.Cartesian2(0.0, 1.0));
  23. */
  24. function pointInsideTriangle(point, p0, p1, p2) {
  25. const coords = barycentricCoordinates(
  26. point,
  27. p0,
  28. p1,
  29. p2,
  30. scratchBarycentricCoords
  31. );
  32. if (!defined(coords)) {
  33. return false;
  34. }
  35. return coords.x > 0.0 && coords.y > 0.0 && coords.z > 0;
  36. }
  37. export default pointInsideTriangle;