[orx-noise] Add ShapeProvider.uniform() and List<Triangle>.uniform()

This commit is contained in:
Edwin Jakobs
2024-05-22 13:04:06 +02:00
parent 86ae5f70de
commit 37ca4f3a5d
2 changed files with 31 additions and 0 deletions

View File

@@ -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<Triangle>.uniform(count: Int): List<Vector2> {
val totalArea = this.sumOf { it.area }
val randoms = (0 until count).map {
Double.uniform(0.0, totalArea)
}.sorted()
val result = mutableListOf<Vector2>()
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
}