[orx-noise] Add ShapeProvider.poissonDiskSampling

This commit is contained in:
Edwin Jakobs
2021-10-17 17:26:39 +02:00
parent 8808e62431
commit 737f1bcf85
4 changed files with 80 additions and 7 deletions

View File

@@ -0,0 +1,31 @@
package org.openrndr.extra.noise
import org.openrndr.math.Vector2
import org.openrndr.shape.*
import kotlin.random.Random
fun ShapeProvider.uniform(random: Random = Random.Default): Vector2 {
val shape = shape
return Vector2.uniformSequence(shape.bounds, random).first {
shape.contains(it)
}
}
fun ShapeProvider.poissonDiskSampling(
r: Double,
tries: Int = 30,
random: Random = Random.Default
): List<Vector2> {
val shape = shape
val bounds = shape.bounds
val poissonBounds = Rectangle(0.0, 0.0, bounds.width, bounds.height)
val initialPoint = this.uniform(random).map(bounds, poissonBounds)
return poissonDiskSampling(bounds.width, bounds.height, r, tries, false, random, initialPoint) { _, _, point ->
val contourPoint = point.map(poissonBounds, bounds)
shape.contains(contourPoint)
}.map {
it.map(poissonBounds, bounds)
}
}