4-way recursive flood fill
Sprache: English
Programmiersprache: C++
Veröffentlicht von: wep [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1718
Beschreibung
this code will flood fill an area with a given character/color combination. although it's now used in a character oriented environment, the same thing goes for graphical applications. there a some queue functions in this code that you'll have to assume are there. if you want them, email me at wep@planetzztpp.com or check my project area (zzt++) at sourceforge.this is a very fast and efficient routine that takes up a minimal amount of stack space during recursion.improvements are welcome.
Code
1 //
2 // Function: FillArea
3 // Fills the area of joining identical characters like FillOverChar,
4 // with the given character and color. This routine will fill the
5 // current position with the new character if it contains the character and
6 // has the same color we should overwrite. It does the same under this
7 // condition for the position to its immediate north, east, west and south.
8 // Except that we have to look ahead one position before filling. This way,
9 // when we hit a wall the filling in that direction stops.
10 // Positions to the north-east, etc are not used to avoid 'leaking'.
11 // e.g.:
12 // ##### <- Would leak through this corner
13 // # #
14 // # #
15 // ######
16 // Parameters:
17 // int x=x-coordinate of starting position
18 // int y=y-coordinate of starting position
19 // int c=character to be used
20 // int col=color to be used
21 // int FillOverChar=character we are filling over
22 // int FillOverCol=color we are filling over
23 // Returns:
24 // Nothing.
25 //
26
27 void BoardManager::FillArea(char x, char y, char c, char col, char FillOverChar, char FillOverCol)
28 {
29 // Temporary storage for x,y positions
30 xypos xy, current, N, E, S, W;
31
32 // Setup the current position as a struct.
33 xy.x=x;
34 xy.y=y;
35 xy.next=NULL;
36
37 // Add the current position to the queue.
38 FillAdd(xy);
39
40 // And immediately draw it.
41 DrawChar(xy.x, xy.y, c, col);
42
43 // Keep looping until the queue is empty.
44 while (!FillQIsEmpty())
45 {
46 // Get next element to draw.
47 FillRemove(¤t);
48
49 // Fetch circling positons to North, East, South and West
50 N.x=current.x; N.y=current.y-1; N.next=NULL;
51 E.x=current.x+1; E.y=current.y; E.next=NULL;
52 S.x=current.x; S.y=current.y+1; S.next=NULL;
53 W.x=current.x-1; W.y=current.y; W.next=NULL;
54
55 // Since xypos.x and xypos.y are unsigned chars, the checks for
56 // them being greater or equal to zero are superfluous.
57 // First check whether we should overwrite this character.
58 // Then check whether we're still inside the screen limits.
59 if (GetChar(N.x, N.y)==FillOverChar && GetCol(N.x, N.y)==FillOverCol)
60 if (N.x<81 && N.y<26)
61 {
62 // Draw the character to the north and
63 // queue its position.
64 DrawChar(N.x, N.y, c, col);
65 FillAdd(N);
66 }
67
68 // Do the same for East, South and West
69 if (GetChar(E.x, E.y)==FillOverChar && GetCol(E.x, E.y)==FillOverCol)
70 if (E.x<81 && E.y<26)
71 {
72 DrawChar(E.x, E.y, c, col);
73 FillAdd(E);
74 }
75
76 if (GetChar(S.x, S.y)==FillOverChar && GetCol(S.x, S.y)==FillOverCol)
77 if (S.x<81 && S.y<26)
78 {
79 DrawChar(S.x, S.y, c, col);
80 FillAdd(S);
81 }
82
83 if (GetChar(W.x, W.y)==FillOverChar && GetCol(W.x, W.y)==FillOverCol)
84 if (W.x<81 && W.y<26)
85 {
86 DrawChar(W.x, W.y, c, col);
87 FillAdd(W);
88 }
89 }
90 }
91
Noch kein Kommentar vorhanden
Dieses Snippet kommentieren
Name *
E-Mail (wird nicht angezeigt) *
Website
Kommentar *
Sicherheitscode *