CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

Lerp 2.2.0

Lerp 2.2.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Aug 2019
SPMSupports SPM

Maintained by Daniel Clelland.



Lerp 2.2.0

  • By
  • Daniel Clelland

Lerp

Lerp is a linear interpolation microlibrary.

I got sick of copying and pasting these functions into every project, so I made a Cocoapod.

Lerp implements a Lerpable protocol on Float, Double, CGFloat, and CGPoint - mostly cribbed from this Stack Overflow post.

Examples

✓ Linear interpolation

lerp(0.5, min: 30, max: 40)
// 35

✓ Inverse linear interpolation

ilerp(35, min: 30, max: 40)
// 0.5

✓ Clamping

clamp(35, min: 30, max: 40)
// 35

clamp(25, min: 30, max: 40)
// 20

clamp(45, min: 30, max: 40)
// 40

CGPoint helpers:

CGPoint(x: 0.5, y: 0.5).lerp(min: CGPoint(x: 0.0, y: 0.0), max: CGPoint(x: 20.0, y: 40.0))
// CGPoint(x: 10.0, y: 20.0)

CGPoint(x: 0.5, y: 0.5).lerp(rect: CGRect(x: 0.0, y: 0.0, width: 20.0, height: 40.0))
// CGPoint(x: 10.0, y: 20.0)

CGPoint(x: 10.0, y: 20.0).ilerp(min: CGPoint(x: 0.0, y: 0.0), max: CGPoint(x: 20.0, y: 40.0))
// CGPoint(x: 0.5, y: 0.5)

CGPoint(x: 10.0, y: 20.0).ilerp(rect: CGRect(x: 0.0, y: 0.0, width: 20.0, height: 40.0))
// CGPoint(x: 0.5, y: 0.5)

CGPoint(x: -10.0, y: 50.0).clamp(min: CGPoint(x: 0.0, y: 0.0), max: CGPoint(x: 20.0, y: 40.0))
// CGPoint(x: 0.0, y: 40.0)

CGPoint(x: -10.0, y: 50.0).clamp(rect: CGRect(x: 0.0, y: 0.0, width: 20.0, height: 40.0))
// CGPoint(x: 0.0, y: 40.0)

CGRect helpers:

CGRect(x: 0.0, y: 0.0, width: 0.5, height: 0.5).lerp(rect: CGRect(x: 0.0, y: 0.0, width: 20.0, height: 40.0))
// CGRect(x: 0.0, y: 0.0, width: 10.0, height: 20.0)

CGRect(x: 0.0, y: 0.0, width: 10.0, height: 20.0).ilerp(rect: CGRect(x: 0.0, y: 0.0, width: 20.0, height: 40.0))
// CGRect(x: 0.0, y: 0.0, width: 0.5, height: 0.5)