2016-03-04 4 views
0

Ceci est pour une affectation. On nous a demandé de faire des fonctions de remplissage d'inondation entièrement récursives et partiellement récursives. J'ai été capable de terminer la fonction entièrement récursive assez simplement, mais je me bats sur le partiellement récursif. Je pourrais utiliser un deuxième avis à ce sujet parce que je me suis convaincu que je l'ai bien fait et que je ne sais plus où chercher une erreur. J'ai inclus des commentaires sur ma logique pour chaque ligne de code.Fonction de remplissage d'inondation partiellement récursif

//setup function for the partially recursive function. 
void DoFloodFill(int x, int y) 
{ 
    x -= m_nTestShapeX; 
    y -= m_nTestShapeY; 
    m_nStartColor = GetPixel(x,y) | 0xff000000; 
    Graphics canvas = getGraphics(); 
    canvas.setColor(m_objSelectedColor); 

    int w = m_objShape.getWidth(); 
    int h = m_objShape.getHeight(); 


    if(m_nStartColor == m_nSelectedColor) 
    { 
     return; 
    } 

    FloodFill(x, y, w, h, canvas); 
} 

void FloodFill(int x, int y, int w, int h, Graphics canvas) 
{ 
    int xx = 0, right = 0; 

    // if the x or y values are out of bounds return 
    if(x >= w || y >= h || x < 0 || y < 0) 
     return; 

    //if the passed in pixel is not the start color return 
    //base case for recursion 
    if(GetPixel(x,y) != this.m_nStartColor) 
     return; 

    //used to walk right untill a wall or bound is hit. 
    xx = x; 

    //walk right from the current pixel setting it to the desired color 
    while(xx < w && this.m_nStartColor == GetPixel(xx,y)) 
    { 
     this.SetPixel(xx+100, y+100, canvas); 
     this.SetPixel(xx+100, y+100, this.m_nSelectedColor); 
     xx++; 
    } 
    //save the x value of the the pixel where the wall is 
    right = xx; 

    //used to left starting one pixel to the left of the current pixel 
    xx = x-1; 

    //walk left of the current pixel setting it to the desired color 
    while(xx >= 0 && this.m_nStartColor == GetPixel(xx,y)) 
    { 
     this.SetPixel(xx+100, y+100, canvas); 
     this.SetPixel(xx+100, y+100, this.m_nSelectedColor); 
     xx--; 
    } 

    //start from where the left wall is 
    for(; xx < right; xx++) 
    { 
     //now this should go up one/down one and repeat the right and left walks 
     //the base cases should prevent this from running in most cases 
     //because when it tries to walk down and the start color is not == to the current pixel it will return. 
     FloodFill(xx,y+1,w,h,canvas); 
     FloodFill(xx,y-1,w,h,canvas); 
    } 
} 

C'est à quoi ça ressemble quand je clique sur un pixel. Où j'ai cliqué est marqué en rouge. eventual stack-overflows

+0

'+ 100'? C'est peut-être moi. –

+0

@JoopEggen il ne peut pas en raison de la réputation? –

+0

désolé le +100 est un décalage pour l'endroit où le pixel a effectivement enregistré vs où il devrait apparaître sur l'écran si cela fait depuis. – user3590350

Répondre

0

Depuis le changement de code ci-dessus:

this.SetPixel(xx+100, y+100, m_nSelectedColor); 

à

this.SetPixel(xx, y, m_nSelectedColor);