Exemplo n.º 1
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.º 2
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);
        }