Skip to content

dclelland/Lerp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lerp

Lerp is a linear interpolation microlibrary.

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)
// 30

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)

About

Linear interpolation microlibrary

Resources

License

Stars

Watchers

Forks

Packages

No packages published