Пример #1
0
        private Boolean is_ear(Point p)                         // checks if given vertice is in a ear
        {
            mPolygon m = new mPolygon(updated_vertices);        // init. a polygon from the current vertices

            if (m.is_vertex(p) == true)                         // if given point is a vertex
            {
                if (m.vertex_type(p) == VertexType.ConvexPoint) // and it's a convex point
                {
                    Point curr_point = p;
                    Point prev_point = m.get_prev_point(p);                                                     // find previous adjacent point
                    Point next_point = m.get_next_point(p);                                                     // find next adjacent point

                    for (int i = updated_vertices.GetLowerBound(0); i < updated_vertices.GetUpperBound(0); i++) // loop through all other vertices
                    {
                        Point pt = updated_vertices[i];
                        if (!(is_points_equal(pt, curr_point) || is_points_equal(pt, prev_point) || is_points_equal(pt, next_point)))
                        {                                                                                     // if pt is not equal to checked vertice or its's next and prev adjacent vertices
                            if (is_point_in_triangle(new Point[] { prev_point, curr_point, next_point }, pt)) // check pt lies in triangle
                            {
                                return(false);                                                                // if another vertice lies in this triangle, then this is not an ear
                            }
                        }
                    }
                }
                else // concave
                {
                    return(false); // we cannot make ears from concave points
                }
                return(true); // if CS:IP reaches here, this means vertice passed the test and is an ear
            }
            return(false);    // if the given vertex is not an vertex, it's not related to an ear also!
        }
Пример #2
0
        private void update_vertices(Point p)                 // called after finding a ear from a vertice
        {                                                     // removes the vertice from updates_vertices list
            ArrayList temp_points = new ArrayList();          // temp. location while updating the list

            for (int i = 0; i < updated_vertices.Length; i++) // loop through the list
            {
                if (p == updated_vertices[i])                 // if we found the vertice we'r lookig for
                {
                    mPolygon poly       = new mPolygon(updated_vertices);
                    Point    curr_point = p;
                    Point    next_point = poly.get_next_point(p); // find the vertices next neighboor
                    Point    prev_point = poly.get_prev_point(p); // find the vertices previous neighboor

                    Point[] ear = new Point[3];                   // create a 3 point triangle-polygon from these 3 points
                    ear[0] = prev_point;                          // previos vertice
                    ear[1] = curr_point;                          // current vertice
                    ear[2] = next_point;                          // next vertice

                    ears.Add(ear);                                // add this ear to ears list
                }
                else                                              // this is not the vertice we'r looking for
                {
                    temp_points.Add(updated_vertices[i]);         // so add it to temp. vertice list
                }
            }

            if ((updated_vertices.Length - temp_points.Count) == 1)        // we removed 1 vertice from the list, check the counts
            {
                updated_vertices = new Point[updated_vertices.Length - 1]; // redimension the list to -1 size, cause we removed 1 vertice
                for (int i = 0; i < temp_points.Count; i++)
                {
                    updated_vertices[i] = (Point)temp_points[i];    // update the newest updated_vertices from temp location
                }
            }
        }
Пример #3
0
        public void triangulate()                               // triangulate the bigger polygon-shape
        {
            mPolygon poly     = new mPolygon(updated_vertices); // create a polygon from the current vertices
            Boolean  finished = false;                          // triangulation-finished?

            if (updated_vertices.Length == 3)                   // if there's only 3 points, no need to run algorithm
            {
                finished = true;
            }

            Point p = new Point();

            while (finished == false)   // loop while triangulation not finished yet
            {
                int     i         = 0;
                Boolean not_found = true;                          // did we found an ear? no, not yet
                while (not_found && (i < updated_vertices.Length)) // while we did not found any ear and not yet processed all vertices
                {
                    p = updated_vertices[i];                       // get current point
                    if (is_ear(p))                                 // check if we can get an ear from that vertice
                    {
                        not_found = false;                         // good we found one
                    }
                    else
                    {
                        i++;                    // continue to search
                    }
                }

                update_vertices(p);                    // remove the vertice we found the ear from the updated_vertices list
                poly = new mPolygon(updated_vertices); // reupdate the polygon from the rest of vertices
                if (updated_vertices.Length == 3)      // if there's only 3 vertice left
                {
                    finished = true;                   // this means we finished the triangulation
                }
            }

            // when the CS:IP reaches here, this means triangulation finished
            polygons = new Point[ears.Count + 1][]; // init polygons structure to ears.count + 1(for last 3 points left)
            for (int i = 0; i < ears.Count; i++)
            {
                Point[] points = (Point[])ears[i];  // move ears to final triangulated polygons list
                polygons[i]    = new Point[3];
                polygons[i][0] = points[0];
                polygons[i][1] = points[1];
                polygons[i][2] = points[2];
            }

            // we have 3 left vertices on updated_vertices list, - the last triangulated polygon -
            polygons[ears.Count] = new Point[updated_vertices.Length]; // add it to triangulated polygons list also
            for (int i = 0; i < updated_vertices.Length; i++)
            {
                polygons[ears.Count][i] = updated_vertices[i];
            }
        }