Followers 0

# Checkerboard algorithm?

## 3 posts in this topic

Hi everyone!

I'm looking to create a function which simulates a checkerboard, i.e. returns 0 for a black square and 1 for a white square. I've got it working for a board where each square has a size of 1, but I can't work out how to make a scaleable version:

//Pseudo-code
checkerboardColour(Vec point)
{
return (floor(p.x) + floor(p.y)) % 2 == 0 ? white : black;
}


I know that I'd need to do a division of some sort to scale the size of the squares, but I can't work out where to put it. Say I wanted to quadruple the size of each black/white square, how would I insert a 'scale' variable into the function to make this happen?

Thanks!

0

##### Share on other sites

Try this:

//Pseudo-code
checkerboardColour(Vec point, float fScale)
{
return ((int)(p.x/fScale) + (int)(p.y/fScale)) % 2 == 0 ? white : black;
}

1

##### Share on other sites

Cool, that works :) I actually tried that first but I must have had a bad bracket or something...

0

## Create an account

Register a new account