From eed873d404ce684951d37f606afc1f0f7d41a82b Mon Sep 17 00:00:00 2001 From: Abe Pazos Date: Sat, 30 Aug 2025 14:01:02 +0200 Subject: [PATCH] [orx-shapes] Add demos, tweak comments --- .../commonMain/kotlin/adjust/ContourEdge.kt | 6 +-- .../kotlin/adjust/DemoAdjustContour09.kt | 1 - ...{DemoAlphaShape.kt => DemoAlphaShape01.kt} | 10 +++- .../kotlin/alphashape/DemoAlphaShape02.kt | 47 +++++++++++++++++++ .../kotlin/operators/DemoRoundCorners02.kt | 28 +++++++++++ 5 files changed, 86 insertions(+), 6 deletions(-) rename orx-shapes/src/jvmDemo/kotlin/alphashape/{DemoAlphaShape.kt => DemoAlphaShape01.kt} (59%) create mode 100644 orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape02.kt create mode 100644 orx-shapes/src/jvmDemo/kotlin/operators/DemoRoundCorners02.kt diff --git a/orx-shapes/src/commonMain/kotlin/adjust/ContourEdge.kt b/orx-shapes/src/commonMain/kotlin/adjust/ContourEdge.kt index 3eeac964..d39a002c 100644 --- a/orx-shapes/src/commonMain/kotlin/adjust/ContourEdge.kt +++ b/orx-shapes/src/commonMain/kotlin/adjust/ContourEdge.kt @@ -31,10 +31,10 @@ fun List.update(vararg updates: Pair): List { /** * Helper for querying and adjusting [ShapeContour]. - * * An edge embodies exactly the same thing as a [Segment][org.openrndr.shape.Segment] + * * An edge embodies exactly the same thing as a [Segment2D][org.openrndr.shape.Segment2D] * * All edge operations are immutable and will create a new [ContourEdge] pointing to a copied and updated [ShapeContour] * @param contour the contour to be adjusted - * @param segmentIndex the index of the segment of the contour to be adjusted + * @param segmentIndex the index the contour's segment to be adjusted * @param adjustments a list of [SegmentOperation] that have been applied to reach to [contour], this is used to inform [ShapeContour] * of changes in the contour topology. * @since 0.4.4 @@ -126,7 +126,7 @@ data class ContourEdge( } /** - * Split the edge in [numberOfParts] parts of equal length + * Split the edge in [parts] parts of equal length */ fun splitIn(parts: Int): ContourEdge { if (contour.empty || parts < 2) { diff --git a/orx-shapes/src/jvmDemo/kotlin/adjust/DemoAdjustContour09.kt b/orx-shapes/src/jvmDemo/kotlin/adjust/DemoAdjustContour09.kt index 04f9dd75..7fcf61d7 100644 --- a/orx-shapes/src/jvmDemo/kotlin/adjust/DemoAdjustContour09.kt +++ b/orx-shapes/src/jvmDemo/kotlin/adjust/DemoAdjustContour09.kt @@ -3,7 +3,6 @@ package adjust import org.openrndr.application import org.openrndr.color.ColorRGBa import org.openrndr.draw.loadFont -import org.openrndr.extra.color.presets.DARK_CYAN import org.openrndr.extra.shapes.adjust.adjustContour import org.openrndr.extra.shapes.adjust.extensions.averageTangents import org.openrndr.extra.shapes.adjust.extensions.switchTangents diff --git a/orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape.kt b/orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape01.kt similarity index 59% rename from orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape.kt rename to orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape01.kt index 8cdd122a..029ab1bb 100644 --- a/orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape.kt +++ b/orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape01.kt @@ -6,12 +6,18 @@ import org.openrndr.extra.shapes.alphashape.AlphaShape import org.openrndr.math.Vector2 import kotlin.random.Random +/** + * Demonstrates the use of [AlphaShape] to create a [org.openrndr.shape.ShapeContour] out + * of a collection of random [Vector2] points. Unlike the convex hull, an Alpha shape can be concave. + * + * More details in [WikiPedia](https://en.wikipedia.org/wiki/Alpha_shape) + */ fun main() = application { program { val points = List(40) { Vector2( - Random.nextDouble(width*0.25, width*0.75), - Random.nextDouble(height*0.25, height*0.75) + Random.nextDouble(width * 0.25, width * 0.75), + Random.nextDouble(height * 0.25, height * 0.75) ) } val alphaShape = AlphaShape(points) diff --git a/orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape02.kt b/orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape02.kt new file mode 100644 index 00000000..d82f6253 --- /dev/null +++ b/orx-shapes/src/jvmDemo/kotlin/alphashape/DemoAlphaShape02.kt @@ -0,0 +1,47 @@ +package alphashape + +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.extra.noise.shapes.uniform +import org.openrndr.extra.shapes.alphashape.AlphaShape +import org.openrndr.math.Vector2 +import kotlin.random.Random + +/** + * Demonstrates the use of [AlphaShape] to create ten + * [org.openrndr.shape.ShapeContour] instances out of a collection of random [Vector2] points. + * + * The same points are used for each contour, but an increased alpha parameter + * is passed to the AlphaShape algorithm. Higher values return more convex shapes + * = shapes with a larger surface. + * + * The list of shapes is reversed to draw the smaller contours on top, otherwise only + * the last one would be visible. + * + * An instance of [Random] with a fixed seed is used to ensure the resulting + * random shape is always the same. + */ +fun main() = application { + program { + val rand = Random(242) + val points = List(40) { + drawer.bounds.uniform(rand) + } + val alphaShape = AlphaShape(points) + val minAlpha = alphaShape.determineContourAlpha() + + val contours = List(10) { + alphaShape.createContour(minAlpha + it * it * it) + }.reversed() + extend { + drawer.stroke = null + contours.forEachIndexed { index, contour -> + drawer.fill = ColorRGBa.PINK.shade(0.5 + index * 0.07) + drawer.contour(contour) + } + + drawer.fill = ColorRGBa.WHITE + drawer.circles(points, 4.0) + } + } +} \ No newline at end of file diff --git a/orx-shapes/src/jvmDemo/kotlin/operators/DemoRoundCorners02.kt b/orx-shapes/src/jvmDemo/kotlin/operators/DemoRoundCorners02.kt new file mode 100644 index 00000000..99b4ed49 --- /dev/null +++ b/orx-shapes/src/jvmDemo/kotlin/operators/DemoRoundCorners02.kt @@ -0,0 +1,28 @@ +package operators + +import org.openrndr.application +import org.openrndr.extra.shapes.adjust.adjustContour +import org.openrndr.extra.shapes.operators.roundCorners +import org.openrndr.shape.Rectangle + +fun main() = application { + program { + extend { + val contours = listOf( + Rectangle(100.0, 100.0, 100.0, 100.0).contour, + adjustContour(Rectangle(400.0, 100.0, 100.0, 100.0).contour) { + selectVertex(0) + vertices.forEach { it.rotate(30.0) } + } + ) + + val contoursRounded = contours.map { + it.roundCorners(10.0) + } + + drawer.contours(contours) + drawer.translate(0.0, 150.0) + drawer.contours(contoursRounded) + } + } +} \ No newline at end of file