Copyright © Philip M. Parker, INSEAD. Terms of Use.

C EXAMPLE

Specialty Definition: Flood fill example in C

(From Wikipedia, the free Encyclopedia)

This flood fill code is adapted from a Tetris clone called "Tetanus On Drugs" by Damian Yerrick. It's licensed under the GNU General Public License and comes with ABSOLUTELY NO WARRANTY.

 #define BOARD_WIDTH  10
 #define BOARD_HEIGHT 20

typedef struct MAP { unsigned char b[BOARD_HEIGHT][BOARD_WIDTH]; } MAP;

static void flood_loop(MAP *map, int x, int y, unsigned int dst_c, unsigned int src_c) { int fillL, fillR, i; int in_line = 1;

/* find left side, filling along the way */ fillL = fillR = x; while( in_line ) { map->b[y][fillL] = dst_c; fillL--; in_line = (fillL < 0) ? 0 : (map->b[y][fillL] == src_c); } fillL++;

/* find right side, filling along the way */ in_line = 1; while( in_line ) { map->b[y][fillR] = c; fillR++; in_line = (fillR > 9) ? 0 : (map->b[y][fillR] == fillC); } fillR--;

/* search top and bottom */ for(i = fillL; i <= fillR; i++) { if( y > 0 && map->b[y - 1][i] == fillC )

flood_loop(map, i, y - 1, c, fillC);
     if( y < BOARD_HEIGHT && map->b[y + 1][i] == fillC )
flood_loop(map, i, y + 1, c, fillC);
   }
 }

void flood_fill(MAP *map, int x, int y, unsigned int c) { flood_loop(map, x, y, c, map->b[y][x]); map->b[y][x] = c; /* some buggy optimizers needed this line */ }

This is the same algorithm modified for Java by Claudio Santana

 int checkPixel(int[] oldPix, int[] newPix)
 {
   return (newPix[0] == oldPix[0] &&
   newPix[1] == oldPix[1]	&&
   newPix[2] == oldPix[2]  &&
   newPix[3] == oldPix[3] ? 1:0);
 }

void floodLoop(WritableRaster raster, int x, int y, int[] fill, int[] old) { int fillL, fillR,i; int in_line=1; int[] aux = {255,255,255,255};

// find left side, filling along the way fillL = fillR = x; while(in_line!=0) { int[] p = raster.getPixel(fillL,y,aux); raster.setPixel(fillL,y,fill); fillL--; in_line = (fillL < 0) ? 0 : checkPixel(raster.getPixel(fillL,y,aux),old); } fillL++;

// find right side, filling along the way in_line = 1; while (in_line!=0) { raster.setPixel(fillR,y,fill); fillR++; in_line = (fillR > d.width-1) ? 0 : checkPixel(raster.getPixel(fillR,y,aux),old); } fillR--;

// look up and down for( i=fillL; i<=fillR; i++ ) { if ( y>0 && checkPixel(raster.getPixel(i,y-1,aux),old)!=0 ) floodLoop(raster,i,y-1,fill,old); if ( y }

// Initial method you must call void floodLoop(WritableRaster raster, int x, int y, int[] fill) { int[] aux = new int[] {255,255,255,255};

// validation so we don't fall in an infinite loop trying to // paint in the same color if ( checkPixel(raster.getPixel(x,y,aux),fill)!=0 ) return;

floodLoop(raster,x,y,fill,raster.getPixel(x,y,aux) ); }

Source: adapted by the editor from Wikipedia, the free encyclopedia under a copyleft GNU Free Documentation License (GFDL) from the article "Flood fill example in C."

Top     

Anagrams: C EXAMPLE

Scrabble® Enable2K-Verified Anagrams

Words within the letters "a-c-e-e-l-m-p-x"

-1 letter: emplace, example, exempla.

-2 letters: empale.

-3 letters: ample, camel, clamp, clepe, excel, expel, macle, maple, peace, place.

-4 letters: acme, alec, alee, alme, apex, axel, axle, calm, calx, came, camp, cape, cepe, clam, clap, exam, exec, lace, lame, lamp, leap, mace, male, meal, pace, pale, palm, peal, peel, pele, plea.

-5 letters: ace, ale, alp, amp, ape, axe.

Source: compiled by the editor from various references; see credits.

SCRABBLE® is a registered trademark. All intellectual property rights in and to the game are owned in the U.S.A and Canada by Hasbro Inc., and throughout the rest of the world by J.W. Spear & Sons Limited of Maidenhead, Berkshire, England, a subsidiary of Mattel Inc. Mattel and Spear are not affiliated with Hasbro.

Top     

Alternative Orthography: C EXAMPLE


Hexadecimal (or equivalents, 770AD-1900s) (references)

43      45 58 41 4D 50 4C 45

Leonardo da Vinci (1452-1519; backwards) (references)

    

Binary Code (1918-1938, probably earlier) (references)

01000011 00100000 01000101 01011000 01000001 01001101 01010000 01001100 01000101

HTML Code (1990) (references)

&#67; &#32; &#69; &#88; &#65; &#77; &#80; &#76; &#69;

ISO 10646 (1991-1993) (references)

0043      0045 0058 0041 004D 0050 004C 0045

Encryption (beginner's substitution cypher): (references)

37239583547504639

Top     



INDEX

1. Anagrams
2. Orthography
3. Bibliography


  

Copyright © Philip M. Parker, INSEAD. Terms of Use.