From dda653b2cf9b56447517aee3757dc42209bdb2ad Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sun, 21 Apr 2024 09:37:35 +0200 Subject: [PATCH] [orx-fcurve] Fix bug in Segment2D.tForX --- orx-fcurve/src/commonMain/kotlin/FCurve.kt | 5 ++++- orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/orx-fcurve/src/commonMain/kotlin/FCurve.kt b/orx-fcurve/src/commonMain/kotlin/FCurve.kt index 304282b1..58cffaa6 100644 --- a/orx-fcurve/src/commonMain/kotlin/FCurve.kt +++ b/orx-fcurve/src/commonMain/kotlin/FCurve.kt @@ -11,6 +11,9 @@ import kotlin.math.abs * Find the (first) t value for a given [x] value */ private fun Segment2D.tForX(x: Double): Double { + if (x == start.x) return 0.0 + if (x == end.x) return 1.0 + if (linear) { return (x - start.x) / (end.x - start.x) } else { @@ -155,7 +158,7 @@ data class FCurve(val segments: List) { if (segments.isEmpty()) { return Pair(0.0, null) } - if (t < segments.first().start.x) { + if (t <= segments.first().start.x) { val segment = segments.first() return Pair(segment.start.y, segment) } else if (t > segments.last().end.x) { diff --git a/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt b/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt index ce46b794..d318bee6 100644 --- a/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt +++ b/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt @@ -1,4 +1,5 @@ import org.openrndr.extra.fcurve.efcurve +import org.openrndr.extra.fcurve.fcurve import kotlin.test.Test import kotlin.test.assertEquals @@ -30,4 +31,11 @@ class TestEFCurve { assertEquals("M0 L1.0, ${0.0} L1.0, ${1.0}", efcurve("M0 (L1.0, {it})[2]")) assertEquals("M0 L1.0, ${0.0} L1.0, ${1.0} L1.0, ${0.0} L1.0, ${1.0} L1.0, ${0.0} L1.0, ${1.0}", efcurve("M0 ((L1.0, {it})[2])[3]")) } + + @Test + fun testContinuity() { + val fc = fcurve("Q1 25% 3 100 Q1 25% 3 0 Q1 25% 3 100 Q1 25% 3 0") + val s = fc.sampler() + assertEquals(0.0, s(6.0)) + } } \ No newline at end of file