Last active
April 24, 2022 22:46
-
-
Save castillejoale/8d414e70fcec0b51ead300f361cd6085 to your computer and use it in GitHub Desktop.
Swift - interpolation function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import simd | |
static func interpolate(desiredX: inout [Float], y: inout [Float]) -> [Float] { | |
let n = desiredX.count - 1 | |
let stride = vDSP_Stride(1) //Int Type Alias | |
let denominator = Float(n) / Float(y.count - 1) | |
let control: [Float] = desiredX.map { | |
let x = Float($0) / denominator | |
let smoothStep = Float(simd_smoothstep(0, 1, simd_fract(x))) | |
return Float(floor(x)) + smoothStep | |
} | |
var desiredY = [Float](repeating: 0, count: desiredX.count) | |
vDSP_vlint(&y, | |
control, stride, | |
&desiredY, stride, | |
UInt(desiredX.count), | |
UInt(y.count)) | |
return desiredY | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment