0

I am trying to build the bricks for the breakout assignment and I have a weird bug. I was trying to work out the spacing between the bricks and the it's fine EXCEPT when there are 10 columns. 9 columns is fine, 11 columns is fine, but 10 columns creates a larger than normal space at the right side of the window. Is this a bug in software or am I coding something weird?

Thanks!

Here is my code and screenshots

#include<spl/gwindow.h>
#include<spl/gobjects.h>

// height and width of game's window in pixels
#define HEIGHT 600
#define WIDTH 400

// 1st brick location
#define BRICK_X 3
#define BRICK_Y 100

// height and width of brick
#define BRICK_W ((WIDTH - (SPACE * (COLS + 1))) / COLS)
#define BRICK_H ((HEIGHT / 10) / ROWS)

// space size
#define SPACE 3

// number of rows of bricks
#define ROWS 5

// number of columns of bricks
#define COLS 11


int main(void)
{
    GWindow window = newGWindow(WIDTH, HEIGHT);

    for(int j = 0; j < ROWS; j++)
    {
        for(int i = 0; i < COLS; i++)
        {            
            GRect brick = newGRect(BRICK_X + (i * BRICK_W) + (SPACE * i), (BRICK_Y + (j * BRICK_H) + (SPACE * j)), BRICK_W, BRICK_H);
            setColor(brick, "Red");

            add(window, brick);          
        }
    }
    return 0;
}

9 columns is fine

10 columns has a larger space on the right

11 columns is fine


update:

So I took your advice and added 1/2 spaces in the beginning and end of the row so that # of spaces equal # of columns. I think the whole number thing is resolved but I'm still getting unequal spacing at the ends.

#include<spl/gwindow.h>
#include<spl/gobjects.h>
#include<spl/gevents.h>

// height and width of game's window in pixels
#define HEIGHT 600
#define WIDTH 400

// 1st brick location
#define BRICK_X (SPACE / 2)
#define BRICK_Y 100

// height and width of brick
#define BRICK_W ((WIDTH - (SPACE * (COLS))) / COLS)
#define BRICK_H ((HEIGHT / 10) / ROWS)

// space size
#define SPACE 6

// number of rows of bricks
#define ROWS 5

// number of columns of bricks
#define COLS 9


int main(void)
{
    GWindow window = newGWindow(WIDTH, HEIGHT);

    for(int j = 0; j < ROWS; j++)
    {
        for(int i = 0; i < COLS; i++)
        {            
            GRect brick = newGRect(BRICK_X + (BRICK_W * i) + (SPACE * i), (BRICK_Y + (BRICK_H * j) + (SPACE * j)), BRICK_W, BRICK_H);
            setColor(brick, "Red");

            add(window, brick);          
        }
    }
    return 0;
}

9 columns 10 columns 11 columns

1

I bet you thought you were done with pset1-greedy and would never see it again!

It's not a bug, it's a side effect of integer division. Your formula for calculating BRICK_W would normally result in 41.1, 36.7, and 33.1 in each of the 3 cases. When it eventually converts to an integer, you get 41, 36 and 33 for widths. You are losing those rounding errors not once, but 9, 10, and 11 times across the window, meaning you are losing about 1 pixel for both 9 and 11 columns, but you are losing 7 pixels for 10 columns! This is the visible difference that you see.

There are a number of different ways to address this. Manipulate the numbers until you see something you like. Change the spacing to something that produces whole numbers when you do division. Change the spacing to use full spaces between bricks and 1/2 spaces between the end bricks and the walls (makes the total # of spaces = the number of columns, also means that 400 width divides nicely by 10 no matter what the other numbers are.)

But, yes, everything is working exactly as it should.

If this answers your question, please click on the check mark to accept. Let's keep up on forum housekeeping. ;-)

2
  • Thanks! I totally forgot about the whole integer thing! Dec 8 '15 at 3:50
  • Hi, I took your advice and made the changes, but I'm still getting some spacing issues. I've added another post as an answer since the comments section is limited. Dec 8 '15 at 4:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .