using System;
using System.Collections.Generic;
using System.Text;

namespace GraphiXNA.MathHelpers
{
    public static class Gaussian
    {
        public static void Fill(float[] Weights, float theta)
        {
            if (Weights == null)
            {
                throw new ArgumentNullException("Weights");
            }

            if ((Weights.Length % 1) == 1)
            {
                throw new ArgumentException("Array length must be odd.");
            }

            Weights[0] = Formula(0, theta);

            // Maintain a sum of all the weighting values.
            float totalWeights = Weights[0];

            // Add pairs of additional sample taps, positioned
            // along a line in both directions from the center.
            for (int i = 0; i < Weights.Length / 2; i++)
            {
                // Store weights for the positive and negative taps.
                float weight = Formula(i + 1, theta);

                Weights[i * 2 + 1] = weight;
                Weights[i * 2 + 2] = weight;

                totalWeights += weight * 2;
            }

            //To loop through arrays, it's more efficent to write Weights.Length than writing 15;
            for (int i = 0; i < Weights.Length; i++)
            {
                Weights[i] /= totalWeights;
            }
        }

        private static float Formula(float radius, float theta)
        {
            theta += float.Epsilon;
            
            double d = (0.39894228 * theta) * Math.Exp(-(radius * radius) / (2 * theta * theta));
            
            return (float)d;
        }
    }
}
