DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

[DZone Research] Observability + Performance: We want to hear your experience and insights. Join us for our annual survey (enter to win $$).

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • SwiftData Dependency Injection in SwiftUI Application
  • HLS Streaming With AVKit and Swift UI for iOS and tvOS
  • Inverse Distance Weighting Interpolation in Python
  • Leetcode: Improving String Performance in Swift

Trending

  • Five Tools for Data Scientists to 10X their Productivity
  • Multi-Tenancy With Keycloak, Angular, and SpringBoot
  • LLMs for Bad Content Detection: Pros and Cons
  • The Winds of Change: How Generative AI is Revolutionizing Cybersecurity
  1. DZone
  2. Coding
  3. Frameworks
  4. Playing With Interpolation Functions in Swift

Playing With Interpolation Functions in Swift

Follow this short guide to interpolation options for your Swift apps.

Simon Gladman user avatar by
Simon Gladman
·
Feb. 02, 16 · Tutorial
Like (1)
Save
Tweet
Share
4.14K Views

Join the DZone community and get the full member experience.

Join For Free

One of the subjects I'll be covering in my upcoming book, Core Image for Swift, is custom transitions and, as part of that, I wanted to add different interpolations for the transition. I found a great post, Interpolation Tricks, which beautifully explains different interpolation functions and I thought it would be nice to port some of that code to Swift and create a handy playground to demonstrate them.

So, here for your enjoyment is my Interpolation Playground.

All the different interpolations use a single loop function to draw the graph. This function accepts the interpolation function as an argument which is of type (Double) -> Double. 

func loop( interpolationFunction: Double -> Double )
{
    let n = 50

    for i in 0 ..< n
    {
        let v = Double(i) / Double(n)

        let _ = interpolationFunction(v)
    }

}

The first example is linear interpolation where the call is simply 

        loop { x in x }

Next is smoothstep where the call is:

        loop { x in ((x) * (x) * (3 - 2 * (x))) }

Then onto smootherstep where the call is:

        loop { x in ((x) * (x) * (x) * ((x) * ((x) * 6 - 15) + 10)) }




Squared and inverse squared are pretty much as expected:


        loop { x in (x * x) }

        loop { x in 1 - (1 - x) * (1 - x) }

Now we start to get funky with Catmull-Rom interpolation - try playing with the q and t values in the playground!


        func catmullRom(t: Double, _ p0: Double, _ p1: Double, _ p2: Double, _ p3: Double) -> Double
        {
            return 0.5 * (
                (2 * p1) +
                (-p0 + p2) * t +
                (2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t +
                (-p0 + 3 * p1 - 3 * p2 + p3) * t * t * t
            )
        }

        let q = -15.0
        let t = 7.5

        loop { x in catmullRom(x, q, 0.0, 1.0, t) }

There are also two elastic interpolations - in and out. The original code comes from Robert Penner in this post with a refactor from Josh on Design in this post.

For elastic ease in, the code is:

 func elasticIn(x: Double, p: Double = 0.2) -> Double
        {
            return pow(2, 10 * (x - 1)) * sin((x - p/4) * (2 * M_PI) / p) + 1
        }

        loop { x in elasticIn(x) }

Ease out is almost the same code:

func elasticOut(x: Double, p: Double = 0.2) -> Double
        {
            return pow(2, -10 * x) * sin((x - p/4) * (2 * M_PI) / p) + 1
        }

        loop { x in elasticOut(x) }




This playground is available at my GitHub repository here. Xcode doesn't seem to support source control for playgrounds, so you'll need to download the zip file. 

For additional functions, Robert Penner's Easing.as ActionScript code has loads of great examples and is available at his repo here.

Interpolation Swift (programming language)

Published at DZone with permission of Simon Gladman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • SwiftData Dependency Injection in SwiftUI Application
  • HLS Streaming With AVKit and Swift UI for iOS and tvOS
  • Inverse Distance Weighting Interpolation in Python
  • Leetcode: Improving String Performance in Swift

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: