From 98b378d5c672a00be0bbdb9829b4906c36e43a98 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Tue, 8 Aug 2023 16:38:18 +0200 Subject: [PATCH] [orx-shapes] Implement RoundedRectangle using circular rounding --- .../src/commonMain/kotlin/RoundedRectangle.kt | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/orx-shapes/src/commonMain/kotlin/RoundedRectangle.kt b/orx-shapes/src/commonMain/kotlin/RoundedRectangle.kt index 4540ff2c..df4986de 100644 --- a/orx-shapes/src/commonMain/kotlin/RoundedRectangle.kt +++ b/orx-shapes/src/commonMain/kotlin/RoundedRectangle.kt @@ -8,8 +8,19 @@ import org.openrndr.shape.ShapeContour 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) - constructor(rectangle: Rectangle, radius: Double) : this(rectangle.corner, rectangle.width, rectangle.height, radius) + constructor(x: Double, y: Double, width: Double, height: Double, radius: Double) : this( + Vector2(x, y), + width, + height, + radius + ) + + constructor(rectangle: Rectangle, radius: Double) : this( + rectangle.corner, + rectangle.width, + rectangle.height, + radius + ) /** the center of the rounded rectangle */ val center: Vector2 @@ -27,16 +38,16 @@ class RoundedRectangle(val corner: Vector2, val width: Double, val height: Doubl moveTo(x + r, y) lineTo(x + width - r, y) - curveTo(Vector2(x + width, y), Vector2(x + width, y + r)) + arcTo(r, r, 90.0, false, true, Vector2(x + width, y + r)) lineTo(x + width, y + height - r) - curveTo(Vector2(x + width, y + height), Vector2(x + width - r, y + height)) + arcTo(r, r, 90.0, false, true, Vector2(x + width - r, y + height)) lineTo(x + r, y + height) - curveTo(Vector2(x, y + height), Vector2(x, y + height - r)) + arcTo(r, r, 90.0, false, true, Vector2(x, y + height - r)) lineTo(x, y + r) - curveTo(Vector2(x, y), Vector2(x + r, y)) + arcTo(r, r, 90.0, false, true, Vector2(x + r, y)) close() } @@ -45,12 +56,12 @@ class RoundedRectangle(val corner: Vector2, val width: Double, val height: Doubl } fun Drawer.roundedRectangle(x: Double, y: Double, width: Double, height: Double, radius: Double) = - contour(RoundedRectangle(x, y, width, height, radius).contour) + contour(RoundedRectangle(x, y, width, height, radius).contour) fun Drawer.roundedRectangle(position: Vector2, width: Double, height: Double, radius: Double) = - contour(RoundedRectangle(position, width, height, radius).contour) + contour(RoundedRectangle(position, width, height, radius).contour) fun Drawer.roundedRectangle(roundedRectangle: RoundedRectangle) = - contour(roundedRectangle.contour) + contour(roundedRectangle.contour) fun Rectangle.toRounded(radius: Double) = RoundedRectangle(this, radius) \ No newline at end of file