From 2208de2fb39e53c5266500bc1a1ddddde1dad762 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sat, 1 Feb 2025 14:20:26 +0100 Subject: [PATCH] [orx-noise] Add LinearRange extension functions for uniform and hashed values --- .../linearrange/LinearRangeExtensions.kt | 89 +++++++++++++++++++ .../kotlin/linearrange/DemoLinearRange01.kt | 26 ++++++ 2 files changed, 115 insertions(+) create mode 100644 orx-noise/src/commonMain/kotlin/linearrange/LinearRangeExtensions.kt create mode 100644 orx-noise/src/jvmDemo/kotlin/linearrange/DemoLinearRange01.kt diff --git a/orx-noise/src/commonMain/kotlin/linearrange/LinearRangeExtensions.kt b/orx-noise/src/commonMain/kotlin/linearrange/LinearRangeExtensions.kt new file mode 100644 index 00000000..31ed49aa --- /dev/null +++ b/orx-noise/src/commonMain/kotlin/linearrange/LinearRangeExtensions.kt @@ -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 > LinearRange1D.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 > LinearRange2D.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 > LinearRange3D.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 > LinearRange4D.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 > LinearRange1D.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 > LinearRange2D.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 > LinearRange3D.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 > LinearRange4D.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)) +} \ No newline at end of file diff --git a/orx-noise/src/jvmDemo/kotlin/linearrange/DemoLinearRange01.kt b/orx-noise/src/jvmDemo/kotlin/linearrange/DemoLinearRange01.kt new file mode 100644 index 00000000..4e810fc3 --- /dev/null +++ b/orx-noise/src/jvmDemo/kotlin/linearrange/DemoLinearRange01.kt @@ -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)) + } + } + } + } +} \ No newline at end of file