/// <summary> /// 计算(R-P)和(Q-P)的叉积 /// </summary> /// <param name="P">点P</param> /// <param name="Q">点Q</param> /// <param name="R">点R</param> /// <returns>返回叉积值</returns> /// <remarks> /// 返回值 大于0 R在矢量PQ的逆时针方向 /// 返回值 等于0 R,P,Q 三点共线 /// 返回值 小于0 R在矢量PQ的顺时针方向 /// </remarks> public static Int32 Multiple(PointI P, PointI Q, PointI R) { PointI RP = PointAlgorithm.Substract(R, P); //R-P PointI QP = PointAlgorithm.Substract(Q, P); //Q-P return(PointAlgorithm.Multiple(RP, QP)); }
/// <summary> /// 计算(R-P)和(Q-P)的叉积 /// </summary> /// <param name="P">点P</param> /// <param name="Q">点Q</param> /// <param name="R">点R</param> /// <returns>返回叉积值</returns> /// <remarks> /// 返回值 大于0 R在矢量PQ的逆时针方向 /// 返回值 等于0 R,P,Q 三点共线 /// 返回值 小于0 R在矢量PQ的顺时针方向 /// </remarks> public static Double Multiple(PointD P, PointD Q, PointD R) { PointD RP = PointAlgorithm.Substract(R, P); //R-P PointD QP = PointAlgorithm.Substract(Q, P); //Q-P return(PointAlgorithm.Multiple(RP, QP)); }
/// <summary> /// 判断折线的偏转方向/线段拐向 /// </summary> /// <param name="P">P点</param> /// <param name="Q">Q点</param> /// <param name="R">R点</param> /// <returns>返回偏转方向</returns> /// <remarks> /// 返回值 大于 0 , 则PQ在R点拐向右侧后得到QR,等同于点R在PQ线段的右侧 /// 返回值 小于 0 , 则PQ在R点拐向左侧后得到QR,等同于点R在PQ线段的左侧 /// 返回值 等于 0 , 则P,Q,R三点共线。 /// </remarks> public static Int32 DeflectingDirection(PointI P, PointI Q, PointI R) { //formular //折线段的拐向判断方法可以直接由矢量叉积的性质推出。对于有公共端点的线段PQ和QR,通过计算(R - P) * (Q - P)的符号便可以确定折线段的拐向 //基本算法是(R-P)计算相对于P点的R点坐标,(Q-P)计算相对于P点的Q点坐标。 //(R-P) * (Q-P)的计算结果是计算以P为相对原点,点R与点Q的顺时针,逆时针方向。 PointI RP = PointAlgorithm.Substract(R, P); //R-P PointI QP = PointAlgorithm.Substract(Q, P); //Q-P return(PointAlgorithm.Multiple(RP, QP)); }
/// <summary> /// 获取线段L与圆C的交点集合 /// </summary> /// <param name="L">线段L</param> /// <param name="C">圆C</param> /// <returns>返回交点集合.</returns> public static PointD[] Intersection(LineI L, CircleI C) { List <PointD> result = new List <PointD>(); Int32? has = HasIntersection(L, C); if (has == 0 || has == null) { return(result.ToArray()); } //Points P (x,y) on a line defined by two points P1 (x1,y1,z1) and P2 (x2,y2,z2) is described by //P = P1 + u (P2 - P1) //or in each coordinate //x = x1 + u (x2 - x1) //y = y1 + u (y2 - y1) //z = z1 + u (z2 - z1) //A sphere centered at P3 (x3,y3,z3) with radius r is described by //(x - x3)2 + (y - y3)2 + (z - z3)2 = r2 //Substituting the equation of the line into the sphere gives a quadratic equation of the form //a u2 + b u + c = 0 //where: //a = (x2 - x1)2 + (y2 - y1)2 + (z2 - z1)2 //b = 2[ (x2 - x1) (x1 - x3) + (y2 - y1) (y1 - y3) + (z2 - z1) (z1 - z3) ] //c = x32 + y32 + z32 + x12 + y12 + z12 - 2[x3 x1 + y3 y1 + z3 z1] - r2 //The solutions to this quadratic are described by PointD PD = PointAlgorithm.Substract(L.Starting, L.End); Double a = PD.X * PD.X + PD.Y * PD.Y; Double b = 2 * ((L.End.X - L.Starting.X) * (L.Starting.X - C.Center.X) + (L.End.Y - L.Starting.Y) * (L.Starting.Y - C.Center.Y)); Double c = C.Center.X * C.Center.X + C.Center.Y * C.Center.Y + L.Starting.X * L.Starting.X + L.Starting.Y * L.Starting.Y - 2 * (C.Center.X * L.Starting.X + C.Center.Y * L.Starting.Y) - C.Radius * C.Radius; Double u1 = ((-1) * b + System.Math.Sqrt(b * b - 4 * a * c)) / (2 * a); Double u2 = ((-1) * b - System.Math.Sqrt(b * b - 4 * a * c)) / (2 * a); //交点 PointD P1 = new PointD(L.Starting.X + u1 * (L.End.X - L.Starting.X), L.Starting.Y + u1 * (L.End.Y - L.Starting.Y)); PointD P2 = new PointD(L.Starting.X + u2 * (L.End.X - L.Starting.X), L.Starting.Y + u2 * (L.End.Y - L.Starting.Y)); if (LineAlgorithm.OnLine(L, P1) == true) { result.Add(P1); } if (LineAlgorithm.OnLine(L, P2) == true && P1.Equals(P2) == false) { result.Add(P2); } return(result.ToArray()); }