Ejemplo n.º 1
0
        /****************************************************************
         * To check whether the Vertex is an ear or not based updated Polygon vertices
         *
         * ref. www-cgrl.cs.mcgill.ca/~godfried/teaching/cg-projects/97/Ian
         * /algorithm1.html
         *
         * If it is an ear, return true,
         * If it is not an ear, return false;
         *****************************************************************/
        private bool IsEarOfUpdatedPolygon(MyVector2 vertex)
        {
            CPolygon polygon = new CPolygon(m_aUpdatedPolygonVertices);

            if (polygon.PolygonVertex(vertex))
            {
                bool bEar = true;
                if (polygon.PolygonVertexType(vertex) == VertexType.ConvexPoint)
                {
                    MyVector2 pi = vertex;
                    MyVector2 pj = polygon.PreviousPoint(vertex);                //previous vertex
                    MyVector2 pk = polygon.NextPoint(vertex);                    //next vertex

                    for (int i = m_aUpdatedPolygonVertices.GetLowerBound(0);
                         i < m_aUpdatedPolygonVertices.GetUpperBound(0); i++)
                    {
                        MyVector2 pt = m_aUpdatedPolygonVertices[i];
                        if (!(pt.EqualsPoint(pi) || pt.EqualsPoint(pj) || pt.EqualsPoint(pk)))
                        {
                            MyVector2[] vecs = new MyVector2[3] {
                                pj, pi, pk
                            };
                            if (TriangleContainsPoint(vecs, pt))
                            {
                                bEar = false;
                            }
                        }
                    }
                }                 //ThePolygon.getVertexType(Vertex)=ConvexPt
                else              //concave point
                {
                    bEar = false; //not an ear/
                }
                return(bEar);
            }
            else             //not a polygon vertex;
            {
                System.Diagnostics.Trace.WriteLine("IsEarOfUpdatedPolygon: " +
                                                   "Not a polygon vertex");
                return(false);
            }
        }
Ejemplo n.º 2
0
        /**********************************************************
         * To check the Pt is in the Triangle or not.
         * If the Pt is in the line or is a vertex, then return true.
         * If the Pt is out of the Triangle, then return false.
         *
         * This method is used for triangle only.
         ***********************************************************/
        private bool TriangleContainsPoint(MyVector2[] trianglePts, MyVector2 pt)
        {
            if (trianglePts.Length != 3)
            {
                return(false);
            }

            for (int i = trianglePts.GetLowerBound(0);
                 i < trianglePts.GetUpperBound(0); i++)
            {
                if (pt.EqualsPoint(trianglePts[i]))
                {
                    return(true);
                }
            }

            bool bIn = false;

            CLineSegment line0 = new CLineSegment(trianglePts[0], trianglePts[1]);
            CLineSegment line1 = new CLineSegment(trianglePts[1], trianglePts[2]);
            CLineSegment line2 = new CLineSegment(trianglePts[2], trianglePts[0]);

            if (line0.PointInLine(pt) || line1.PointInLine(pt) || line2.PointInLine(pt))
            {
                bIn = true;
            }
            else             //point is not in the lines
            {
                double dblArea0 = CPolygon.PolygonArea(new MyVector2[] { trianglePts[0], trianglePts[1], pt });
                double dblArea1 = CPolygon.PolygonArea(new MyVector2[] { trianglePts[1], trianglePts[2], pt });
                double dblArea2 = CPolygon.PolygonArea(new MyVector2[] { trianglePts[2], trianglePts[0], pt });

                if (dblArea0 > 0)
                {
                    if ((dblArea1 > 0) && (dblArea2 > 0))
                    {
                        bIn = true;
                    }
                }
                else if (dblArea0 < 0)
                {
                    if ((dblArea1 < 0) && (dblArea2 < 0))
                    {
                        bIn = true;
                    }
                }
            }
            return(bIn);
        }
Ejemplo n.º 3
0
        /********************************************************
         * To update m_aUpdatedPolygonVertices:
         * Take out Vertex from m_aUpdatedPolygonVertices array, add 3 points
         * to the m_aEars
         **********************************************************/
        private void UpdatePolygonVertices(MyVector2 vertex)
        {
            System.Collections.ArrayList alTempPts = new System.Collections.ArrayList();

            for (int i = 0; i < m_aUpdatedPolygonVertices.Length; i++)
            {
                if (vertex.EqualsPoint(
                        m_aUpdatedPolygonVertices[i]))                 //add 3 pts to FEars
                {
                    CPolygon  polygon = new CPolygon(m_aUpdatedPolygonVertices);
                    MyVector2 pti     = vertex;
                    MyVector2 ptj     = polygon.PreviousPoint(vertex);       //previous point
                    MyVector2 ptk     = polygon.NextPoint(vertex);           //next point

                    MyVector2[] aEar = new MyVector2[3];                     //3 vertices of each ear
                    aEar[0] = ptj;
                    aEar[1] = pti;
                    aEar[2] = ptk;

                    m_alEars.Add(aEar);
                }
                else
                {
                    alTempPts.Add(m_aUpdatedPolygonVertices[i]);
                }                 //not equal points
            }

            if (m_aUpdatedPolygonVertices.Length
                - alTempPts.Count == 1)
            {
                int nLength = m_aUpdatedPolygonVertices.Length;
                m_aUpdatedPolygonVertices = new MyVector2[nLength - 1];

                for (int i = 0; i < alTempPts.Count; i++)
                {
                    m_aUpdatedPolygonVertices[i] = (MyVector2)alTempPts[i];
                }
            }
        }