From 8a18546ef10edcfd6aa08345fbbc8c261521c139 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sat, 19 Oct 2024 16:54:49 +0200 Subject: [PATCH] [orx-noise] Fix bug in List.uniform --- .../src/commonMain/kotlin/TriangleNoise.kt | 2 +- .../src/jvmDemo/kotlin/DemoTriangleNoise01.kt | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 orx-noise/src/jvmDemo/kotlin/DemoTriangleNoise01.kt diff --git a/orx-noise/src/commonMain/kotlin/TriangleNoise.kt b/orx-noise/src/commonMain/kotlin/TriangleNoise.kt index fcc87ab68..a35945a12 100644 --- a/orx-noise/src/commonMain/kotlin/TriangleNoise.kt +++ b/orx-noise/src/commonMain/kotlin/TriangleNoise.kt @@ -17,7 +17,7 @@ fun List.uniform(count: Int, random: Random = Random.Default): List randoms[idx]) { + while (idx <= randoms.lastIndex && sum > randoms[idx]) { result.add(t.randomPoint(random)) idx++ } diff --git a/orx-noise/src/jvmDemo/kotlin/DemoTriangleNoise01.kt b/orx-noise/src/jvmDemo/kotlin/DemoTriangleNoise01.kt new file mode 100644 index 000000000..b0f5c92ee --- /dev/null +++ b/orx-noise/src/jvmDemo/kotlin/DemoTriangleNoise01.kt @@ -0,0 +1,31 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.extra.noise.uniform +import org.openrndr.shape.Triangle +import kotlin.random.Random + +/** + * Demonstrate the generation of uniformly distributed points inside a list of triangles + * @see + */ +fun main() { + application { + configure { + width = 720 + height = 720 + } + program { + val r = drawer.bounds.offsetEdges(-100.0) + val triangle = Triangle(r.position(0.5, 0.0), r.position(0.0, 1.0), r.position(1.0, 1.0)) + val pts = listOf(triangle).uniform(1000, Random(0)) + extend { + drawer.clear(ColorRGBa.PINK) + drawer.stroke = null + drawer.contour(triangle.contour) + drawer.fill = ColorRGBa.BLACK + + drawer.circles(pts, 5.0) + } + } + } +} \ No newline at end of file