From 37ca4f3a5d04a21a2e08fd1d7f7e3e4d98c0475c Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Wed, 22 May 2024 13:04:06 +0200 Subject: [PATCH] [orx-noise] Add ShapeProvider.uniform() and List.uniform() --- orx-noise/src/commonMain/kotlin/ShapeNoise.kt | 6 +++++ .../src/commonMain/kotlin/TriangleNoise.kt | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 orx-noise/src/commonMain/kotlin/TriangleNoise.kt diff --git a/orx-noise/src/commonMain/kotlin/ShapeNoise.kt b/orx-noise/src/commonMain/kotlin/ShapeNoise.kt index 2240d489..e6fe7f22 100644 --- a/orx-noise/src/commonMain/kotlin/ShapeNoise.kt +++ b/orx-noise/src/commonMain/kotlin/ShapeNoise.kt @@ -25,6 +25,12 @@ fun ShapeProvider.uniform(distanceToEdge: Double = 0.0, random: Random = Random. } } +/** + * Generate [sampleCount] uniformly distributed points inside the area of [ShapeProvider] + */ +fun ShapeProvider.uniform(sampleCount: Int) : List = shape.triangulation.uniform(sampleCount) + + /** * Returns a list of pairs in which the first component is a radius and the * second component a list of [Vector2] positions of items with that radius. diff --git a/orx-noise/src/commonMain/kotlin/TriangleNoise.kt b/orx-noise/src/commonMain/kotlin/TriangleNoise.kt new file mode 100644 index 00000000..11f1e04d --- /dev/null +++ b/orx-noise/src/commonMain/kotlin/TriangleNoise.kt @@ -0,0 +1,25 @@ +package org.openrndr.extra.noise + +import org.openrndr.math.Vector2 +import org.openrndr.shape.Triangle + +/** + * Generate [count] uniform samples from a list of [Triangle]s + */ +fun List.uniform(count: Int): List { + val totalArea = this.sumOf { it.area } + val randoms = (0 until count).map { + Double.uniform(0.0, totalArea) + }.sorted() + val result = mutableListOf() + var idx = 0 + var sum = 0.0 + for (t in this) { + sum += t.area + while (idx < randoms.lastIndex && sum > randoms[idx]) { + result.add(t.randomPoint()) + idx++ + } + } + return result +} \ No newline at end of file