lundi 22 mai 2017

Recursion condition doesn't work

I have a function for polygon triangulatuin by ear clipping method.

void triangulation(std::vector<point> Fparam) {
         int iterator;
         int vertex_count = Fparam.size();
         if (vertex_count == 3) {
            return;
         }
         std::vector<point> F2;
         std::vector<point> newF;
         bool isInside = true;
         newF.clear();
         F2.clear();
         double x, y, x_ek, y_ek;
         if (vertex_count > 3) {
            for (iterator = 0; iterator < vertex_count; iterator++) {
                int iteratorMinus = (iterator == 0) ? vertex_count - 1 : iterator - 1;
                int iteratorPlus = (iterator == vertex_count - 1) ? 0 : iterator + 1;

                double b[3][3] = { { Fparam[iteratorMinus].x,Fparam[iteratorMinus].y,1 },{ Fparam[iterator].x,Fparam[iterator].y,1 },{ Fparam[iteratorPlus].x,Fparam[iteratorPlus].y,1 } };
                double result = det(3, b);
                if (result > 0) {
                    int v1;
                    for (v1 = 0; v1 < vertex_count; v1++) {
                        if (iterator != v1) {

                            double newArray[3][3] = { { Fparam[v1].x,Fparam[v1].y,1 },{ Fparam[iteratorMinus].x,Fparam[iteratorMinus].y,1 },{ Fparam[iterator].x,Fparam[iterator].y,1 } };

                            double resultv1 = det(3, newArray);
                            if (resultv1 > 0) {

                                double newArray2[3][3] = { { Fparam[v1].x,Fparam[v1].y,1 },{ Fparam[iterator].x,Fparam[iterator].y,1 },{ Fparam[iteratorPlus].x,Fparam[iteratorPlus].y,1 } };
                                double resultv2 = det(3, newArray2);
                                if (resultv2 > 0) {
                                    double newArray3[3][3] = { { Fparam[v1].x,Fparam[v1].y,1 },{ Fparam[iteratorMinus].x,Fparam[iteratorMinus].y,1 },{ Fparam[iteratorPlus].x,Fparam[iteratorPlus].y,1 } };
                                    double resultv3 = det(3, newArray3);
                                    if (resultv3 > 0) {

                                    }
                                    else {
                                        isInside = false;
                                    }
                                }
                                else
                                {
                                    isInside = false;
                                }
                            }
                            else {
                                isInside = false;
                            }
                        }
                    }


                    if (isInside) {
                        F2.clear();                         
                        x = Fparam[iteratorMinus].x; y = Fparam[iteratorMinus].y;
                        x_ek = c*x + cx; y_ek = c*y + cy;
                        F2.push_back({ x_ek, y_ek });
                        x = Fparam[iteratorPlus].x; y = Fparam[iteratorPlus].y;
                        x_ek = c*x + cx; y_ek = c*y + cy;
                        F2.push_back({ x_ek, y_ek });

                        glColor3d(0, 1, 0);
                        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
                        glLineWidth(3);
                        glBegin(GL_POLYGON);                            
                            glVertex2d(F2[0].x, F2[0].y);
                            glVertex2d(F2[1].x, F2[1].y);
                        glEnd();

                        for (int i = 0; i < vertex_count; i++) {
                            if (i != iterator) {
                                newF.push_back({ Fparam[i].x, Fparam[i].y });
                            }

                        }
                        triangulation(newF);
                        newF.clear();
                    }
                }
                else {
                }
            }
        }
    }

I use recursion with condition

    if (vertex_count == 3) {
       return ;
}

Vertex_count is number of polygon's vertex.When I have 3 vertices my figure is triangle and triangulation is done. So when this condition is true, working return; BUT function get called again. Whath's wrong with it ?

Aucun commentaire:

Enregistrer un commentaire