Exemplo n.º 1
0
 public int getPublicEdge(Triange other)
 {
     Point[] otherPoints = other.getPointArray();
     Point[] myPoints    = getPointArray();
     bool[]  result      = new bool[3] {
         false, false, false
     };
     for (int i = 0; i < 3; i++)
     {
         Point myPoint = myPoints[i];
         for (int j = 0; j < 3; j++)
         {
             if (myPoint.Equals(otherPoints[j]))
             {
                 result[i] = true;
                 break;
             }
         }
     }
     if (result[0] && result[1])
     {
         return(1);
     }
     if (result[1] && result[2])
     {
         return(2);
     }
     if (result[0] && result[2])
     {
         return(3);
     }
     return(0);
 }
Exemplo n.º 2
0
        private static ArrayList ProcessLoop(ArrayList plist)
        {
            ArrayList res = new ArrayList();

            if (plist.Count == 3)
            {
                Triange tr = new Triange((Point)plist[0], (Point)plist[1], (Point)plist[2]);
                res.Add(tr);
            }
            else
            {
                //找到凸点
                int minY     = ((Point)plist[0]).Y;
                int minIndex = 0;
                for (int i = 1; i < plist.Count; i++)
                {
                    Point _p = (Point)plist[i];
                    if (minY > _p.Y)
                    {
                        minY     = _p.Y;
                        minIndex = i;
                    }
                    else if (minY == _p.Y)
                    {
                        if (((Point)plist[minIndex]).X > _p.X)
                        {
                            minIndex = i;
                        }
                    }
                }
                int       lastIndex   = (plist.Count + minIndex - 1) % plist.Count;
                int       nextIndex   = (minIndex + 1) % plist.Count;
                Point     p1          = (Point)plist[lastIndex];
                Point     p2          = (Point)plist[minIndex];
                Point     p3          = (Point)plist[nextIndex];
                Triange   tr          = new Triange(p1, p2, p3);
                ArrayList insidePoint = new ArrayList();
                //检查在三角形内的点
                for (int index = 0; index < plist.Count; index++)
                {
                    Point _p = (Point)plist[index];
                    if (tr.CheckPointInSide(_p))
                    {
                        insidePoint.Add(index);
                    }
                }
                if (insidePoint.Count > 0)
                {
                    //寻找离 线段p1p3最远的点p 连接p2和p 将多边形切割为两个
                    int    maxPointIndex = (int)insidePoint[0];
                    double maxDistance   = distance((Point)plist[maxPointIndex], p1, p3);

                    for (int i = 1; i < insidePoint.Count; i++)
                    {
                        int    curIndex    = (int)insidePoint[i];
                        double curDistance = distance((Point)plist[curIndex], p1, p3);
                        if (curDistance > maxDistance)
                        {
                            maxDistance   = curDistance;
                            maxPointIndex = curIndex;
                        }
                    }

                    //分割多边形
                    ArrayList leftP  = new ArrayList();     // minIndex -> maxPointIndex
                    ArrayList rightP = new ArrayList();     // maxPointIndex -> minIndex

                    int loop = minIndex;
                    while (true)
                    {
                        leftP.Add(plist[loop]);
                        if (loop == maxPointIndex)
                        {
                            break;
                        }
                        loop++;
                        if (loop >= plist.Count)
                        {
                            loop = 0;
                        }
                    }
                    loop = maxPointIndex;
                    while (true)
                    {
                        rightP.Add(plist[loop]);
                        if (loop == minIndex)
                        {
                            break;
                        }
                        loop++;
                        if (loop >= plist.Count)
                        {
                            loop = 0;
                        }
                    }
                    ArrayList otherRes1 = ProcessLoop(leftP);
                    ArrayList otherRes2 = ProcessLoop(rightP);
                    res.AddRange(otherRes1);
                    res.AddRange(otherRes2);
                }
                else
                {
                    //去除顶点
                    res.Add(tr);
                    ArrayList newPList = (ArrayList)plist.Clone();
                    newPList.RemoveAt(minIndex);
                    ArrayList otherRes = ProcessLoop(newPList);
                    res.AddRange(otherRes);
                }
            }
            return(res);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 保存数据
        /// </summary>
        /// 数据格式 三角总个数 int
        /// 三角形编号int 三个坐标 int*3*2 中心点位置 int*2
        /// 三角形邻接关系 个数*个数*byte
        /// <param name="trianges"></param>
        public static void saveData(ArrayList triList, string FilePath)
        {
            string       realPathName = Path.Combine(FilePath, fileName);
            FileStream   fs           = new FileStream(realPathName, FileMode.Create);
            BinaryWriter bw           = new BinaryWriter(fs, Encoding.Default);
            int          meshNum      = triList.Count; //地图分块数

            bw.Write(meshNum);
            int totalTriNum = 0;

            for (int i = 0; i < meshNum; i++)      //各个块三角形数
            {
                totalTriNum += ((ArrayList)triList[i]).Count;
            }
            bw.Write(totalTriNum); //三角形总个数
            foreach (Object obj in triList)
            {
                ArrayList childTriList = (ArrayList)obj;
                Triange[] trianges = (Triange[])childTriList.ToArray(typeof(Triange));
                int       triNum = trianges.Length;
                int       minX = 0, minY = 0, maxX = 0, maxY = 0;
                bw.Write(triNum);
                for (int i = 0; i < triNum; i++)
                {
                    Triange tr = trianges[i];
                    bw.Write(i + 1);
                    bw.Write(tr._p1.X);
                    bw.Write(tr._p1.Y);

                    bw.Write(tr._p2.X);
                    bw.Write(tr._p2.Y);

                    bw.Write(tr._p3.X);
                    bw.Write(tr._p3.Y);

                    //Point [] trps= new Point[]{tr._p1,tr._p2,tr._p3};
                    Rectangle rec = tr.getRect();
                    if (i == 0)
                    {
                        minX = rec.X;
                        maxX = rec.X + rec.Width;

                        minY = rec.Y;
                        maxY = rec.Y + rec.Height;
                    }
                    else
                    {
                        int curMinX = rec.X;
                        int curMaxX = rec.X + rec.Width;

                        int curMinY = rec.Y;
                        int curMaxY = rec.Y + rec.Height;

                        if (curMinX < minX)
                        {
                            minX = curMinX;
                        }

                        if (curMaxX > maxX)
                        {
                            maxX = curMaxX;
                        }

                        if (curMinY < minY)
                        {
                            minY = curMinY;
                        }

                        if (curMaxY > maxY)
                        {
                            maxY = curMaxY;
                        }
                    }
                    int[] link = new int[] { 0, 0, 0 };
                    for (int j = 0; j < triNum; j++)
                    {
                        //有两个点相交即为邻接
                        if (j == i)
                        {
                            continue;
                        }
                        Triange temptr    = trianges[j];
                        int     linkIndex = tr.getPublicEdge(temptr);
                        if (linkIndex == 0)
                        {
                            continue;
                        }
                        link[linkIndex - 1] = j + 1;
                    }
                    bw.Write(link[0]);
                    bw.Write(link[1]);
                    bw.Write(link[2]);
                }

                bw.Write(minX);
                bw.Write(minY);
                bw.Write(maxX);
                bw.Write(maxY);
            }
            bw.Close();
        }