2015-04-16 72 views
0

我正在開發一個項目,該項目必須是Windows的顏料。我已經實施了8種工具(刷子,矩形,橢圓形,多邊形,三角形,線條,噴霧和填充工具)。現在我想製作一個「桶」工具,它必須填滿自己周圍的區域。 我使用DFS算法這個工具,但是當面積大,GDB在下面的錯誤:使用DFS算法填充形狀

Program received signal SIGSEGV, Segmentation fault. 
0xb7c47f9d in _IO_new_file_xsputn (f=0xb7d82ac0 <_IO_2_1_stdout_>, data=0xbf80009e, n=6) 
    at fileops.c:1273 
1273 fileops.c: No such file or directory. 

有誰知道這個錯誤是什麼意思? 你可以看到下面bucket.h和bucket.cpp:

bucket.h:

#ifndef BUCKET_H 
#define BUCKET_H 

#include "tool.h" 

#include "SDL/SDL.h" 
#include "SDL/SDL_gfxPrimitives.h" 

#include <cmath> 

class Bucket : public Tool { 
    private: 
     bool **mark; 
     Color selectedPointColor; 
    public: 
     Bucket(bool state, SDLKey key) : Tool(state, key) {} 

     virtual void draw(SDL_Surface*, int, int, int, int, int, Color color); 

     friend void DFS(SDL_Surface*, int, int, Color, bool**); 
     friend Color getColor(SDL_Surface*, int, int); 
}; 

inline Color getColor(SDL_Surface *screen, int x, int y){ 
    Uint32* pixel = (Uint32*) screen->pixels; 
    Uint8* color = (Uint8*) &(pixel[ y * screen->w + x ]); 
    return Color((int) color[2], (int) color[1], (int) color[0]); 
} 

inline void DFS(SDL_Surface *screen, int x, int y, Color color, Color selectedPointColor){ 
    static int counter; 
    counter++; 
    cout << counter << endl; 
    pixelRGBA(screen, x, y, color.red(), color.green(), color.blue(), 255); 

    if (x + 1 < screen->w && getColor(screen, x + 1, y) == selectedPointColor) 
     DFS(screen, x + 1, y, color, selectedPointColor); 

    if (y + 1 < screen->h && getColor(screen, x, y + 1) == selectedPointColor) 
     DFS(screen, x, y + 1, color, selectedPointColor); 

    if (x - 1 >= 0 && getColor(screen, x - 1, y) == selectedPointColor) 
     DFS(screen, x - 1, y, color, selectedPointColor); 

    if (y - 1 >= 0 && getColor(screen, x, y - 1) == selectedPointColor) 
     DFS(screen, x, y - 1, color, selectedPointColor); 
} 

#endif 

bucket.cpp:

#include "bucket.h" 

void Bucket::draw(SDL_Surface *screen, int x, int y, int, int, int, Color color){ 
    selectedPointColor = getColor(screen, x, y); 
    if (selectedPointColor == color) 
     return; 
    DFS(screen, x, y, color, this->selectedPointColor); 
} 

任何幫助,將不勝感激。

+0

是一個完整的堆棧gdb給你(只有1幀)?很難說,但也許它就像堆棧溢出一樣簡單(因爲它只發生在大區域,DFS()被遞歸地調用)? –

+0

「歡迎使用堆棧溢出」似乎適用於多種原因。 –

回答

2

它是堆棧溢出。如果填充一個形狀簡單的大空間(如矩形),遞歸深度將大致等於該空間的面積,因爲它幾乎總是會到達某個分支而不會返回。

這就是說,如果圖像是例如1000x1000,則遞歸深度大約是100萬,這太多了。

您不應該使用DFS進行填充,而應該使用BFS。