From eeec6b867af104a93a615a285a29fab91d8709d4 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 14 Aug 2023 17:13:01 +0100 Subject: [PATCH] Untangle: make snapping grid invariant under window resize In grid-snapping mode, Untangle was still recording vertex positions in increments of one pixel. This meant that if you positioned vertices on a small window, then enlarged the window and positioned more vertices, the two sets of vertices generally wouldn't line up with one another. This was annoying, and obviously silly when Untangle has a resolution-independent co-ordinate system. So now the snapped positions are recorded in a form that doesn't depend on the tilesize. --- untangle.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/untangle.c b/untangle.c index b23f9cd..2c9aaae 100644 --- a/untangle.c +++ b/untangle.c @@ -1159,18 +1159,18 @@ static void place_dragged_point(const game_state *state, game_ui *ui, */ int d = state->params.n - 1; + /* Calculate position in terms of the snapping grid. */ x = d * x / (state->w * ds->tilesize); - x *= (state->w * ds->tilesize) / d; - x += (state->w * ds->tilesize) / (2*d); - y = d * y / (state->h * ds->tilesize); - y *= (state->h * ds->tilesize) / d; - y += (state->h * ds->tilesize) / (2*d); + /* Convert to standard co-ordinates, applying a half-square offset. */ + ui->newpoint.x = (x * 2 + 1) * state->w; + ui->newpoint.y = (y * 2 + 1) * state->h; + ui->newpoint.d = d * 2; + } else { + ui->newpoint.x = x; + ui->newpoint.y = y; + ui->newpoint.d = ds->tilesize; } - - ui->newpoint.x = x; - ui->newpoint.y = y; - ui->newpoint.d = ds->tilesize; } static char *interpret_move(const game_state *state, game_ui *ui,