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