/// <summary> /// 计算两条二维线段的交点 /// </summary> /// <param name="other"></param> /// <param name="intersectPoint">线段交点</param> /// <returns></returns> public LineCrossState Intersection(Line2D other, out Vector2 intersectPoint) { intersectPoint.x = intersectPoint.y = float.NaN; if (!UMath.IsLineCross(this, other)) { return(LineCrossState.NOT_CROSS); } double A1, B1, C1, A2, B2, C2; A1 = point2.y - point1.y; B1 = point1.x - point2.x; C1 = point2.x * point1.y - point1.x * point2.y; A2 = other.point2.y - other.point1.y; B2 = other.point1.x - other.point2.x; C2 = other.point2.x * other.point1.y - other.point1.x * other.point2.y; if (UMath.IsEqualZero(A1 * B2 - B1 * A2)) { if (UMath.IsEqualZero((A1 + B1) * C2 - (A2 + B2) * C1)) { return(LineCrossState.COLINE); } else { return(LineCrossState.PARALLEL); } } intersectPoint.x = (float)((B2 * C1 - B1 * C2) / (A2 * B1 - A1 * B2)); intersectPoint.y = (float)((A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2)); return(LineCrossState.CROSS); }
/// <summary> /// 三角形的外接圆 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="p3"></param> /// <returns>Circle</returns> public static Circle TriangleCircumscribedCircle(Triangle triangle) { if (!UMath.IsEqualZero(triangle.point1.y - triangle.point2.y) || !UMath.IsEqualZero(triangle.point2.y - triangle.point3.y)) { double yc, xc; float m1 = -(triangle.point2.x - triangle.point1.x) / (triangle.point2.y - triangle.point1.y); float m2 = -(triangle.point3.x - triangle.point2.x) / (triangle.point3.y - triangle.point2.y); double mx1 = (triangle.point1.x + triangle.point2.x) / 2.0; double mx2 = (triangle.point2.x + triangle.point3.x) / 2.0; double my1 = (triangle.point1.y + triangle.point2.y) / 2.0; double my2 = (triangle.point2.y + triangle.point3.y) / 2.0; if (UMath.IsEqualZero(triangle.point1.y - triangle.point2.y)) { xc = (triangle.point2.x + triangle.point1.x) / 2.0; yc = m2 * (xc - mx2) + my2; } else if (UMath.IsEqualZero(triangle.point3.y - triangle.point2.y)) { xc = (triangle.point3.x + triangle.point2.x) / 2.0; yc = m1 * (xc - mx1) + my1; } else { xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2); yc = m1 * (xc - mx1) + my1; } double dx = triangle.point2.x - xc; double dy = triangle.point2.y - yc; double rsqr = dx * dx + dy * dy; double r = Math.Sqrt(rsqr); return(new Circle(new Vector2((float)xc, (float)yc), (float)r)); } return(new Circle(Vector2.zero, 0)); }
/// <summary> /// 判断点与直线的关系 /// </summary> /// <param name="point">判断点</param> /// <returns></returns> public PointSideRelation ClassifyPoint(Vector2 point) { if (point == m_point1 || point == m_point2) { return(PointSideRelation.ON_LINE); } Vector2 vectorA = m_point2 - m_point1; Vector2 vectorB = point - m_point1; float crossResult = UMath.CrossProduct(vectorA, vectorB); if (UMath.IsEqualZero(crossResult)) { return(PointSideRelation.ON_LINE); } else if (crossResult < 0) { return(PointSideRelation.RIGHT_SIDE); } else { return(PointSideRelation.LEFT_SIDE); } }