| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import { Vec2, Vec3 } from "cc";
- /**
- * 几何工具类
- * @author 袁浩、王锡铜
- */
- export class GeometryUtils {
- /**
- * Vec2转Vec3
- * @param v2 2D坐标
- * @returns
- */
- public static V2ToV3(v2: Vec2): Vec3 {
- return new Vec3(v2.x, v2.y, 0);
- }
- /**
- * Vec3转Vec2
- * @param v2 2D坐标
- * @returns
- */
- public static V3ToV2(v3: Vec3): Vec2 {
- return new Vec2(v3.x, v3.y);
- }
- /**
- * 对值进行约束
- * @returns
- */
- public static clamp(value: number, min: number, max: number): number {
- if (value < min) {
- return min;
- } else if (value > max) {
- return max;
- } else {
- return value
- }
- }
- /**
- * 判断一个点是否在多边形内
- * @param point 点的坐标
- * @param polyPoints 多边形所有顶点的坐标
- * @returns
- */
- public static pointInPoly(point: Vec2 | Vec3, polyPoints: Vec2[]) {
- for (var c = false, i = -1, l = polyPoints.length, j = l - 1; ++i < l; j = i)
- ((polyPoints[i].y <= point.y && point.y < polyPoints[j].y) || (polyPoints[j].y <= point.y && point.y < polyPoints[i].y))
- && (point.x < (polyPoints[j].x - polyPoints[i].x) * (point.y - polyPoints[i].y) / (polyPoints[j].y - polyPoints[i].y) + polyPoints[i].x)
- && (c = !c);
- return c;
- }
- }
|