Latest 2.0.0
Homepage https://github.com/dclelland/Lerp
License MIT
Platforms ios 8.0, requires ARC
Authors

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)

Latest podspec

{
    "name": "Lerp",
    "version": "2.0.0",
    "summary": "A linear interpolation microlibrary.",
    "homepage": "https://github.com/dclelland/Lerp",
    "license": {
        "type": "MIT"
    },
    "authors": {
        "Daniel Clelland": "[email protected]"
    },
    "source": {
        "git": "https://github.com/dclelland/Lerp.git",
        "tag": "2.0.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "ios": {
        "source_files": "Lerp.swift"
    },
    "requires_arc": true
}

Pin It on Pinterest

Share This