0

How to draw triangle wave (symmetrical) using ZedGraph?

alt text http://img101.imageshack.us/img101/8482/okr20troj.jpg

Preferably with option to adjust period and amplitude.

//Edit: the function has to be [related/based on]? x (x-axis).

Something like this:

for (x = 0; x <= 10; x += .005)
{
   if (Math.Sin(x * (2 * Math.PI / period)) >= 0)
      y = amplitude;
   else
      y = -amplitude;
   originalList.Add(x, y);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
o_O
  • 516
  • 2
  • 8
  • 25

1 Answers1

0
double amplitude = 1.7;
                double period = 2;
                PointPairList ppl = new PointPairList();
                double y=0;
                for (double x = 0; x <= 10; x += .005)
                {
                    double p = (x % (period)) / period ;
                    if (p >= 0 && p <= 0.25)
                        y = 4 * p * amplitude;
                    if (p > 0.25 && p < 0.5)
                        y = amplitude - (p - 0.25) * 4 * amplitude;
                    if(p>0.5 && p<=0.75)
                        y = - 4 * (p-0.5) * amplitude;
                    if(p>0.75 && p<=1)
                        y = - (amplitude - (p - 0.75) * 4 * amplitude);
                    ppl.Add(x,y);
                }

                var line = zg1.MasterPane[0].AddCurve("", ppl, Color.Blue);
                line.Symbol.IsVisible = false;
                zg1.AxisChange();
                zg1.Refresh();
Gacek
  • 10,184
  • 9
  • 54
  • 87
  • tx, but i forgot to add one requirement (now added) – o_O Nov 02 '09 at 19:15
  • well, technically, what I wrote is "based on x axis". What is the difference? Just replace "i" with "x" and that's all. And second thing - the procedure you have written is generating values for square wave, not the triangle wave. But OK, I will change the code... – Gacek Nov 03 '09 at 19:21