[orx-noise] Add LinearRange extension functions for uniform and hashed values

This commit is contained in:
Edwin Jakobs
2025-02-01 14:20:26 +01:00
parent 426e35ebd0
commit 2208de2fb3
2 changed files with 115 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
package org.openrndr.extra.noise.linearrange
import org.openrndr.extra.noise.fhash1D
import org.openrndr.math.*
import kotlin.random.Random
/**
* Generates a uniformly distributed random value within the range.
*
* @param random The random number generator to use for generating the value. Defaults to [Random.Default].
* @return A value of type [T] sampled uniformly within the range.
*/
fun <T : LinearType<T>> LinearRange1D<T>.uniform(random: Random = Random.Default): T = value(random.nextDouble())
/**
* Generates a random value within the 2D linear range based on a uniform distribution.
*
* @param random The random number generator to use for producing random values, defaults to Random.Default.
* @return A randomly generated value of type T within the linear range.
*/
fun <T : LinearType<T>> LinearRange2D<T>.uniform(random: Random = Random.Default): T =
value(random.nextDouble(), random.nextDouble())
/**
* Generates a uniform random value within the 3D linear range, based on the given random number generator.
*
* @param random The random number generator to use for generating random values. Defaults to `Random.Default`.
* @return A randomly generated value of type `T` within the 3D linear range.
*/
fun <T : LinearType<T>> LinearRange3D<T>.uniform(random: Random = Random.Default): T =
value(random.nextDouble(), random.nextDouble(), random.nextDouble())
/**
* Generates a value of type `T` uniformly distributed within the 4D linear range.
*
* @param random The random number generator to use. Defaults to `Random.Default`.
* @return A uniformly distributed value of type `T` within the 4D range.
*/
fun <T : LinearType<T>> LinearRange4D<T>.uniform(random: Random = Random.Default): T =
value(random.nextDouble(), random.nextDouble(), random.nextDouble(), random.nextDouble())
/**
* Computes a hashed value based on the provided seed and input, and generates
* a `LinearType` instance using the hash results.
*
* @param seed The seed value used for hash computation.
* @param x The integer input value used for hash computation.
* @return a `LinearType` instance computed from the hash values
*
*/
fun <T : LinearType<T>> LinearRange1D<T>.hash(seed: Int, x: Int) : T = value(fhash1D(seed, x))
/**
* Computes a hashed value based on the provided seed and input, and generates
* a `LinearType` instance using the hash results.
*
* @param seed an integer seed value used to initialize the hash computation
* @param x an integer input used in the hash computation
* @return a `LinearType` instance computed from the hash values
*/
fun <T : LinearType<T>> LinearRange2D<T>.hash(seed: Int, x: Int) : T =
value(fhash1D(seed, x), fhash1D(seed xor 0x7f7f_7f7f, x))
/**
* Computes a hashed value based on the provided seed and input, and generates
* a `LinearType` instance using the hash results.
*
* @param seed an integer seed value used to initialize the hash computation
* @param x an integer input used in the hash computation
* @return a `LinearType` instance computed from the hash values
*/
fun <T : LinearType<T>> LinearRange3D<T>.hash(seed: Int, x: Int) :T {
val x4 = x * 3
return value(fhash1D(seed, x4), fhash1D(seed, x4 + 1), fhash1D(seed, x4 + 2))
}
/**
* Computes a hashed value based on the provided seed and input, and generates
* a `LinearType` instance using the hash results.
*
* @param seed an integer seed value used to initialize the hash computation
* @param x an integer input used in the hash computation
* @return a `LinearType` instance computed from the hash values
*/
fun <T : LinearType<T>> LinearRange4D<T>.hash(seed: Int, x: Int) {
val x4 = x * 4
value(fhash1D(seed, x4), fhash1D(seed, x4 + 1), fhash1D(seed, x4 + 2), fhash1D(seed, x4 + 3))
}

View File

@@ -0,0 +1,26 @@
package linearrange
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.noise.linearrange.uniform
import org.openrndr.math.rangeTo
import kotlin.random.Random
fun main() {
application {
configure {
width = 720
height = 720
}
program {
val range = drawer.bounds.offsetEdges(-300.0, -50.0) .. drawer.bounds.offsetEdges(-50.0, -300.0)
extend {
drawer.fill = ColorRGBa.WHITE.opacify(0.9)
val r = Random(seconds.toInt())
for (i in 0 until 100) {
drawer.rectangle(range.uniform(r))
}
}
}
}
}