Error when drawing grid, grid lines a bit too short
Hello, as I've mentioned in some other post I'm making a simple "Game of
Life" with a small, fixed-sized grid. Anyway, the function I used for
drawing the actual grid was written ages ago when doing some pure Win32
programming and it was full of magic numbers, assuming a 16*16 grid. I
tried to rewrite it without magic numbers and to take the actual size
into consideration. Right now each cell in the grid is 20*20 and I have
horizontal and vertical bars that are four pixels wide. The x and y
offset is 20 pixels.
Hope I explained it properly, here's the code:
void MyView::DrawGrid(CDC *dc)
{
static const int cell_width = 20;
static const int cell_height = 20;
static const int initial_x_offset = 20;
static const int initial_y_offset = 20;
static const int grid_bar_thickness = 4;
static CPen pen(PS_SOLID, grid_bar_thickness, RGB(0x00, 0x00, 0xFF));
dc->SelectObject(&pen);
/* Drawing vertical lines. */
int x = initial_x_offset;
int y = initial_y_offset;
for (int i = 0; i < (num_cols_ + 1); ++i)
{
dc->MoveTo(x, y);
dc->LineTo(x, grid_bar_thickness * (num_rows_ + 1) + num_rows_ *
cell_height);
x += cell_width + grid_bar_thickness;
}
/* Drawing horizontal lines. */
x = initial_x_offset;
y = initial_y_offset;
for (int i = 0; i < (num_rows_ + 1); ++i)
{
dc->MoveTo(x, y);
dc->LineTo(grid_bar_thickness * (num_cols_ + 1) + num_cols_ *
cell_width, y);
y += cell_height + grid_bar_thickness;
}
}
I'm testing a grid with 16 rows and 18 columns. I suspect I'm missing to
take something into account when calling LineTo() because the grid lines
are not as long as they should be, neither vertically nor horizontally.
Here's a screenshot of how it looks:
http://student.stunet.se/hivemind/grid.png
Where am I going wrong here? I've made sure the values of num_rows_ and
num_cols_ are correct using the debugger. Also, I'm getting the cells
coordinates correct I just have problems with the grid lines. :)
- Eric