Exemplo n.º 1
0
        /// <summary>
        /// 获取有效的多边形
        /// </summary>
        /// <param name="PG">多边形PG</param>
        /// <returns>返回多边形</returns>
        public static PolygonD?GetValidatePolygon(PolygonD PG)
        {
            List <PointD> pts = new List <PointD>();

            for (Int32 i = 0; i < PG.Vertex.Count; ++i)
            {
                if (pts.Contains(PG.Vertex[i]) == false)
                {
                    if (pts.Count > 2)
                    {
                        //斜率相等
                        if (LineAlgorithm.Gradient(new LineD(pts[pts.Count - 2], pts[pts.Count - 1])) == LineAlgorithm.Gradient(new LineD(pts[pts.Count - 1], PG.Vertex[i])))
                        {
                            pts[pts.Count - 1] = PG.Vertex[i];
                        }
                        else
                        {
                            pts.Add(PG.Vertex[i]);
                        }
                    }
                }
            }
            if (pts.Count < 3)
            {
                return(null);
            }
            return(new PolygonD(pts.ToArray()));
        }
Exemplo n.º 2
0
 /// <summary>
 /// 判断是否为凸多边形
 /// </summary>
 /// <param name="PG">PG多边形</param>
 /// <returns>如果是凸多边形返回True,否则返回False。</returns>
 public static Boolean IsConvexPolygon(PolygonI PG)
 {
     if (PG.Vertex == null)
     {
         return(false);
     }
     if (PG.Vertex.Count < 4)
     {
         return(true);
     }
     for (Int32 i = 0; i < PG.Vertex.Count; ++i)
     {
         LineD  line = new LineD(PG.Vertex[i], PG.Vertex[(i + 1) % PG.Vertex.Count]);
         Double flag = 0;
         for (Int32 j = 0; j < PG.Vertex.Count; ++j)
         {
             Double result = LineAlgorithm.Position(line, PG.Vertex[j]);
             if ((flag * result) < 0)//点在线的两侧则返回失败。
             {
                 return(false);
             }
             if (result != 0)
             {
                 flag = result;
             }
         }
     }
     return(true);
 }
Exemplo n.º 3
0
        /// <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());
        }
Exemplo n.º 4
0
        /// <summary>
        /// 点P是否在多边形区域内
        /// </summary>
        /// <param name="PG">多边形PL</param>
        /// <param name="P">点P</param>
        /// <returns>如果点P在区域内返回True,否则返回False.</returns>
        /// <remarks>此处不考虑平行边的情况</remarks>
        public static Boolean InPolygon(PolygonI PG, PointI P)
        {
            //count ← 0;
            //以P为端点,作从右向左的射线L;
            //for 多边形的每条边s
            // do if P在边s上
            //      then return true;
            //    if s不是水平的
            //      then if s的一个端点在L上
            //             if 该端点是s两端点中纵坐标较大的端点
            //               then count ← count+1
            //           else if s和L相交
            //             then count ← count+1;
            //if count mod 2 = 1
            //  then return true;
            //else return false;
            Int32 count = 0;
            LineI L     = new LineI(P, new PointI(Int32.MaxValue, P.Y));

            foreach (LineI S in PG)
            {
                if (LineAlgorithm.OnLine(S, P) == true)
                {
                    return(true);
                }
                if (LineAlgorithm.Gradient(S) != 0)//不是水平线段
                {
                    if (LineAlgorithm.OnLine(L, S.Starting) || LineAlgorithm.OnLine(L, S.End))
                    {
                        if (LineAlgorithm.OnLine(L, S.Starting) && S.Starting.Y > S.End.Y)
                        {
                            ++count;
                        }
                        if (LineAlgorithm.OnLine(L, S.End) && S.End.Y > S.Starting.Y)
                        {
                            ++count;
                        }
                    }
                    else if (LineAlgorithm.HasIntersection(S, L) > 0)
                    {
                        ++count;
                    }
                }
            }
            //如果X的水平射线和多边形的交点数是奇数个则点在多边形内,否则不在区域内。
            if (count % 2 == 0)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 根据点P,Q,R三点确定一个圆,注意三点不能共线
        /// </summary>
        /// <param name="P">点P</param>
        /// <param name="Q">点Q</param>
        /// <param name="R">点R</param>
        /// <returns>返回圆,如果圆不存在则返回null.</returns>
        public static CircleD?CreateCircle(PointI P, PointI Q, PointI R)
        {
            if (DoubleAlgorithm.Equals(LineAlgorithm.Position(P, Q, R), 0))
            {
                return(null);                                                          //三点共线无法确定圆
            }
            //formula
            //(x-a)^2+(y-b)^2=r^2
            //f1:(x1-a)^2 + (y1-b)^2 = r^2
            //f2:(x2-a)^2 + (y2-b)^2 = r^2
            //f3:(x3-a)^2 + (y3-b)^2 = r^2
            //f1=f2: x1^2-2ax1+y1^2-2by1= x2^2-2ax2+y2^2-2by2;
            //a=(((X(1)^2-X(2)^2+Y(1)^2-Y(2)^2)*(Y(2)-Y(3)))-((X(2)^2-X(3)^2+Y(2)^2-Y(3)^2)*(Y(1)-Y(2))))/(2*(X(1)-X(2))*(Y(2)-Y(3))-2*(X(2)-X(3))*(Y(1)-Y(2)))
            //b=(((X(1)^2-X(2)^2+Y(1)^2-Y(2)^2)*(X(2)-X(3)))-((X(2)^2-X(3)^2+Y(2)^2-Y(3)^2)*(X(1)-X(2))))/(2*(Y(1)-Y(2))*(X(2)-X(3))-2*(Y(2)-Y(3))*(X(1)-X(2)))
            Double a = (Double)(((P.X * P.X - Q.X * Q.X + P.Y * P.Y - Q.Y * Q.Y) * (Q.Y - R.Y)) - ((Q.X * Q.X - R.X * R.X + Q.Y * Q.Y - R.Y * R.Y) * (P.Y - Q.Y))) / (Double)(2 * (P.X - Q.X) * (Q.Y - R.Y) - 2 * (Q.X - R.X) * (P.Y - Q.Y));
            Double b = (Double)(((P.X * P.X - Q.X * Q.X + P.Y * P.Y - Q.Y * Q.Y) * (Q.X - R.X)) - ((Q.X * Q.X - R.X * R.X + Q.Y * Q.Y - R.Y * R.Y) * (P.X - Q.X))) / (Double)(2 * (P.Y - Q.Y) * (Q.X - R.X) - 2 * (Q.Y - R.Y) * (P.X - Q.X));
            Double r = PointAlgorithm.Distance(P, new PointD(a, b));

            return(new CircleD(a, b, r));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 线段L是否在多边形区域内
        /// </summary>
        /// <param name="PG">多边形PG</param>
        /// <param name="L">线段L</param>
        /// <returns>如果线段L在区域内返回True,否则返回False.</returns>
        public static Boolean InPolygon(PolygonI PG, LineI L)
        {
            //if 线端PQ的端点不都在多边形内
            //  then return false;
            //点集pointSet初始化为空;
            //for 多边形的每条边s
            //  do if 线段的某个端点在s上
            //       then 将该端点加入pointSet;
            //     else if s的某个端点在线段PQ上
            //       then 将该端点加入pointSet;
            //     else if s和线段PQ相交 // 这时候已经可以肯定是内交了
            //       then return false;
            //将pointSet中的点按照X-Y坐标排序;
            //for pointSet中每两个相邻点 pointSet[i] , pointSet[ i+1]
            //  do if pointSet[i] , pointSet[ i+1] 的中点不在多边形中
            //       then return false;
            //return true;
            List <PointI> PointList = new List <PointI>();

            if (InPolygon(PG, L.Starting) == false)
            {
                return(false);
            }
            foreach (LineI S in PG)
            {
                if (LineAlgorithm.OnLine(S, L.Starting) == true)
                {
                    PointList.Add(L.Starting);
                }
                else if (LineAlgorithm.OnLine(S, L.End) == true)
                {
                    PointList.Add(L.End);
                }
                else if (LineAlgorithm.OnLine(L, S.Starting) == true)
                {
                    PointList.Add(S.Starting);
                }
                else if (LineAlgorithm.OnLine(L, S.End) == true)
                {
                    PointList.Add(S.End);
                }
                else if (LineAlgorithm.HasIntersection(L, S) > 0)
                {
                    return(false);
                }
            }
            PointI[] OrderedPointList = PointList.ToArray();
            for (Int32 i = 0; i < (OrderedPointList.Length - 1); ++i)
            {
                for (Int32 j = 0; j < (OrderedPointList.Length - i - 1); j++)
                {
                    MinMax <PointI> MM = new MinMax <PointI>(OrderedPointList[j], OrderedPointList[j + 1]);
                    OrderedPointList[j]     = MM.Min;
                    OrderedPointList[j + 1] = MM.Max;
                }
            }
            for (Int32 i = 0; i < (OrderedPointList.Length - 1); ++i)
            {
                if (false == InPolygon(PG, PointAlgorithm.MidPoint(OrderedPointList[i], OrderedPointList[i + 1])))
                {
                    return(false);
                }
            }
            return(true);
        }