Modern Apps git

← Modern-Apps.git

games-arrows: draw turns as rounded quarter-circle arcs

Commit
78c6a6300bc4620edba309cafe89ed683b678213
Author
Vayun Mathur <[email protected]>
When
2026-11-07 20:14 UTC (29 days ago)
Parents
e6c5fb2
The body path ran lineTo through each cell centre, so a turn
pivoted sharply on the corner cell's centre with only the stroke
join softening the outside edge.

Build the body with a new arrowBodyPath(): each interior point
where the polyline actually turns is replaced by an arc that
leaves the incoming straight tangentially and joins the outgoing
one the same way, drawn as a quadratic curve with its control
point on the corner. The radius is half a cell - the largest
quarter circle that fits inside a cell - clamped to half the
shorter neighbouring segment so adjacent arcs on a short hop
cannot overlap. Straight points keep plain lines.

Applies to settled and travelling arrows alike since both go
through drawArrowShape. New bitmap test pins that the exact
corner point is no longer stroked through while the arrow still
paints.

Diff

games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawing.kt
diff --git a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawing.kt b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawing.ktindex 9a94612685..5cae3ca8d6 100644--- a/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawing.kt+++ b/games/arrows/src/main/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawing.kt@@ -117,9 +117,10 @@ internal fun routePoint(route: List<Int>, at: Float, cols: Int, cell: Float): Of /**  * One arrow: its body as a single joined stroke, and an arrowhead at the head.  *- * The stroke runs through cell centres, so a turn in the polyline becomes a rounded corner. The tip- * reaches past the head's centre by [HEAD_REACH] to leave room for the barbs while staying inside its own- * cell rather than poking into the next one.+ * The stroke runs through cell centres, and a turn in the polyline is drawn as a rounded arc (see+ * [arrowBodyPath]) rather than a sharp corner. The tip reaches past the head's centre by+ * [HEAD_REACH] to leave room for the barbs while staying inside its own cell rather than poking+ * into the next one.  */ internal fun DrawScope.drawArrow(piece: ArrowPiece, cols: Int, cell: Float, color: Color) {     val centres = piece.cells.map { Offset((it % cols + 0.5f) * cell, (it / cols + 0.5f) * cell) }@@ -127,6 +128,54 @@ internal fun DrawScope.drawArrow(piece: ArrowPiece, cols: Int, cell: Float, colo     drawArrowShape(centres, heading, cell, color) } +/**+ * The body path through [points] (cell centres, ending at the arrow tip), with every turn+ * replaced by a rounded quarter-circle arc.+ *+ * The arc's radius follows the grid: half a cell, the largest quarter circle that fits inside+ * one cell, so a turn reads as the arrow bending around the corner cell instead of pivoting on+ * its centre. It is clamped to half the shorter neighbouring segment so two arcs on adjacent+ * corners of a short hop can never overlap and swallow the straight between them; a straight+ * (collinear) point keeps a plain line, and a degenerate zero-length neighbour is skipped.+ *+ * Quadratic curves stand in for the circular arc: with the control point on the corner itself+ * the curve leaves one straight tangentially and joins the next the same way, which at stroke+ * width is indistinguishable from the true arc while needing no angle bookkeeping per turn.+ */+internal fun arrowBodyPath(points: List<Offset>, cell: Float): Path {+    val path = Path()+    if (points.isEmpty()) return path+    path.moveTo(points.first().x, points.first().y)+    if (points.size == 1) return path++    val maxRadius = cell * 0.5f+    for (index in 1 until points.lastIndex) {+        val previous = points[index - 1]+        val corner = points[index]+        val next = points[index + 1]+        val inLength = kotlin.math.hypot(corner.x - previous.x, corner.y - previous.y)+        val outLength = kotlin.math.hypot(next.x - corner.x, next.y - corner.y)+        val radius = minOf(maxRadius, inLength / 2f, outLength / 2f)+        // Collinear (or degenerate) points have no turn to round: cross product ~ 0.+        val cross = (corner.x - previous.x) * (next.y - corner.y) -+            (corner.y - previous.y) * (next.x - corner.x)+        if (radius <= 0f || inLength <= 0f || outLength <= 0f ||+            kotlin.math.abs(cross) <= 0.001f * inLength * outLength+        ) {+            path.lineTo(corner.x, corner.y)+            continue+        }+        val inUnit = Offset((corner.x - previous.x) / inLength, (corner.y - previous.y) / inLength)+        val outUnit = Offset((next.x - corner.x) / outLength, (next.y - corner.y) / outLength)+        val arcStart = Offset(corner.x - inUnit.x * radius, corner.y - inUnit.y * radius)+        val arcEnd = Offset(corner.x + outUnit.x * radius, corner.y + outUnit.y * radius)+        path.lineTo(arcStart.x, arcStart.y)+        path.quadraticBezierTo(corner.x, corner.y, arcEnd.x, arcEnd.y)+    }+    path.lineTo(points.last().x, points.last().y)+    return path+}+ /**  * Draws the body through [centres] and an arrowhead pointing along [heading].  *@@ -146,13 +195,8 @@ internal fun DrawScope.drawArrowShape(     val head = centres.last()     val tip = Offset(head.x + unit.x * cell * 0.34f, head.y + unit.y * cell * 0.34f) -    val path = Path().apply {-        moveTo(centres.first().x, centres.first().y)-        for (point in centres.drop(1)) lineTo(point.x, point.y)-        lineTo(tip.x, tip.y)-    }     drawPath(-        path = path,+        path = arrowBodyPath(centres + tip, cell),         color = color,         style = Stroke(width = stroke, cap = StrokeCap.Round, join = StrokeJoin.Round),     )
games/arrows/src/test/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawingTest.kt
diff --git a/games/arrows/src/test/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawingTest.kt b/games/arrows/src/test/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawingTest.ktindex 43245ad6b1..33ad1877e7 100644--- a/games/arrows/src/test/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawingTest.kt+++ b/games/arrows/src/test/java/com/vayunmathur/games/arrows/ui/components/ArrowsBoardDrawingTest.kt@@ -107,6 +107,25 @@ class ArrowsBoardDrawingTest {         assertTrue(nonTransparentPixels(bitmap) > 5)     } +    @Test+    fun drawArrowShapeRoundsTheTurnInsteadOfPivotingOnTheCorner() {+        val bitmap = drawOnBitmap {+            // An L: right along y=100, then up at x=60. Cell is 40px, so the arc+            // radius is 20px and the curve passes ~8px inside the corner point.+            drawArrowShape(+                listOf(Offset(20f, 100f), Offset(60f, 100f), Offset(60f, 60f)),+                Offset(0f, -1f), 40f, Color.Black,+            )+        }+        assertTrue(nonTransparentPixels(bitmap) > 10, "rounded arrow should still paint")+        // A sharp polyline strokes straight through the corner centre; the arc cuts inside it,+        // leaving the exact corner point (well beyond the 2.2px half-stroke) unpainted.+        assertEquals(+            0, android.graphics.Color.alpha(bitmap.getPixel(60, 100)),+            "corner point should be cut by the arc, not stroked through",+        )+    }+     @Test     fun drawArrowShapeWithDiagonalAndZeroHeadings() {         val bitmap = drawOnBitmap {