shithub: puzzles

Download patch

ref: eeec6b867af104a93a615a285a29fab91d8709d4
parent: 899c7c41efb2bcc8da8121cc7a8f4fccf5eb9b1e
author: Ben Harris <[email protected]>
date: Mon Aug 14 13:13:01 EDT 2023

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.

--- a/untangle.c
+++ b/untangle.c
@@ -1159,18 +1159,18 @@
          */
         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,