/// <summary> /// 判断点P是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="P">点P</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleI C, PointI P) { //判断点是否在圆内: //计算圆心到该点的距离,如果小于等于半径则该点在圆内。 Double D = PointAlgorithm.Distance(P, C.Center); return((D < C.Radius) || DoubleAlgorithm.Equals(D, C.Radius)); }
/// <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()); }
/// <summary> /// 判断折线PL是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="R">矩形R</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleI C, RectangleI R) { if (PointAlgorithm.Distance(new PointI(R.Left, R.Top), C.Center) > C.Radius) { return(false); } if (PointAlgorithm.Distance(new PointI(R.Right, R.Bottom), C.Center) > C.Radius) { return(false); } return(true); }
/// <summary> /// 判断线段L是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="L">线段L</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleI C, LineI L) { //判断点是否在圆内: //计算圆心到该点的距离,如果小于等于半径则该点在圆内。 if (PointAlgorithm.Distance(L.Starting, C.Center) > C.Radius) { return(false); } if (PointAlgorithm.Distance(L.End, C.Center) > C.Radius) { return(false); } return(true); }
/// <summary> /// 判断圆C是否在多边形PG内 /// </summary> /// <param name="PG">PG多边形</param> /// <param name="C">圆C</param> /// <returns>如果圆C在区域内返回True,否则返回False.</returns> public static Boolean InPolygon(PolygonI PG, CircleI C) { //如果圆心不在多边形内则返回不在多边形内 if (false == InPolygon(PG, C.Center)) { return(false); } Double D = PointAlgorithm.ClosestDistance(C.Center, PG); if (D > C.Radius || DoubleAlgorithm.Equals(D, C.Radius)) { return(true); } return(false); }
/// <summary> /// 判断折线PL是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="PL">折线PL</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleI C, PolylineI PL) { if (PL.Points == null) { return(false); } for (Int32 i = 0; i < PL.Points.Count; ++i) { if (PointAlgorithm.Distance(PL.Points[i], C.Center) > C.Radius) { return(false); } } return(true); }
/// <summary> /// 判断多边形PG是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="PG">多边形PG</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleI C, PolygonI PG) { if (PG.Vertex == null) { return(false); } for (Int32 i = 0; i < PG.Vertex.Count; ++i) { if (PointAlgorithm.Distance(PG.Vertex[i], C.Center) > C.Radius) { return(false); } } return(true); }
/// <summary> /// 圆形是否在矩形内 /// </summary> /// <param name="R">矩形R</param> /// <param name="C">圆形C</param> /// <returns> 返回True表示圆形C在区域内,返回False则不在区域内.</returns> public static Boolean InRectangle(RectangleI R, CircleI C) { //很容易证明,圆在矩形中的充要条件是:圆心在矩形中且圆的半径小于等于圆心到矩形四边的距离的最小值。 if (InRectangle(R, C.Center) == false) { return(false); } Int32 MinXDistance = System.Math.Min((C.X - R.Left), (R.Right - C.X)); Int32 MinYDistance = System.Math.Min((C.Y - R.Top), (R.Bottom - C.Y)); if (C.Radius <= MinXDistance && C.Radius <= MinYDistance) { return(true); } return(false); }
/// <summary> /// 判断线段L与圆C的交点个数 /// </summary> /// <param name="L">线段L</param> /// <param name="C">圆形C</param> /// <returns>相交返回交点数目,否则返回0</returns> public static Int32?HasIntersection(LineI L, CircleI C) { Int32 count = 0; //如果和圆C有交点首先是L到圆心的距离小于或等于C的半径 if (DoubleAlgorithm.Equals(PointAlgorithm.ClosestDistance(C.Center, L), C.Radius)) { return(1); } else if (PointAlgorithm.ClosestDistance(C.Center, L) > C.Radius) { return(0); } if (PointAlgorithm.Distance(C.Center, L.Starting) >= C.Radius) { ++count; } if (PointAlgorithm.Distance(C.Center, L.End) >= C.Radius) { ++count; } return(count); }
/// <summary> /// 计算圆形的偏移 /// </summary> /// <param name="C">圆形C</param> /// <param name="velocity">偏移速度。</param> /// <returns>返回偏移后的圆形。</returns> public static CircleI Offset(CircleI C, PointI velocity) { return(new CircleI(C.Center + velocity, C.Radius)); }
/// <summary> /// 计算圆形面积 /// </summary> /// <param name="C">圆形C</param> /// <returns>返回面积。</returns> public static Double Area(CircleI C) { //formula PI*R*R return(System.Math.PI * C.Radius * C.Radius); }
/// <summary> /// 判断点P是否在圆边界上 /// </summary> /// <param name="C">圆C</param> /// <param name="P">点P</param> /// <returns>如果在圆边界上返回True,否则返回False。</returns> public static Boolean OnCircle(CircleI C, PointI P) { return(DoubleAlgorithm.Equals(Distance(C, P), 0)); }
/// <summary> /// 计算线L到圆C的距离 /// </summary> /// <param name="C">圆C</param> /// <param name="L">线L</param> /// <returns>返回线到圆周的距离。</returns> /// <remarks> /// 返回值小于0 表示线在圆内或与圆周相交。 /// 返回值等于0 表示线在圆周上与圆周相切。 /// 返回值大于0 表示线在圆外与圆周没有交点。 /// </remarks> public static Double Distance(CircleI C, LineI L) { return(PointAlgorithm.Distance(C.Center, L) - C.Radius); }
/// <summary> /// 计算点到圆的距离 /// </summary> /// <param name="C">圆C</param> /// <param name="P">点P</param> /// <returns>返回点到圆周的距离。</returns> /// <remarks> /// 返回值小于0 表示点在圆内。 /// 返回值等于0 表示点在圆周上。 /// 返回值大于0 表示点在圆外。 /// </remarks> public static Double Distance(CircleI C, PointI P) { return(PointAlgorithm.Distance(C.Center, P) - C.Radius); }
/// <summary> /// 判断圆C2是否在圆C1内 /// </summary> /// <param name="C1">圆C1</param> /// <param name="C2">圆C2</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleI C1, CircleI C2) { //formula //C2的中心点到C1中心点的距离 加上C2的半径小于C1的半径 return(((PointAlgorithm.Distance(C1.Center, C2.Center) + C2.Radius) > C1.Radius) ? false : true); }