Example #1
0
        /// <summary>
        /// 获取离边界最近的一条边(边可以不再边界上)
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="border"></param>
        /// <returns></returns>
        public static Vector getNearestVector(Dot2 origin, Border border)
        {
            if (border.DotCount < 2)
            {
                return(null);
            }
            int    j1 = 0, j2 = 1;
            double lengh1 = Math.Length(origin, border.get(j1)), lengh2 = Math.Length(origin, border.get(j2)), temp;

            if (lengh1 > lengh2)
            {
                lengh1 = lengh1 + lengh2;
                lengh2 = lengh1 - lengh2;
                lengh1 = lengh1 - lengh2;
            }
            for (int i = 1; i < border.DotCount; i++)
            {
                temp = Math.Length(origin, border.get(i));
                if (temp <= lengh1)
                {
                    lengh2 = lengh1;
                    lengh1 = temp;
                    j2     = j1;
                    j1     = i;
                }
                else if (temp > lengh1 && temp <= lengh2)
                {
                    lengh2 = temp;
                    j2     = i;
                }
            }
            return(new Vector(border.get(j1), border.get(j2)));
        }
Example #2
0
        /// <summary>
        /// 判断点是否在边界内
        /// </summary>
        /// <param name="dot"></param>
        /// <param name="border"></param>
        /// <returns></returns>
        public static bool isInside(Dot2 dot, Border border)
        {
            int num = 0;

            //double minx, maxx;
            for (int i = 0; i < border.DotCount - 1; i++)
            {
                if (dot.X <= (System.Math.Max(border.get(i).X, border.get(i + 1).X)) && (border.get(i).Y - dot.Y) * (border.get(i + 1).Y - dot.Y) < 0)
                {
                    if (dot.X > System.Math.Min(border.get(i).X, border.get(i + 1).X))
                    {
                        if ((border.get(i).X *border.get(i + 1).Y - border.get(i).Y *border.get(i + 1).X + dot.Y * (border.get(i + 1).X - border.get(i).X)) / (border.get(i + 1).Y - border.get(i).Y) > dot.X)
                        {
                            num++;
                        }
                    }
                    else
                    {
                        num++;
                    }
                }
            }
            if (num % 2 == 1)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #3
0
        /// <summary>
        /// 将增加长度最小的点插入
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="border"></param>
        public static void putMinLengthDotToBorder(Dot2 origin, Border border)
        {
            double temp, lengh = double.MaxValue;

            if (border.DotCount <= 2)
            {
                border.Add(origin);
                return;
            }
            int j = 0;

            for (int i = 0; i < border.DotCount; i++)
            {
                temp = Math.Length(origin, border.get(i)) + Math.Length(origin, border.get(i + 1)) - border.getVecter(i).Length;
                if (temp < lengh)
                {
                    lengh = temp;
                    j     = i;
                }
            }
            border.Insert(origin, j + 1, DrawType.Ignore);
        }
Example #4
0
        /// <summary>
        /// 将一个点插入到边界中,形成凸面体
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="border"></param>
        /// <returns></returns>
        public static void putDotToBorder(Dot2 origin, Border border)
        {
            Dot2 dot;
            int  i, j = 0, count = border.DotCount, tempj;

            if (count <= 2)
            {
                border.Add(origin);
                return;
            }
            double temp, lengh = double.MaxValue;

            int isIntersects = 0;

            for (i = 0; i < count; i++)
            {
                temp = Math.Length(origin, border.get(i));
                if (temp < lengh)
                {
                    lengh = temp;
                    j     = i;
                }
            }

            //判断是否相交
            tempj = count + j;
            for (i = 0; i < count; i++)
            {
                if (i == tempj % count || i == (tempj - 1) % count)
                {
                    continue;
                }
                if (isIntersect(new Vector(origin, border.get(tempj)), border.getVecter(i), out dot))
                {
                    isIntersects = 1;
                    break;
                }
            }
            if (isIntersects == 1)
            {
                isIntersects = 4;
                lengh        = double.MaxValue;
                //采用面积最小方式
                for (i = 0; i < border.DotCount; i++)
                {
                    temp = Math.Area(origin, border.get(i), border.get((i + 1)));
                    if (temp < lengh)
                    {
                        lengh = temp;
                        j     = i;
                    }
                }
                //判断是否相交,若相交,取最近的相交的线段插入
                temp  = double.MaxValue;
                tempj = count + j;
                for (i = 0; i < count; i++)
                {
                    if (i == tempj % count || i == (tempj - 1) % count)
                    {
                        continue;
                    }
                    if (isIntersect(new Vector(origin, border.get(tempj)), border.getVecter(i), out dot))
                    {
                        temp = Math.Length(origin, dot);
                        if (temp < lengh)
                        {
                            isIntersects = 5;
                            lengh        = temp;
                            j            = i;
                        }
                    }
                }
                //if (isIntersects == 5)
                //{

                //}
                //tempj = count + i+1;
                //for (i = 0; i < count; i++)
                //{
                //    if (i == tempj % count || i == (tempj - 1) % count)
                //    {
                //        continue;
                //    }
                //    if (isIntersect(new Vector(origin, border.get(tempj)), border.getVecter(i), out dot))
                //    {
                //        isIntersects = 5;
                //        break;
                //    }
                //}
            }
            else
            {
                tempj = count + j - 1;
                for (i = 0; i < count; i++)
                {
                    if (i == tempj % count || i == (tempj - 1) % count)
                    {
                        continue;
                    }
                    if (isIntersect(new Vector(origin, border.get(tempj)), border.getVecter(i), out dot))
                    {
                        isIntersects = 1;
                        break;
                    }
                }
                tempj = count + j + 1;
                for (i = 0; i < count; i++)
                {
                    if (i == tempj % count || i == (tempj - 1) % count)
                    {
                        continue;
                    }
                    if (isIntersect(new Vector(origin, border.get(tempj)), border.getVecter(i), out dot))
                    {
                        if (isIntersects == 1)
                        {
                            isIntersects = 2;
                        }
                        else
                        {
                            isIntersects = 3;
                        }
                        break;
                    }
                }
            }


            switch (isIntersects)
            {
            case 0:    //两线段都无交点
                if (Math.Length(origin, border.get(j - 1)) + border.getVecter(j).Length > Math.Length(origin, border.get(j + 1)) + border.getVecter(j - 1).Length)
                {
                    border.Insert(origin, j + 1, DrawType.normal);
                }
                else
                {
                    border.Insert(origin, j, DrawType.normal);
                }
                break;

            case 1:    //仅左线段相交
                border.Insert(origin, j + 1, DrawType.Ignore);
                break;

            case 2:    //左右线段都相交
                break;

            case 3:    //仅右线段相交
                border.Insert(origin, j, DrawType.Ignore);
                break;

            case 4:    //采用面积最小方式,无相交
                border.Insert(origin, j + 1, DrawType.Ignore);
                break;

            case 5:    //采用面积最小方式,有相交,插入相交线段
                border.Insert(origin, j + 1, DrawType.Ignore);
                break;

            default:
                break;
            }
        }