[orx-shapes] Add splitIn

This commit is contained in:
Edwin Jakobs
2023-10-28 14:01:39 +02:00
parent cce913d269
commit 2293d5d74e
2 changed files with 36 additions and 13 deletions

View File

@@ -20,8 +20,6 @@ data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segment
}
/**
* A [ContourAdjusterVertex] interface for the start-vertex of the edge
*/
@@ -32,7 +30,9 @@ data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segment
* A [ContourAdjusterVertex] interface for the end-vertex of the edge
*/
val end
get() = ContourAdjusterVertex(contourAdjuster, { (segmentIndex() + 1).mod(contourAdjuster.contour.segments.size) } )
get() = ContourAdjusterVertex(
contourAdjuster,
{ (segmentIndex() + 1).mod(contourAdjuster.contour.segments.size) })
/**
* A link to the edge before this edge
@@ -65,18 +65,27 @@ data class ContourAdjusterEdge(val contourAdjuster: ContourAdjuster, val segment
fun select() {
contourAdjuster.selectEdge(segmentIndex())
}
private fun wrap(block: ContourEdge.() -> ContourEdge) {
val newEdge = ContourEdge(contourAdjuster.contour, segmentIndex()).block()
contourAdjuster.contour = newEdge.contour
contourAdjuster.updateSelection(newEdge.adjustments)
}
fun toLinear() = wrap { toLinear() }
fun toCubic() = wrap { toCubic() }
fun splitAt(t: Double) = wrap { splitAt(t) }
/**
* split edge in [numberOfParts] parts of equal length
*/
fun splitIn(numberOfParts: Int) = wrap { splitIn(numberOfParts) }
fun moveBy(translation: Vector2, updateTangents: Boolean = true) = wrap { movedBy(translation, updateTangents) }
fun rotate(rotationInDegrees: Double, anchorT: Double = 0.5, updateTangents: Boolean = true) =
wrap { rotatedBy(rotationInDegrees, anchorT, updateTangents) }
fun scale(scaleFactor: Double, anchorT: Double = 0.5, updateTangents: Boolean = true) =
wrap { scaledBy(scaleFactor, anchorT, updateTangents = true) }

View File

@@ -1,5 +1,7 @@
package org.openrndr.extra.shapes.adjust
import org.openrndr.extra.shapes.rectify.rectified
import org.openrndr.extra.shapes.utilities.fromContours
import org.openrndr.extra.shapes.utilities.insertPointAt
import org.openrndr.math.Matrix44
import org.openrndr.math.Vector2
@@ -111,6 +113,20 @@ data class ContourEdge(
return ContourEdge(ShapeContour.fromSegments(newSegments, contour.closed), segmentIndex, adjustments)
}
fun splitIn(parts: Int): ContourEdge {
if (contour.empty || parts < 2) {
return withoutAdjustments()
}
val segment = contour.segments[segmentIndex]
val r = segment.contour.rectified()
val newSegments = (0..parts).map {
it.toDouble() / parts
}.windowed(2, 1).map {
r.sub(it[0], it[1])
}
return replacedWith(ShapeContour.fromContours(newSegments, false))
}
fun replacedWith(openContour: ShapeContour): ContourEdge {
if (contour.empty) {
return withoutAdjustments()
@@ -255,7 +271,5 @@ data class ContourEdge(
translate(-anchor)
}, updateTangents)
}
}