pipes: interpolate fast strokes and cut back on re-entry (#ux-audit)
- Commit
ba8fd2f24fd7bb2b10f9b1fc878f0bab0d3f02b4- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:18 UTC (23 days ago)
- Parents
5a7522d
Symptom: fast or diagonal swipes stalled part-way: the pipe stopped
growing and the finger had to come back and retrace slowly. Fast
backtracking stalled the same way.
Root cause:
- GameBoard fed only the latest pointer position of each touch event
to the ViewModel. A fast swipe moves more than a cell between events
(and Android batches intermediate samples into `historical`), so the
next reported cell was often two or more cells from the stroke's end.
- PipesViewModel.extendPath accepted only cells adjacent to the end,
and backtracked only when the finger hit exactly the previous cell,
one step per event. Anything else was silently dropped.
Fix:
- BoardGeometry.cellsAlong walks a straight movement in quarter-cell
samples and returns every cell crossed, in order. The gesture loop
now runs it from the last processed position through each
historical sample and the current position, calling extendPath once
per crossed cell.
- The stroke rules move out of the ViewModel into a new pure
domain/StrokeRules (extend, isComplete, isValidStep; bridge,
endpoint and ownership checks unchanged). extend now:
- cuts the stroke back to any cell already on it that the finger
re-enters, however far back;
- reaches a cell two steps away (a corner-to-corner hop through a
diagonal, or a skipped cell) through a shared neighbour when that
neighbour is a valid step, never finishing on the partner dot
mid-route;
- keeps the existing "stroke is done once it reaches its partner
dot" lock.
The ViewModel drops seven private helpers as a result.
User-visible effect: swipes of any speed or angle draw continuously,
and swiping back over the pipe shortens it at once.
Verification: new StrokeRulesTest (pure JVM) covers adjacent steps,
diagonal routing including a blocked neighbour and the partner-dot
case, skipped cells, out-of-range cells, cut-back, blocking rules and
the completed-stroke lock; StrokeSamplingTest covers cellsAlong for a
four-cell single event, a diagonal (only neighbouring hops), back and
forth, and in-cell jitter; PipesStrokeTest checks the same through the
ViewModel. PipesViewModelTest's "far cell is rejected" case now picks
a cell more than two steps away, since two-step cells are now routed.Diff
games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/StrokeRules.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/StrokeRules.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/StrokeRules.ktnew file mode 100644index 0000000000..2cd898e292--- /dev/null+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/domain/StrokeRules.kt@@ -0,0 +1,91 @@+package com.vayunmathur.games.pipes.domain++import com.vayunmathur.games.pipes.data.CellPos+import com.vayunmathur.games.pipes.data.LevelData+import kotlin.math.abs++/**+ * Pure rules for growing one colour's stroke as the finger moves.+ *+ * [owners] is the board's cell ownership the stroke is checked against (the committed+ * board with the stroke's own colour already lifted when it was grabbed by an endpoint).+ */+object StrokeRules {++ /**+ * The stroke after the finger enters [cell].+ *+ * - Re-entering a cell already on the stroke cuts the stroke back to it, however far+ * back it is, so a fast backtrack never stalls.+ * - An adjacent free cell is appended.+ * - A cell two steps away through a shared neighbour (a diagonal move, or a sample that+ * skipped a cell) is reached through that neighbour.+ * - A finished stroke (it reached its partner dot) stays put.+ */+ fun extend(+ level: LevelData,+ owners: Map<CellPos, Int>,+ color: Int,+ path: List<CellPos>,+ cell: CellPos,+ ): List<CellPos> {+ if (path.isEmpty() || cell !in level.cells || isComplete(level, color, path)) return path+ val idx = path.indexOf(cell)+ if (idx >= 0) return path.subList(0, idx + 1).toList()+ if (isValidStep(level, owners, color, path, cell)) return path + cell+ val via = level.adjacency[path.last()].orEmpty().firstOrNull { n ->+ cell in level.adjacency[n].orEmpty() &&+ isValidStep(level, owners, color, path, n) &&+ !isComplete(level, color, path + n) &&+ isValidStep(level, owners, color, path + n, cell)+ }+ return if (via != null) path + via + cell else path+ }++ /** Whether [path] has reached its partner dot (pairs) or visited every dot (triplets). */+ fun isComplete(level: LevelData, color: Int, path: List<CellPos>): Boolean {+ val endpointCells = level.endpoints.find { it.colorIndex == color }?.cells ?: return false+ if (path.isEmpty() || path.first() !in endpointCells || path.last() !in endpointCells) return false+ if (path.first() == path.last()) return false+ return if (endpointCells.size == 2) {+ val paired = if (path.first() == endpointCells[0]) endpointCells[1] else endpointCells[0]+ path.last() == paired+ } else {+ endpointCells.all { it in path }+ }+ }++ /** Whether [cell] may follow [path] directly. */+ fun isValidStep(+ level: LevelData,+ owners: Map<CellPos, Int>,+ color: Int,+ path: List<CellPos>,+ cell: CellPos,+ ): Boolean {+ if (cell in path) return false+ val neighbors = level.adjacency[path.last()] ?: return false+ if (cell !in neighbors || isForeignEndpoint(level, color, cell)) return false+ if (!isStraightThroughBridge(level, path, cell)) return false+ val owner = owners[cell] ?: return true+ return owner == color || cell in level.bridges+ }++ private fun isForeignEndpoint(level: LevelData, color: Int, cell: CellPos): Boolean =+ level.endpoints.any { it.colorIndex != color && cell in it.cells }++ /**+ * Bridges read as an overpass only when both pipes run straight through. When the path+ * leaves a bridge cell, the exit must continue the entry line, so a pipe cannot corner+ * on the crossing. Long-range exits (portal/wrap jumps) are not grid lines and skip+ * the check.+ */+ private fun isStraightThroughBridge(level: LevelData, path: List<CellPos>, cell: CellPos): Boolean {+ val origin = path.lastOrNull() ?: return true+ if (origin !in level.bridges || path.size < 2) return true+ if (abs(cell.row - origin.row) + abs(cell.col - origin.col) != 1) return true+ val entry = path[path.size - 2]+ return (entry.row == origin.row && origin.row == cell.row) ||+ (entry.col == origin.col && origin.col == cell.col)+ }+}
games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.ktindex d27ad9e6e0..a2afd04d9d 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/platform/PipesViewModel.kt@@ -6,6 +6,7 @@ import androidx.lifecycle.viewModelScope import com.vayunmathur.games.pipes.data.CellPos import com.vayunmathur.games.pipes.data.CompletedLevelsRepository import com.vayunmathur.games.pipes.domain.DailyLevels+import com.vayunmathur.games.pipes.domain.StrokeRules import com.vayunmathur.games.pipes.data.LevelData import com.vayunmathur.games.pipes.data.LevelPack import com.vayunmathur.library.util.LevelStats@@ -263,89 +264,9 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), val s = _uiState.value val activeColor = s.activeColor ?: return val levelData = s.levelData ?: return- if (s.isLevelWon || cell !in levelData.cells) return- val currentPath = s.activePath- if (currentPath.isEmpty() || isAtPairedEndpoint(levelData, activeColor, currentPath)) return- if (tryBacktrack(currentPath, cell)) return- if (isValidStep(s, levelData, activeColor, currentPath, cell)) {- _uiState.update { it.copy(activePath = currentPath + cell) }- }- }-- private fun isAtPairedEndpoint(- levelData: LevelData,- activeColor: Int,- path: List<CellPos>,- ): Boolean {- val endpointCells = levelData.endpoints.find { it.colorIndex == activeColor }?.cells- ?: return false- if (path.first() !in endpointCells || path.last() !in endpointCells) return false- if (path.first() == path.last()) return false- // Pairs: the other end. Triplets (chain): all dots visited, ending on a dot.- return if (endpointCells.size == 2) {- val paired = if (path.first() == endpointCells[0]) endpointCells[1] else endpointCells[0]- path.last() == paired- } else {- endpointCells.all { it in path }- }- }-- private fun tryBacktrack(currentPath: List<CellPos>, cell: CellPos): Boolean {- if (currentPath.size < BACKTRACK_MIN_LENGTH || cell != currentPath[currentPath.size - 2]) {- return false- }- _uiState.update { it.copy(activePath = currentPath.dropLast(1)) }- return true- }-- private fun isValidStep(- s: PipesUiState,- levelData: LevelData,- activeColor: Int,- path: List<CellPos>,- cell: CellPos,- ): Boolean {- if (cell in path) return false- val neighbors = levelData.adjacency[path.last()] ?: return false- if (cell !in neighbors || isForeignEndpoint(levelData, activeColor, cell)) return false- if (!isStraightThroughBridge(levelData, path, cell)) return false- return isFreeForColor(s, levelData, activeColor, cell)- }-- private fun isForeignEndpoint(levelData: LevelData, activeColor: Int, cell: CellPos): Boolean =- levelData.endpoints.filter { it.colorIndex != activeColor }- .flatMap { it.cells }- .contains(cell)-- /**- * Bridges read as an overpass only when both pipes run straight through. When the path- * leaves a bridge cell, the exit must continue the entry line, so a pipe cannot corner- * on the crossing. Long-range exits (portal/wrap jumps) are not grid lines and skip- * the check.- */- private fun isStraightThroughBridge(- levelData: LevelData,- path: List<CellPos>,- cell: CellPos,- ): Boolean {- val origin = path.lastOrNull() ?: return true- if (origin !in levelData.bridges || path.size < 2) return true- if (kotlin.math.abs(cell.row - origin.row) + kotlin.math.abs(cell.col - origin.col) != 1) {- return true- }- val entry = path[path.size - 2]- return (entry.row == origin.row && origin.row == cell.row) ||- (entry.col == origin.col && origin.col == cell.col)- }-- private fun isFreeForColor(- s: PipesUiState,- levelData: LevelData,- activeColor: Int,- cell: CellPos,- ): Boolean {- val owner = s.gameState.cellOwner[cell] ?: return true- return owner == activeColor || cell in levelData.bridges+ if (s.isLevelWon) return+ val next = StrokeRules.extend(levelData, s.gameState.cellOwner, activeColor, s.activePath, cell)+ if (next != s.activePath) _uiState.update { it.copy(activePath = next) } } override fun commitDraw() {@@ -539,8 +460,5 @@ class PipesViewModel(application: Application) : AndroidViewModel(application), /** Sentinel [PipesUiState.packIndex] for "no level loaded". */ private const val NO_PACK_INDEX = -1-- /** Minimum path length for a backtrack step (needs a cell to step back from). */- private const val BACKTRACK_MIN_LENGTH = 2 } }
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardGeometry.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardGeometry.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardGeometry.ktindex 73e4eb0f6a..ed3079cb05 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardGeometry.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/BoardGeometry.kt@@ -5,6 +5,7 @@ import androidx.compose.ui.geometry.Rect import androidx.compose.ui.geometry.Size import com.vayunmathur.games.pipes.data.CellPos import com.vayunmathur.games.pipes.data.LevelData+import kotlin.math.ceil /** * The board's layout in cell units, independent of the pixel size it is drawn at.@@ -126,8 +127,31 @@ class BoardGeometry(val layout: BoardLayout, val cellSizePx: Float) { )?.key } + /**+ * Every cell a straight finger movement from [from] to [to] passes over, in order and+ * without consecutive repeats (the cell under [from] itself is not included unless the+ * path returns to it). Touch events arrive far apart on a fast swipe; feeding only+ * their end points to the stroke skipped cells and stalled it.+ */+ fun cellsAlong(from: Offset, to: Offset): List<CellPos> {+ if (cellSizePx <= 0f) return listOfNotNull(hitTestDrag(to))+ val distance = (to - from).getDistance()+ val steps = ceil(distance / (cellSizePx * SAMPLE_FRACTION)).toInt().coerceIn(1, MAX_SAMPLES)+ val out = ArrayList<CellPos>()+ val start = hitTestDrag(from)+ for (i in 1..steps) {+ val cell = hitTestDrag(from + (to - from) * (i / steps.toFloat())) ?: continue+ if (cell != (out.lastOrNull() ?: start)) out += cell+ }+ return out+ }+ private companion object { const val HEX_HIT_RADIUS = 0.62f++ /** Sample spacing along a movement, as a fraction of a cell: fine enough to never jump a cell. */+ const val SAMPLE_FRACTION = 0.25f+ const val MAX_SAMPLES = 4096 } }
games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt
diff --git a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.ktindex 877e22237b..fae58ad2b4 100644--- a/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt+++ b/games/pipes/src/main/java/com/vayunmathur/games/pipes/ui/GameBoard.kt@@ -66,13 +66,19 @@ fun GameBoard( down.consume() val start = geometry.hitTest(down.position) ?: return@awaitEachGesture currentStartDraw(start)+ var last = down.position try { while (true) { val event = awaitPointerEvent() val change = event.changes.firstOrNull { it.id == down.id } event.changes.forEach { it.consume() } if (change == null || !change.pressed) break- geometry.hitTestDrag(change.position)?.let { currentExtendPath(it) }+ // Walk every sample since the last event (batched historical+ // points too), cell by cell, so fast strokes never skip cells.+ for (p in change.historical.map { it.position } + change.position) {+ geometry.cellsAlong(last, p).forEach { currentExtendPath(it) }+ last = p+ } } } finally { // Also on cancellation (the board resized mid-stroke): never leave a
games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/StrokeRulesTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/StrokeRulesTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/StrokeRulesTest.ktnew file mode 100644index 0000000000..aedf260490--- /dev/null+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/domain/StrokeRulesTest.kt@@ -0,0 +1,80 @@+package com.vayunmathur.games.pipes.domain++import com.vayunmathur.games.pipes.data.CellPos+import com.vayunmathur.games.pipes.data.EndpointPair+import com.vayunmathur.games.pipes.data.LevelData+import com.vayunmathur.games.pipes.data.computeAdjacency+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++class StrokeRulesTest {+ private fun p(r: Int, c: Int) = CellPos(r, c)+ private val cells = buildSet { for (r in 0 until 5) for (c in 0 until 5) add(p(r, c)) }++ /** Red 0: (0,0)-(4,4). Blue 1: (0,4)-(4,0). */+ private val level = LevelData(+ "s", 5, 5, cells, computeAdjacency(cells), null,+ listOf(EndpointPair(0, listOf(p(0, 0), p(4, 4))), EndpointPair(1, listOf(p(0, 4), p(4, 0)))),+ emptySet(), 2,+ )++ private fun ext(path: List<CellPos>, cell: CellPos, owners: Map<CellPos, Int> = emptyMap()) =+ StrokeRules.extend(level, owners, 0, path, cell)++ @Test fun adjacentStepAppends() {+ assertEquals(listOf(p(0, 0), p(0, 1)), ext(listOf(p(0, 0)), p(0, 1)))+ }++ @Test fun diagonalStepRoutesThroughAnOrthogonalNeighbour() {+ val out = ext(listOf(p(0, 0), p(1, 0), p(1, 1)), p(2, 2))+ assertEquals(5, out.size)+ assertEquals(p(2, 2), out.last())+ val via = out[3]+ assertTrue(via == p(1, 2) || via == p(2, 1), "via=$via")+ }++ @Test fun diagonalAvoidsABlockedNeighbour() {+ // (2,1) belongs to blue, so the route must go through (1,2).+ val out = ext(listOf(p(0, 0), p(1, 0), p(1, 1)), p(2, 2), owners = mapOf(p(2, 1) to 1))+ assertEquals(listOf(p(0, 0), p(1, 0), p(1, 1), p(1, 2), p(2, 2)), out)+ }++ @Test fun skippedCellOnAStraightLineIsFilledIn() {+ assertEquals(listOf(p(0, 0), p(0, 1), p(0, 2)), ext(listOf(p(0, 0)), p(0, 2)))+ }++ @Test fun cellsThreeStepsAwayAreIgnored() {+ assertEquals(listOf(p(0, 0)), ext(listOf(p(0, 0)), p(0, 3)))+ }++ @Test fun reEnteringAnEarlierCellCutsBackToIt() {+ val path = listOf(p(0, 0), p(0, 1), p(0, 2), p(1, 2), p(2, 2))+ assertEquals(listOf(p(0, 0), p(0, 1)), ext(path, p(0, 1)))+ assertEquals(listOf(p(0, 0)), ext(path, p(0, 0)))+ assertEquals(path, ext(path, p(2, 2)))+ }++ @Test fun foreignEndpointsAndOwnedCellsBlock() {+ assertEquals(listOf(p(0, 3)), StrokeRules.extend(level, emptyMap(), 0, listOf(p(0, 3)), p(0, 4)))+ assertEquals(listOf(p(0, 0)), ext(listOf(p(0, 0)), p(0, 1), owners = mapOf(p(0, 1) to 1)))+ // No route around through a blocked neighbour either+ assertEquals(listOf(p(0, 0)), ext(listOf(p(0, 0)), p(0, 2), owners = mapOf(p(0, 1) to 1)))+ }++ @Test fun completedStrokeStaysPut() {+ val path = listOf(p(0, 0), p(1, 0), p(2, 0), p(3, 0), p(3, 1), p(4, 1), p(4, 2), p(4, 3), p(4, 4))+ assertTrue(StrokeRules.isComplete(level, 0, path))+ assertEquals(path, ext(path, p(3, 4)))+ assertEquals(path, ext(path, p(4, 2)))+ assertFalse(StrokeRules.isComplete(level, 0, listOf(p(0, 0))))+ }++ @Test fun diagonalNeverRoutesThroughThePartnerDot() {+ // From (4,3) to (3,4) the shared neighbours are (3,3) and the partner dot (4,4);+ // finishing on the dot would strand the target cell, so the route takes (3,3).+ val path = listOf(p(0, 0), p(0, 1), p(1, 1), p(2, 1), p(3, 1), p(4, 1), p(4, 2), p(4, 3))+ assertEquals(path + p(3, 3) + p(3, 4), ext(path, p(3, 4)))+ }+}
games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.ktindex 22ec28f945..d968824336 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesStrokeTest.kt@@ -68,4 +68,23 @@ class PipesStrokeTest { vm.onUndo() assertTrue(vm.uiState.value.gameState.paths.isEmpty()) }++ @Test fun skippedAndBacktrackedCellsAreHandledInOneEvent() {+ val vm = vm()+ val level = vm.level+ val ep = level.endpoints[0]+ val a = level.freeNeighbour(ep.cells[0])+ val b = level.freeNeighbour(a, setOf(ep.cells[0]))+ vm.startDraw(ep.cells[0])+ // The finger landed two cells away in one event: the cell in between is filled in.+ vm.extendPath(b)+ val stroke = vm.uiState.value.activePath+ assertEquals(3, stroke.size)+ assertEquals(b, stroke.last())+ assertTrue(stroke[1] in level.adjacency.getValue(ep.cells[0]) && b in level.adjacency.getValue(stroke[1]))+ // A fast swipe straight back to the dot cuts the whole stroke back at once.+ vm.extendPath(ep.cells[0])+ assertEquals(listOf(ep.cells[0]), vm.uiState.value.activePath)+ vm.commitDraw()+ } }
games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.ktindex 7fb9e43718..2d9ac69b3e 100644--- a/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/platform/PipesViewModelTest.kt@@ -106,7 +106,9 @@ class PipesViewModelTest { assertEquals(ep.colorIndex, vm.uiState.value.activeColor) assertEquals(listOf(start), vm.uiState.value.activePath) // Non-adjacent and foreign-endpoint steps are rejected- val far = level.cells.first { it != start && it !in (level.adjacency[start] ?: emptyList()) && level.endpoints.none { e -> it in e.cells } }+ // Far = not adjacent and not two steps away either (two-step moves route through the shared neighbour)+ val startAdj = level.adjacency[start] ?: emptyList()+ val far = level.cells.first { c -> c != start && c !in startAdj && (level.adjacency[c] ?: emptyList()).none { it in startAdj } && level.endpoints.none { e -> c in e.cells } } vm.extendPath(far); assertEquals(listOf(start), vm.uiState.value.activePath) val foreign = level.endpoints[1].cells[0] if (foreign !in (level.adjacency[start] ?: emptyList())) { /* adjacency guard also covers */ }
games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/StrokeSamplingTest.kt
diff --git a/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/StrokeSamplingTest.kt b/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/StrokeSamplingTest.ktnew file mode 100644index 0000000000..58fef3156e--- /dev/null+++ b/games/pipes/src/test/java/com/vayunmathur/games/pipes/ui/StrokeSamplingTest.kt@@ -0,0 +1,49 @@+package com.vayunmathur.games.pipes.ui++import androidx.compose.ui.geometry.Offset+import com.vayunmathur.games.pipes.data.CellPos+import com.vayunmathur.games.pipes.data.EndpointPair+import com.vayunmathur.games.pipes.data.LevelData+import com.vayunmathur.games.pipes.data.computeAdjacency+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++class StrokeSamplingTest {+ private val cells = buildSet { for (r in 0 until 5) for (c in 0 until 5) add(CellPos(r, c)) }+ private val geo = BoardGeometry(+ BoardLayout(+ LevelData(+ "s", 5, 5, cells, computeAdjacency(cells), null,+ listOf(EndpointPair(0, listOf(CellPos(0, 0), CellPos(4, 4)))), emptySet(), 1,+ )+ ),+ 100f,+ )++ @Test fun fastHorizontalSwipeVisitsEveryCell() {+ // One touch event spanning four cells.+ val cells = geo.cellsAlong(Offset(50f, 50f), Offset(450f, 50f))+ assertEquals((1..4).map { CellPos(0, it) }, cells)+ }++ @Test fun diagonalSwipeOnlyEverMovesToNeighbouringCells() {+ val cells = geo.cellsAlong(Offset(50f, 50f), Offset(450f, 430f))+ assertEquals(CellPos(4, 4), cells.last())+ var prev = CellPos(0, 0)+ for (c in cells) {+ val d = kotlin.math.abs(c.row - prev.row) + kotlin.math.abs(c.col - prev.col)+ // Sampling may hop corner-to-corner (StrokeRules routes those), never further.+ assertTrue(d in 1..2 && kotlin.math.abs(c.row - prev.row) <= 1 && kotlin.math.abs(c.col - prev.col) <= 1, "$prev -> $c")+ prev = c+ }+ }++ @Test fun backAndForthIsKeptInOrder() {+ assertEquals(listOf(CellPos(0, 1), CellPos(0, 0)), geo.cellsAlong(Offset(50f, 50f), Offset(150f, 50f)) + geo.cellsAlong(Offset(150f, 50f), Offset(50f, 50f)))+ }++ @Test fun stayingInsideACellYieldsNothing() {+ assertEquals(emptyList(), geo.cellsAlong(Offset(10f, 10f), Offset(90f, 90f)))+ }+}