Bilinear interpolation. - CSharp System

CSharp examples for System:Math Number

Description

Bilinear interpolation.

Demo Code

/*//  ww w . j  a va2 s. c  o  m
 * Copyright (C) 2011 - 2013 Voxeliq Engine - http://www.voxeliq.org - https://github.com/raistlinthewiz/voxeliq
 *
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the Microsoft Public License (Ms-PL).
 */
using System;

public class Main{
        /// <summary>
        /// Bilinear interpolation.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="q11"></param>
        /// <param name="q12"></param>
        /// <param name="q21"></param>
        /// <param name="q22"></param>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <param name="y1"></param>
        /// <param name="y2"></param>
        /// <returns></returns>
        public static double BiLerp(double x, double y, double q11, double q12, double q21, double q22, double x1, double x2, double y1, double y2)
        {
            double r1 = Lerp(x, x1, x2, q11, q21);
            double r2 = Lerp(x, x1, x2, q12, q22);
            return Lerp(y, y1, y2, r1, r2);
        }
        /// <summary>
        /// Linear interpolation.
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static double Lerp(double x1, double x2, double p)
        {
            return x1 * (1.0 - p) + x2 * p;
        }
        /// <summary>
        /// Linear interpolation.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <param name="q00"></param>
        /// <param name="q01"></param>
        /// <returns></returns>
        public static double Lerp(double x, double x1, double x2, double q00, double q01)
        {
            return ((x2 - x) / (x2 - x1)) * q00 + ((x - x1) / (x2 - x1)) * q01;
        }
}

Related Tutorials