alexgeek's Web Design and Development Blog Logo The mindless banter that is,

AlexGeek's Web Design and Development Blog

Periodic Sequences Generator C#

09 Oct 2010 - Category: C#

Thought I'd share this small class I made, it will produce a periodic sequence between a low and high limit. For example it can produce:
1,2,1,2,1,2...
3,4,5,6,3,4,5,6...

I plan to adapt it to produce wave movements for water in XNA. Anyway, there's the code, mostly commented:

 public class PeriodicSequence
    {
        private float ctr = 0;
        private float modulo;
        private float lowTerm;
        private float highTerm;

        /// 
        /// Plug in the highest and lowest terms of the periodic sequence.
        /// The number of unique terms will be the highest minus the lowest plus one.
        /// The sequence is zero based in that the fist term is n=0.
        /// 
        /// Lowest term in the sequence
        /// Highest term in the sequence
        public PeriodicSequence(float LowTerm, float HighTerm)
        {
            lowTerm = LowTerm;
            highTerm = HighTerm;
            modulo = HighTerm-lowTerm+1;
        }
        /// 
        /// Returns the next term in the sequence starting at n=0.
        /// 
        /// Evaluation of the current term.
        public float NextTerm()
        {
            return (mod((ctr++), modulo, (lowTerm)));
        }
        private float getTerm(float term)
        {
            return (mod(term, modulo, (modulo)));
        }

        /// 
        /// Get a specific term in the sequence e.g. when n=5,
        /// ModuloSequence s = new ModuloSequence(foo, bar);
        /// float n_5 = s[5];
        /// 
        /// Nth term of sequence.
        /// Evaluation term given in parameter.
        public float this[int Term]
        {
            get { return getTerm(Term); }
        }

        /// 
        /// The current point at which the sequence is, i.e. n 
        /// 
        public float CurrentTerm
        {
            get { return ctr; }
            set{ctr = CurrentTerm;}
        }

        private float mod(float val, float mod, float offset)
        {
            int n = 0;
            while (val >= mod)
            {
                val -= mod;
                n++;
            }
            val += (lowTerm);
            return val;
        }
    }

Post A Comment

Name:Email:

What is 6 + 15?

Bette - 01 Feb 2012

Thank God! Someone with brains spkeas!

qhgrkunux - 01 Feb 2012

WXHgSP qzcptirrdoya

ffesvnyissa - 04 Feb 2012

TzDtsm rwhamxbaehju