GeometryUtils.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Vec2, Vec3 } from "cc";
  2. /**
  3. * 几何工具类
  4. * @author 袁浩、王锡铜
  5. */
  6. export class GeometryUtils {
  7. /**
  8. * Vec2转Vec3
  9. * @param v2 2D坐标
  10. * @returns
  11. */
  12. public static V2ToV3(v2: Vec2): Vec3 {
  13. return new Vec3(v2.x, v2.y, 0);
  14. }
  15. /**
  16. * Vec3转Vec2
  17. * @param v2 2D坐标
  18. * @returns
  19. */
  20. public static V3ToV2(v3: Vec3): Vec2 {
  21. return new Vec2(v3.x, v3.y);
  22. }
  23. /**
  24. * 对值进行约束
  25. * @returns
  26. */
  27. public static clamp(value: number, min: number, max: number): number {
  28. if (value < min) {
  29. return min;
  30. } else if (value > max) {
  31. return max;
  32. } else {
  33. return value
  34. }
  35. }
  36. /**
  37. * 判断一个点是否在多边形内
  38. * @param point 点的坐标
  39. * @param polyPoints 多边形所有顶点的坐标
  40. * @returns
  41. */
  42. public static pointInPoly(point: Vec2 | Vec3, polyPoints: Vec2[]) {
  43. for (var c = false, i = -1, l = polyPoints.length, j = l - 1; ++i < l; j = i)
  44. ((polyPoints[i].y <= point.y && point.y < polyPoints[j].y) || (polyPoints[j].y <= point.y && point.y < polyPoints[i].y))
  45. && (point.x < (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x)
  46. && (c = !c);
  47. return c;
  48. }
  49. }