/// <summary> /// 判断点point是否在矩形内 /// </summary> public static bool IsPointInRectangle(Vector3 point, Vector3 rectA, Vector3 rectC, Vector3 rectB, Vector3 rectD) { Vector3 vector = rectC - rectA; float size = -(vector.magnitude / 2f); vector = Math3d.AddVectorLength(vector, size); Vector3 linePoint = rectA + vector; Vector3 vector2 = rectB - rectA; float num = vector2.magnitude / 2f; Vector3 vector3 = rectD - rectA; float num2 = vector3.magnitude / 2f; Vector3 a = Math3d.ProjectPointOnLine(linePoint, vector2.normalized, point); float magnitude = (a - point).magnitude; a = Math3d.ProjectPointOnLine(linePoint, vector3.normalized, point); float magnitude2 = (a - point).magnitude; return(magnitude2 <= num && magnitude <= num2); }
/// <summary> /// 一个点在一条线段上的投影点 /// </summary> /// <param name="linePoint1"></param> /// <param name="linePoint2"></param> /// <param name="point"></param> /// <returns></returns> public static Vector3 ProjectPointOnLineSegment(Vector3 linePoint1, Vector3 linePoint2, Vector3 point) { Vector3 vector = Math3d.ProjectPointOnLine(linePoint1, (linePoint2 - linePoint1).normalized, point); int num = Math3d.PointOnWhichSideOfLineSegment(linePoint1, linePoint2, vector); if (num == 0) { return(vector); } if (num == 1) { return(linePoint1); } if (num == 2) { return(linePoint2); } return(Vector3.zero); }