Skip to content

Commit 382d2cf

Browse files
committed
Add Shift.kt with utilities for contour shifting
1 parent 00e2fa9 commit 382d2cf

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • orx-shapes/src/commonMain/kotlin/utilities
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.openrndr.extra.shapes.utilities
2+
3+
import org.openrndr.extra.shapes.rectify.RectifiedContour
4+
import org.openrndr.shape.ShapeContour
5+
6+
/**
7+
* Shifts the starting point of the current `ShapeContour` by a specified normalized distance `dt`.
8+
*
9+
* @param dt the normalized distance by which to shift the starting point. The value is interpreted
10+
* as a fraction of the total contour length (from 0.0 to 1.0). Positive values shift forward along
11+
* the contour, while negative values shift backward.
12+
* @return a new `ShapeContour` with the starting point shifted by the specified distance.
13+
* If the contour is not closed, the original contour is returned unaltered.
14+
*/
15+
fun ShapeContour.shift(dt: Double): ShapeContour {
16+
if (!closed) return this
17+
18+
val t = dt.mod(1.0)
19+
val parts = splitAt(listOf(t))
20+
return ShapeContour.fromContours(parts.reversed(), closed)
21+
}
22+
23+
/**
24+
* Shifts the contour by a specified amount along its parameterized path.
25+
* If the contour is not closed, it returns the original contour unaltered.
26+
*
27+
* @param dt the shift amount, where 1.0 represents the entire length of the contour
28+
* and positive values move forward while negative values move backward.
29+
* @return a new shifted `ShapeContour` if the contour is closed, or the original contour otherwise.
30+
*/
31+
fun RectifiedContour.shift(dt: Double): ShapeContour {
32+
if (!contour.closed) return contour
33+
34+
val t = dt.mod(1.0)
35+
val parts = splitAt(listOf(t))
36+
return ShapeContour.fromContours(parts.reversed(), contour.closed)
37+
}

0 commit comments

Comments
 (0)