From 660949271d906f2d9ffa8bf1dd2f721da6e98da6 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Wed, 26 Feb 2025 21:21:27 +0100 Subject: [PATCH] [orx-noise] Add uniformSub functions for Box and Rectangle Introduce `uniformSub` functions to generate sub-boxes and sub-rectangles with random dimensions within specified ranges. These additions enhance functionality for creating randomized geometric shapes. --- orx-noise/src/commonMain/kotlin/shapes/Box.kt | 36 ++++++++++++++++++- .../src/commonMain/kotlin/shapes/Rectangle.kt | 27 ++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/orx-noise/src/commonMain/kotlin/shapes/Box.kt b/orx-noise/src/commonMain/kotlin/shapes/Box.kt index d386bb70..0449bd4e 100644 --- a/orx-noise/src/commonMain/kotlin/shapes/Box.kt +++ b/orx-noise/src/commonMain/kotlin/shapes/Box.kt @@ -15,7 +15,7 @@ fun Box.uniform(random: Random = Random.Default): Vector3 { val x = random.nextDouble() * width + corner.x val y = random.nextDouble() * height + corner.y val z = random.nextDouble() * depth + corner.z - return Vector3(x, y ,z) + return Vector3(x, y, z) } /** @@ -38,4 +38,38 @@ fun Box.hash(seed: Int, x: Int): Vector3 { val y = fy * height + corner.y val z = fz * depth + corner.z return Vector3(x, y, z) +} + + +/** + * Generates a uniformly distributed sub-box based on random parameters within specified ranges. + * + * @param minWidth The minimum width of the sub-box. Defaults to 0.0. + * @param maxWidth The maximum width of the sub-box. Defaults to 1.0. + * @param minHeight The minimum height of the sub-box. Defaults to 0.0. + * @param maxHeight The maximum height of the sub-box. Defaults to 1.0. + * @param minDepth The minimum depth of the sub-box. Defaults to 0.0. + * @param maxDepth The maximum depth of the sub-box. Defaults to 1.0. + * @param random The `Random` instance used for generating random values. Defaults to `Random.Default`. + * @return A new `Box` that represents the sub-box. + */ +fun Box.uniformSub( + minWidth: Double = 0.0, + maxWidth: Double = 1.0, + minHeight: Double = 0.0, + maxHeight: Double = 1.0, + minDepth: Double = 0.0, + maxDepth: Double = 1.0, + random: Random = Random.Default +): Box { + val width = random.nextDouble(minWidth, maxWidth) + val height = random.nextDouble(minHeight, maxHeight) + val depth = random.nextDouble(minDepth, maxDepth) + val u0 = random.nextDouble(1.0 - width) + val v0 = random.nextDouble(1.0 - height) + val w0 = random.nextDouble(1.0 - depth) + val u1 = u0 + width + val v1 = v0 + height + val w1 = w0 + depth + return sub(u0..u1, v0..v1, w0..w1) } \ No newline at end of file diff --git a/orx-noise/src/commonMain/kotlin/shapes/Rectangle.kt b/orx-noise/src/commonMain/kotlin/shapes/Rectangle.kt index 4e613812..55a3a0fa 100644 --- a/orx-noise/src/commonMain/kotlin/shapes/Rectangle.kt +++ b/orx-noise/src/commonMain/kotlin/shapes/Rectangle.kt @@ -34,4 +34,31 @@ fun Rectangle.hash(seed: Int, x: Int): Vector2 { val x = fx * width + corner.x val y = fy * height + corner.y return Vector2(x, y) +} + + +/** + * Generates a uniformly distributed sub-rectangle based on random parameters within specified ranges. + * + * @param minWidth The minimum width of the sub-rectangle. Defaults to 0.0. + * @param maxWidth The maximum width of the sub-rectangle. Defaults to 1.0. + * @param minHeight The minimum height of the sub-rectangle. Defaults to 0.0. + * @param maxHeight The maximum height of the sub-rectangle. Defaults to 1.0. + * @param random The `Random` instance used for generating random values. + * @return A new `Rectangle` that represents the sub-rectangle. + */ +fun Rectangle.uniformSub( + minWidth: Double = 0.0, + maxWidth: Double = 1.0, + minHeight: Double = 0.0, + maxHeight: Double = 1.0, + random: Random = Random.Default +): Rectangle { + val width = random.nextDouble(minWidth, maxWidth) + val height = random.nextDouble(minHeight, maxHeight) + val u0 = random.nextDouble(1.0 - width) + val v0 = random.nextDouble(1.0 - height) + val u1 = u0 + width + val v1 = v0 + height + return sub(u0..u1, v0..v1) } \ No newline at end of file