create a chessboard with squares of any size

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    class Image
    {
        public:
        typedef uint16_t PixelType;

        private:
        int width_;
        int height_;
        std::vector<PixelType> data_;

        public:
        
        Image()
        : width_(0),height_(0),data_()
        {}

        
        Image(unsigned int width, unsigned int height)
        : width_(width),height_(height),data_(width*height,0)
        {}

        
        int width() const
        {
            return width_;
        }

        int height() const
        {
            return height_;
        }

        int size() const
        {
            return width_ * height_ ;
        }

        
        void resize(unsigned int new_width, unsigned int new_height)
        {
            data_.resize(new_width * new_height);
            width_ = new_width;
            height_ = new_height;
        }

         PixelType operator()(int x, int y) const
        {
            return data_[x+y*width_];
        }

        
         PixelType & operator()(int x, int y)
         {
            return data_[x+y*width_];
         }
    };


Hello everyone, I was meant to write this class called "Image".
It creates an image of given width and height and then a 1D vector that stores black and white pixels and its initial pixels are all black.
Now I am meant to write a function so that I can create a big picture of a chessboard whose squares' size can vary.
The function declaration should look like this:

Image chessboard(unsigned int width, unsigned int height,
unsigned int square_size)

So a square of side = square_size and black pixels (value '0') should be at the top left of the image and next to it and below there should be squares of white pixels (value '255'), etc.
However I have a really hard time figuring the algorithm! I thought of resizing the bigger picture to an image of width = (original width)/square_size and height = (original height)/square_size so that each square is one bigger pixel but I still can't figure out how to fill in the vector so that it looks like a chessboard.
I also have these equations:
i = x + y*width,
x = i%width,
y = i/width
where i is the index of the vector and x and y the 'coordinates' of each pixel in the bigger picture.
Any help would be great!
Topic archived. No new replies allowed.