diff --git a/orx-shapes/src/main/kotlin/RoundedRectangle.kt b/orx-shapes/src/main/kotlin/RoundedRectangle.kt new file mode 100644 index 00000000..29d4a196 --- /dev/null +++ b/orx-shapes/src/main/kotlin/RoundedRectangle.kt @@ -0,0 +1,36 @@ +import org.openrndr.math.Vector2 +import org.openrndr.shape.contour +import kotlin.math.min + +class RoundedRectangle(val corner: Vector2, val width: Double, val height: Double, val radius: Double) { + constructor(x: Double, y: Double, width: Double, height: Double, radius: Double) : this(Vector2(x, y), width, height, radius) + + /** the center of the rounded rectangle */ + val center: Vector2 + get() = corner + Vector2(width / 2, height / 2) + + val x: Double get() = corner.x + val y: Double get() = corner.y + + /** [ShapeContour] representation of the rounded rectangle */ + val contour + get() = contour { + // A higher radius than half the width/height makes it go weird + val r = min(min(radius, width / 2), height / 2) + + moveTo(x + r, y) + lineTo(x + width - r, y) + + curveTo(Vector2(x + width, y), Vector2(x + width, y + r)) + lineTo(x + width, y + height - r) + + curveTo(Vector2(x + width, y + height), Vector2(x + width - r, y + height)) + lineTo(x + r, y + height) + + curveTo(Vector2(x, y + height), Vector2(x, y + height - r)) + lineTo(x, y + r) + + curveTo(Vector2(x, y), Vector2(x + r, y)) + close() + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index bee51272..005d1930 100644 --- a/settings.gradle +++ b/settings.gradle @@ -27,6 +27,7 @@ include 'orx-camera', 'orx-runway', 'orx-shader-phrases', 'orx-shade-styles', + 'orx-shapes', 'orx-syphon', 'orx-temporal-blur', 'orx-kinect-common',