Modern Apps git

← Modern-Apps.git

library/ink: the eraser hit-tests the stroke centreline, not its bounding box

Commit
c212bb5d60ca348798161d179ccb52c4dd8b0e71
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:18 UTC (4 days ago)
Parents
b643b4d
eraseAt hit-tested only the stroke's bounding box expanded
by 20px and took the last bbox hit. A touch at (0,100) over a
diagonal stroke (0,0)->(100,100) โ€” 70.7px from the ink โ€”
erased the whole stroke; and a later-drawn stroke whose box
merely covered the touch shadowed a stroke actually touched
underneath. bbox geometry is not ink geometry.

eraserTargetIndex now hits only when the touch is within
20px + brush.size/2 of the sampled centreline
(point-to-segment distance, endpoints clamped; single-point
dots use point distance); among real hits the topmost still
wins, and empty-point strokes never hit. Six regression
tests cover the bbox-corner miss, on/near/off distances, the
shadowing case, topmost-wins, dots and empty inputs.
library/ocr's result mapping and ink serialization were
audited and cleared. Verified: :library:ink:
testDebugUnitTest and lint BUILD SUCCESSFUL (ktlint findings
are the standing backlog on untouched code).

Diff

library/ink/src/main/java/com/vayunmathur/library/ink/InkCanvasView.kt
diff --git a/library/ink/src/main/java/com/vayunmathur/library/ink/InkCanvasView.kt b/library/ink/src/main/java/com/vayunmathur/library/ink/InkCanvasView.ktindex 4e8d28f394..08c1e50378 100644--- a/library/ink/src/main/java/com/vayunmathur/library/ink/InkCanvasView.kt+++ b/library/ink/src/main/java/com/vayunmathur/library/ink/InkCanvasView.kt@@ -127,6 +127,73 @@ private const val SELECTION_PADDING = 4f private const val SELECTION_TEXT_EXTRA = 8f private const val ERASER_HIT_RADIUS = 20f +/**+ * Index of the stroke the eraser at ([touchX], [touchY]) removes, or null.+ *+ * A stroke is hit only when the touch is within [hitRadius] of the ink+ * itself: the sampled centreline (segments between consecutive points, or+ * the single point of a dot stroke), widened by half the brush size. Testing+ * the stroke's bounding box instead would erase it from empty corners of+ * the box โ€” e.g. the (0, 100) corner of a (0, 0)-(100, 100) diagonal. Among+ * strokes actually hit, the last (topmost-drawn) wins, matching the draw+ * order in [FinishedStrokesView.onDraw].+ */+internal fun eraserTargetIndex(+    pointSets: List<List<Pair<Float, Float>>>,+    brushSizes: List<Float>,+    touchX: Float,+    touchY: Float,+    hitRadius: Float = ERASER_HIT_RADIUS,+): Int? {+    var target: Int? = null+    pointSets.forEachIndexed { index, points ->+        if (points.isEmpty()) return@forEachIndexed+        val threshold = hitRadius + (brushSizes.getOrNull(index) ?: 0f) / 2f+        val nearest =+            if (points.size == 1) {+                distanceBetween(touchX, touchY, points[0].first, points[0].second)+            } else {+                points.zipWithNext().minOf { (a, b) ->+                    distanceToSegment(touchX, touchY, a.first, a.second, b.first, b.second)+                }+            }+        if (nearest <= threshold) target = index+    }+    return target+}++private fun distanceBetween(+    x1: Float,+    y1: Float,+    x2: Float,+    y2: Float,+): Float {+    val dx = x1 - x2+    val dy = y1 - y2+    return kotlin.math.sqrt(dx * dx + dy * dy)+}++/** Distance from a point to segment (ax, ay)-(bx, by), clamped to its ends. */+private fun distanceToSegment(+    px: Float,+    py: Float,+    ax: Float,+    ay: Float,+    bx: Float,+    by: Float,+): Float {+    val abx = bx - ax+    val aby = by - ay+    val lengthSq = abx * abx + aby * aby+    val t =+        if (lengthSq == 0f) {+            0f+        } else {+            (((px - ax) * abx + (py - ay) * aby) / lengthSq).coerceIn(0f, 1f)+        }+    return distanceBetween(px, py, ax + t * abx, ay + t * aby)+}+ private class InkState {     var currentBrush: Brush? = null     var enabled: Boolean = true@@ -223,15 +290,16 @@ private class StrokeTouchListener(     }      private fun eraseAt(touchX: Float, touchY: Float) {-        finishedStrokesView.strokes.lastOrNull { stroke ->-            stroke.shape.computeBoundingBox()?.let { box ->-                box.xMin <= touchX + ERASER_HIT_RADIUS &&-                    box.xMax >= touchX - ERASER_HIT_RADIUS &&-                    box.yMin <= touchY + ERASER_HIT_RADIUS &&-                    box.yMax >= touchY - ERASER_HIT_RADIUS-            } ?: false-        }?.let { hitStroke ->-            state.onStrokeErased?.invoke(hitStroke)+        val strokes = finishedStrokesView.strokes+        val pointSets = strokes.map { stroke ->+            (0 until stroke.inputs.size).map { i ->+                val input = stroke.inputs[i]+                input.x to input.y+            }+        }+        val brushSizes = strokes.map { it.brush.size }+        eraserTargetIndex(pointSets, brushSizes, touchX, touchY)?.let { index ->+            state.onStrokeErased?.invoke(strokes[index])         }     } 
library/ink/src/test/java/com/vayunmathur/library/ink/InkEraserHitTest.kt
diff --git a/library/ink/src/test/java/com/vayunmathur/library/ink/InkEraserHitTest.kt b/library/ink/src/test/java/com/vayunmathur/library/ink/InkEraserHitTest.ktnew file mode 100644index 0000000000..e3e164f50c--- /dev/null+++ b/library/ink/src/test/java/com/vayunmathur/library/ink/InkEraserHitTest.kt@@ -0,0 +1,92 @@+package com.vayunmathur.library.ink++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++/**+ * Eraser hit-testing over sampled stroke centrelines. Regression: the eraser+ * used to hit-test only each stroke's bounding box, so touching an empty+ * corner of the box erased a stroke the eraser never came near.+ */+class InkEraserHitTest {+    @Test fun emptyCornerOfDiagonalBoundingBoxDoesNotErase() {+        // Diagonal ink from (0, 0) to (100, 100); (0, 100) is inside its+        // bounding box but ~70.7px from the stroke itself.+        val index =+            eraserTargetIndex(+                pointSets = listOf(listOf(0f to 0f, 100f to 100f)),+                brushSizes = listOf(4f),+                touchX = 0f,+                touchY = 100f,+            )+        assertNull(index)+    }++    @Test fun touchOnAndNearStrokeErasesButFarTouchDoesNot() {+        val points = listOf(listOf(0f to 0f, 100f to 100f))+        val sizes = listOf(4f)+        // On the centreline.+        assertEquals(0, eraserTargetIndex(points, sizes, 50f, 50f))+        // ~14.1px off the centreline: within 20px radius + half the brush.+        assertEquals(0, eraserTargetIndex(points, sizes, 70f, 50f))+        // ~30.4px off the centreline: outside the widened stroke.+        assertNull(eraserTargetIndex(points, sizes, 93f, 50f))+    }++    @Test fun laterBoundingBoxOnlyStrokeDoesNotShadowStrokeActuallyTouched() {+        // Touch (10, 50) lies on the horizontal stroke and inside the later+        // diagonal's bounding box, but ~28.3px from the diagonal itself.+        // The old last-bounding-box-hit logic erased the diagonal.+        val index =+            eraserTargetIndex(+                pointSets = listOf(+                    listOf(0f to 50f, 100f to 50f),+                    listOf(0f to 0f, 100f to 100f),+                ),+                brushSizes = listOf(4f, 4f),+                touchX = 10f,+                touchY = 50f,+            )+        assertEquals(0, index)+    }++    @Test fun topmostStrokeWinsWhenSeveralAreActuallyHit() {+        val index =+            eraserTargetIndex(+                pointSets = listOf(+                    listOf(0f to 50f, 100f to 50f),+                    listOf(0f to 52f, 100f to 52f),+                ),+                brushSizes = listOf(4f, 4f),+                touchX = 50f,+                touchY = 51f,+            )+        assertEquals(1, index)+    }++    @Test fun dotStrokeAndSegmentEndpointsUsePointDistance() {+        val dot = listOf(listOf(50f to 50f))+        // 20px from the dot's centre: within 20px radius + half of size 10.+        assertEquals(0, eraserTargetIndex(dot, listOf(10f), 70f, 50f))+        // 30px away: outside.+        assertNull(eraserTargetIndex(dot, listOf(10f), 80f, 50f))++        val segment = listOf(listOf(0f to 0f, 100f to 0f))+        // Past the end but 10px from the endpoint: still a hit.+        assertEquals(0, eraserTargetIndex(segment, listOf(4f), 110f, 0f))+        assertNull(eraserTargetIndex(segment, listOf(4f), 130f, 0f))+    }++    @Test fun emptyInputsNeverHit() {+        assertNull(+            eraserTargetIndex(+                pointSets = listOf(emptyList(), listOf(0f to 0f)),+                brushSizes = listOf(4f, 4f),+                touchX = 100f,+                touchY = 100f,+            ),+        )+        assertNull(eraserTargetIndex(emptyList(), emptyList(), 0f, 0f))+    }+}