diff --git a/orx-shapes/src/commonMain/kotlin/primitives/RectanglePlace.kt b/orx-shapes/src/commonMain/kotlin/primitives/RectanglePlace.kt new file mode 100644 index 00000000..88ab7691 --- /dev/null +++ b/orx-shapes/src/commonMain/kotlin/primitives/RectanglePlace.kt @@ -0,0 +1,22 @@ +package org.openrndr.extra.shapes.primitives + +import org.openrndr.math.Vector2 +import org.openrndr.shape.Rectangle + +/** + * Places a given rectangle (`item`) within the bounds of the current rectangle (`this`), + * positioning it based on the specified anchor point. + * + * @param item The rectangle to be placed within the current rectangle. + * @param anchor The relative position of the anchor point within the bounds of the current rectangle. + * Defaults to `(0.5, 0.5)` which centers the item within the current rectangle. + * @return A new rectangle representing the positioned `item` within the current rectangle. + */ +fun Rectangle.place(item: Rectangle, anchor: Vector2 = Vector2(0.5, 0.5), itemAnchor: Vector2 = anchor): Rectangle { + return Rectangle( + x = x + (width * anchor.x - item.width * itemAnchor.x), + y = y + (height * anchor.y - item.height * itemAnchor.y), + width = item.width, + height = item.height + ) +} \ No newline at end of file diff --git a/orx-shapes/src/jvmDemo/kotlin/primitives/DemoRectanglePlace01.kt b/orx-shapes/src/jvmDemo/kotlin/primitives/DemoRectanglePlace01.kt new file mode 100644 index 00000000..ab59fc5c --- /dev/null +++ b/orx-shapes/src/jvmDemo/kotlin/primitives/DemoRectanglePlace01.kt @@ -0,0 +1,38 @@ +package primitives + +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.extra.shapes.primitives.place +import org.openrndr.math.Vector2 +import org.openrndr.shape.Rectangle + +/** + * Demo for rendering a 10x10 grid of rectangles within the bounds + * of the canvas. Each rectangle's position is calculated relative to its anchors, filling the entire + * canvas with evenly placed items. + * + * The rectangles are drawn using the default white color. The `place` function is applied to each + * rectangle to position them dynamically based on their relative anchor points within the bounding area. + * + * This serves as a demonstration of positioning and rendering shapes in a structured grid layout. + */ +fun main() { + application { + configure { + width = 720 + height = 720 + } + program { + extend { + val item = Rectangle(200.0, 200.0, 40.0, 40.0) + + drawer.fill = ColorRGBa.WHITE + for (j in 0 until 10) { + for (i in 0 until 10) { + drawer.rectangle(drawer.bounds.place(item, Vector2(i / 9.0, j / 9.0))) + } + } + } + } + } +} \ No newline at end of file