Externalize fitting algorithm

This commit is contained in:
Ricardo Matias
2020-03-14 16:43:28 +01:00
parent 142f3d52c4
commit e2364c9560

View File

@@ -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<Rectangle, Rectangle> {
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)
}