[orx-shapes] Implement RoundedRectangle using circular rounding

This commit is contained in:
Edwin Jakobs
2023-08-08 16:38:18 +02:00
parent 650397a698
commit 98b378d5c6

View File

@@ -8,8 +8,19 @@ import org.openrndr.shape.ShapeContour
import kotlin.math.min import kotlin.math.min
class RoundedRectangle(val corner: Vector2, val width: Double, val height: Double, val radius: Double) { 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(x: Double, y: Double, width: Double, height: Double, radius: Double) : this(
constructor(rectangle: Rectangle, radius: Double) : this(rectangle.corner, rectangle.width, rectangle.height, radius) 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 */ /** the center of the rounded rectangle */
val center: Vector2 val center: Vector2
@@ -27,16 +38,16 @@ class RoundedRectangle(val corner: Vector2, val width: Double, val height: Doubl
moveTo(x + r, y) moveTo(x + r, y)
lineTo(x + width - 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) 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) 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) lineTo(x, y + r)
curveTo(Vector2(x, y), Vector2(x + r, y)) arcTo(r, r, 90.0, false, true, Vector2(x + r, y))
close() 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) = 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) = 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) = fun Drawer.roundedRectangle(roundedRectangle: RoundedRectangle) =
contour(roundedRectangle.contour) contour(roundedRectangle.contour)
fun Rectangle.toRounded(radius: Double) = RoundedRectangle(this, radius) fun Rectangle.toRounded(radius: Double) = RoundedRectangle(this, radius)