---
tags:
    - "Lerp"
    - "Slerp"
    - "Linear"
    - "Smooth"
    - "Damp"
    - "Step"
---

# Interpolate

[Math](../index.md) / [Algebra](index.md) / **Interpolate**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

Interpolates a value between A and B given a ratio number

## Code

```cs
using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]
    
    [Title("Interpolate")]
    [Category("Math/Algebra/Interpolate")]
    
    [Description("Interpolates a value between A and B given a ratio number")]
    [Image(typeof(IconInterpolate), ColorTheme.Type.Yellow)]
    
    [Keywords("Lerp", "Slerp", "Linear", "Smooth", "Damp", "Step")]
    
    [Serializable]
    public class TypeNumberMathInterpolate : TypeNumber
    {
        [SerializeField] private GetNumber m_A = new GetNumber();
        [SerializeField] private GetNumber m_B = new GetNumber();
        
        [SerializeField] private GetNumber m_Ratio = new GetNumber();
        [SerializeField] private EaseMode m_Easing = EaseMode.Linear;
        [SerializeField] private GetBool m_Clamp = new GetBool();

        public override double GetPreview(GameObject source)
        {
            float ratio = Ease.Get(this.m_Easing, (float) this.m_Ratio.GetPreview(source));
            return Interpolate(this.m_A.GetPreview(source), this.m_B.GetPreview(source), ratio, this.m_Clamp.GetPreview(source));
        }

        public override double Get(Args args)
        {
            double a = this.m_A.Get(args);
            double b = this.m_B.Get(args);
            
            double ratio = Ease.Get(this.m_Easing, (float) this.m_Ratio.Get(args));
            bool clamp = this.m_Clamp.Get(args);
            
            return Interpolate(a, b, ratio, clamp);
        }

        private static double Interpolate(double a, double b, double ratio, bool clamp)
        {
            if (clamp) ratio = Math.Clamp(ratio, 0.0, 1.0);
            return a + (b - a) * ratio;
        }

        public override string ToString()
        {
            return $"Interpolate {this.m_A} to {this.m_B} by {this.m_Ratio}";
        }
        
        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}
```

