Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Count the number of Alive pixels around the pixel at (x, y).
- int countNeighbours(CImg<unsigned char> &image, int x, int y)
- {
- int count = 0;
- for (int x0 = x -1; x0 < x + 2; x0++)
- {
- if (x < 0 || x >= image.width())
- continue; // Skip this loop if out of range.
- for (int y0 = y -1; y0 < y +2; y0++)
- {
- if (y < 0 || y >= image.height())
- continue; // Skip this loop if out of range.
- if(!(x0 == x && y0 == y))
- { // Check we're not on current pixel.
- count += image(x0, y0, 0);
- }
- }
- }
- // Count number of living around pixel and return.
- // Divide by 255 because each time we increment count using image(x,y,0)
- // if it is white the number returned will be 255.
- // Do 8 - (ans) because white = dead.
- return(8 - (count/255));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement