From c5b3d2a31a8c88fc6c14248e5f71f9124f82d529 Mon Sep 17 00:00:00 2001 From: Julian Chang Date: Tue, 1 Dec 2026 07:26:18 +0900 Subject: [PATCH 1/2] Keep path area precise far from the origin --- .../org/polyfrost/compose/backend/PolyPathOps.kt | 12 +++++++----- .../org/polyfrost/compose/backend/PolyPathOpsTest.kt | 7 +++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/core/src/main/kotlin/org/polyfrost/compose/backend/PolyPathOps.kt b/core/src/main/kotlin/org/polyfrost/compose/backend/PolyPathOps.kt index d305b65..0229992 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/backend/PolyPathOps.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/backend/PolyPathOps.kt @@ -36,12 +36,14 @@ private fun oriented(contours: List, sign: Float): List // Twice the signed area private fun area(c: FloatArray): Float { - var sum = 0f - for (k in c.indices step 2) { - val j = (k + 2) % c.size - sum += c[k] * c[j + 1] - c[j] * c[k + 1] + if (c.size < 6) return 0f + val x0 = c[0] + val y0 = c[1] + var sum = 0.0 + for (k in 2 until c.size - 2 step 2) { + sum += (c[k] - x0).toDouble() * (c[k + 3] - y0) - (c[k + 2] - x0).toDouble() * (c[k + 1] - y0) } - return sum + return sum.toFloat() } private fun convex(contours: List): FloatArray? { diff --git a/core/src/test/kotlin/org/polyfrost/compose/backend/PolyPathOpsTest.kt b/core/src/test/kotlin/org/polyfrost/compose/backend/PolyPathOpsTest.kt index 15bb723..6506bc6 100644 --- a/core/src/test/kotlin/org/polyfrost/compose/backend/PolyPathOpsTest.kt +++ b/core/src/test/kotlin/org/polyfrost/compose/backend/PolyPathOpsTest.kt @@ -86,6 +86,13 @@ class PolyPathOpsTest { assertEquals(listOf(true, false, false), clipped.hits(7f, 7f, 12f, 12f, 2f, 2f)) } + @Test + fun intersectKeepsTinyBoxFarFromOrigin() { + val box = rect(1299.995f, 305.995f, 1300.005f, 306.005f) + assertFalse(op(rect(1100f, 286f, 1500f, 326f), box, PathOperation.Intersect).isEmpty) + assertTrue(op(rect(1100f, 286f, 1290f, 326f), box, PathOperation.Intersect).isEmpty) + } + @Test fun nonUniformRoundedBorderRenders() { val rec = FrameRecomposer(Dispatchers.Unconfined) {} From 590e3ecd9abc977c487956b9a1d0adaea7fdddde Mon Sep 17 00:00:00 2001 From: Julian Chang Date: Tue, 1 Dec 2026 07:57:53 +0900 Subject: [PATCH 2/2] Sum path area in double without shifting vertices --- .../kotlin/org/polyfrost/compose/backend/PolyPathOps.kt | 8 +++----- .../org/polyfrost/compose/backend/PolyPathOpsTest.kt | 6 ++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/core/src/main/kotlin/org/polyfrost/compose/backend/PolyPathOps.kt b/core/src/main/kotlin/org/polyfrost/compose/backend/PolyPathOps.kt index 0229992..779eab2 100644 --- a/core/src/main/kotlin/org/polyfrost/compose/backend/PolyPathOps.kt +++ b/core/src/main/kotlin/org/polyfrost/compose/backend/PolyPathOps.kt @@ -36,12 +36,10 @@ private fun oriented(contours: List, sign: Float): List // Twice the signed area private fun area(c: FloatArray): Float { - if (c.size < 6) return 0f - val x0 = c[0] - val y0 = c[1] var sum = 0.0 - for (k in 2 until c.size - 2 step 2) { - sum += (c[k] - x0).toDouble() * (c[k + 3] - y0) - (c[k + 2] - x0).toDouble() * (c[k + 1] - y0) + for (k in c.indices step 2) { + val j = (k + 2) % c.size + sum += c[k].toDouble() * c[j + 1] - c[j].toDouble() * c[k + 1] } return sum.toFloat() } diff --git a/core/src/test/kotlin/org/polyfrost/compose/backend/PolyPathOpsTest.kt b/core/src/test/kotlin/org/polyfrost/compose/backend/PolyPathOpsTest.kt index 6506bc6..d91fe47 100644 --- a/core/src/test/kotlin/org/polyfrost/compose/backend/PolyPathOpsTest.kt +++ b/core/src/test/kotlin/org/polyfrost/compose/backend/PolyPathOpsTest.kt @@ -93,6 +93,12 @@ class PolyPathOpsTest { assertTrue(op(rect(1100f, 286f, 1290f, 326f), box, PathOperation.Intersect).isEmpty) } + @Test + fun intersectKeepsStretchedTriangle() { + val triangle = Path().apply { moveTo(-1e8f, -1e8f); lineTo(0f, 0f); lineTo(0f, 1f); close() } + assertFalse(op(triangle, triangle, PathOperation.Intersect).isEmpty) + } + @Test fun nonUniformRoundedBorderRenders() { val rec = FrameRecomposer(Dispatchers.Unconfined) {}