From e2364c95607ba6cbcc94d3425b4b01a825d82fc1 Mon Sep 17 00:00:00 2001 From: Ricardo Matias Date: Sat, 14 Mar 2020 16:43:28 +0100 Subject: [PATCH] Externalize fitting algorithm --- orx-image-fit/src/main/kotlin/ImageFit.kt | 45 +++++++++++++++++------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/orx-image-fit/src/main/kotlin/ImageFit.kt b/orx-image-fit/src/main/kotlin/ImageFit.kt index 519398ed..950c1557 100644 --- a/orx-image-fit/src/main/kotlin/ImageFit.kt +++ b/orx-image-fit/src/main/kotlin/ImageFit.kt @@ -11,28 +11,28 @@ enum class FitMethod { Contain } -fun Drawer.imageFit( - img: ColorBuffer, - x: Double = 0.0, - y: Double = 0.0, - width: Double = img.width.toDouble(), - height: Double = img.height.toDouble(), +fun fitRectangle( + src: Rectangle, + dest: Rectangle, horizontalPosition: Double = 0.0, verticalPosition: Double = 0.0, fitMethod: FitMethod = FitMethod.Cover -) { - val sourceWidth = img.width.toDouble() - val sourceHeight = img.height.toDouble() - - var targetX = x - var targetY = y +): Pair { + val sourceWidth = src.width + val sourceHeight = src.height + var targetX: Double + var targetY: Double var targetWidth: Double var targetHeight: Double val source: Rectangle val target: Rectangle + val (x, y) = dest.corner + val width = dest.width + val height = dest.height + when (fitMethod) { FitMethod.Contain -> { targetWidth = width @@ -87,5 +87,26 @@ fun Drawer.imageFit( } } + return Pair(source, target) +} + +fun Drawer.imageFit( + img: ColorBuffer, + x: Double = 0.0, + y: Double = 0.0, + width: Double = img.width.toDouble(), + height: Double = img.height.toDouble(), + horizontalPosition: Double = 0.0, + verticalPosition: Double = 0.0, + fitMethod: FitMethod = FitMethod.Cover +) { + val (source, target) = fitRectangle( + img.bounds, + Rectangle(x, y, width, height), + horizontalPosition, + verticalPosition, + fitMethod + ) + image(img, source, target) } \ No newline at end of file