xref: /aosp_15_r20/frameworks/base/packages/SystemUI/compose/scene/src/com/android/compose/ui/util/MathHelpers.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 package com.android.compose.ui.util
19 
20 import androidx.compose.ui.geometry.isSpecified
21 import androidx.compose.ui.geometry.lerp
22 import androidx.compose.ui.unit.IntSize
23 import androidx.compose.ui.util.lerp
24 import com.android.compose.animation.scene.Scale
25 
26 /** Linearly interpolate between [start] and [stop] with [fraction] fraction between them. */
lerpnull27 fun lerp(start: IntSize, stop: IntSize, fraction: Float): IntSize {
28     return IntSize(
29         lerp(start.width, stop.width, fraction),
30         lerp(start.height, stop.height, fraction),
31     )
32 }
33 
34 /** Linearly interpolate between [start] and [stop] with [fraction] fraction between them. */
lerpnull35 fun lerp(start: Scale, stop: Scale, fraction: Float): Scale {
36     val pivot =
37         when {
38             start.pivot.isSpecified && stop.pivot.isSpecified ->
39                 lerp(start.pivot, stop.pivot, fraction)
40             start.pivot.isSpecified -> start.pivot
41             else -> stop.pivot
42         }
43     return Scale(
44         lerp(start.scaleX, stop.scaleX, fraction),
45         lerp(start.scaleY, stop.scaleY, fraction),
46         pivot,
47     )
48 }
49