[orx-noise] Add ShapeProvider.uniform() and List<Triangle>.uniform()
This commit is contained in:
@@ -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<Vector2> = 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.
|
||||
|
||||
25
orx-noise/src/commonMain/kotlin/TriangleNoise.kt
Normal file
25
orx-noise/src/commonMain/kotlin/TriangleNoise.kt
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user