[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.
This commit is contained in:
Edwin Jakobs
2025-02-26 21:21:27 +01:00
parent 72af832655
commit 660949271d
2 changed files with 62 additions and 1 deletions

View File

@@ -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)
}

View File

@@ -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)
}