[orx-shapes] Add demos, tweak comments

This commit is contained in:
Abe Pazos
2025-08-30 14:01:02 +02:00
parent 878f2b040d
commit eed873d404
5 changed files with 86 additions and 6 deletions

View File

@@ -31,10 +31,10 @@ fun <E> List<E>.update(vararg updates: Pair<Int, E>): List<E> {
/**
* 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) {

View File

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

View File

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

View File

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

View File

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