Combines two input numbers in some proportion.

image_pdfimage_print
   
 

//http://isotopescreencapture.codeplex.com/
//The MIT License (MIT)
namespace Isotope.Math
{
    public static class MathUtil
    {
        /// <summary>
        /// Combines two input numbers in some proportion. 
        /// ratio=0.0 the first number is not used at all, 
        /// ratio=0.5 they are weight equally
        /// ratio=1.0 the first number completely dominates the value
        /// </summary>
        /// <param name="val0"></param>
        /// <param name="val1"></param>
        /// <param name="ratio"></param>
        /// <returns></returns>
        public static double Blend_0_1(double val0, double val1, double ratio)
        {
            var cratio = ClampToRange_0_1(ratio);
            var v0 = val0*cratio;
            var v1 = val1*(1.0 - cratio);
            return v0 + v1;
        }
        /// <summary>
        /// Given an input value will force the value to fit within the range (min,max) inclusive
        /// </summary>
        /// <param name="v"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static double ClampToRange(double v, double min, double max)
        {
            if (v < min)
            {
                v = min;
            }
            else if (v > max)
            {
                v = max;
            }
            return v;
        }

        /// <summary>
        /// Given an input value, will limit it to the range 0.0 and 1.0 inclusive
        /// </summary>
        /// <param name="v"></param>
        /// <returns>the clamped value</returns>
        public static double ClampToRange_0_1(double v)
        {
            return ClampToRange(v, 0.0, 1.0);
        }

    }
}