Conway: Distribute leftover space equally on all sides

This draws the universe centered rather than just putting all leftover
space on the right and bottom sides until the window is large enough to
be completely filled with cells again.
This commit is contained in:
Linus Groh 2021-01-01 23:11:07 +01:00 committed by Andreas Kling
parent 849e2ded24
commit cf2c215def
Notes: sideshowbarker 2024-07-19 00:14:49 +09:00

View file

@ -103,12 +103,14 @@ void Game::paint_event(GUI::PaintEvent& event)
painter.fill_rect(event.rect(), m_dead_color);
auto game_rect = rect();
auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows);
auto x_margin = (game_rect.width() - (cell_size.width() * m_columns)) / 2;
auto y_margin = (game_rect.height() - (cell_size.height() * m_rows)) / 2;
for (int y = 0; y < m_rows; y++) {
for (int x = 0; x < m_columns; x++) {
Gfx::IntRect rect {
x * cell_size.width(),
y * cell_size.height(),
x * cell_size.width() + x_margin,
y * cell_size.height() + y_margin,
cell_size.width(),
cell_size.height()
};