Skip to content

Commit

Permalink
Added range value calculations Helper (#53)
Browse files Browse the repository at this point in the history
* Added range value calculations Helper

* overload linearInterpolation

* fix build error

---------

Co-authored-by: Giulia Ariu <>
Co-authored-by: Oguz Yuksel <74555694+OguzYuuksel@users.noreply.github.com>
  • Loading branch information
GiuliaAriu and OguzYuuksel committed Jan 2, 2024
1 parent 1a129f2 commit c441f8f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Sources/FoundationExtensions/Numeric/Numeric+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ import Foundation

// MARK: - Interpolation & Progress for Floating Points
extension BinaryFloatingPoint {

/// Linear Interpolation between two floating-point numbers of same type
/// (https://en.wikipedia.org/wiki/Linear_interpolation)
/// Usage:
/// assert(Double.linearInterpolation(minimum: 0.0, maximum: 10.0, percentage: 0.5) == 5.0)
/// assert(Double.linearInterpolation(minimum: 1.0, maximum: 2.0, percentage: 0.5) == 1.5)
/// assert(Double.linearInterpolation(minimum: 1.0, maximum: 3.0, percentage: 0.2) == 1.4)
/// assert(Double.linearInterpolation(minimum: 1.0, maximum: 3.0, percentage: 2) == 3)
/// assert(Double.linearInterpolation(minimum: 1.0, maximum: 3.0, percentage: 2, constrainedToValidPercentage: false) == 5)
///
/// - Parameters:
/// - minimum: lower number
/// - maximum: greater number
/// - percentage: point in interpolation where the result should be, from 0.0 to 1.0
/// - constrainedToValidPercentage: constrains the percentage between 0 and 1
/// - Returns: the normalized number between maximum and minimum, given the percentage progress
public static func linearInterpolation(minimum: Self, maximum: Self, percentage: Self) -> Self {
(percentage * (maximum - minimum)) + minimum
public static func linearInterpolation(minimum: Self, maximum: Self, percentage: Self, constrainedToValidPercentage: Bool = true) -> Self {
let percentage = constrainedToValidPercentage
? percentage.clamped(to: 0...1)
: percentage
return (percentage * (maximum - minimum)) + minimum
}

/// Linear Progress between two floating-point numbers of same type
/// It's the dual of Linear Interpolation, for a value we want the percentage, not the opposite.
/// Usage:
Expand Down

0 comments on commit c441f8f

Please sign in to comment.