none
Drawing complex math functions like sin, cos, arcsin...

    Question

  • Hi i would like to write a application that would draw a complex math functions.
    I dont know how to start and what classes to use. I tryed to use object Graphics
    but it can only draw simple shapes.
    Can enyone provide to me some tutorials how to do this?

    Thanks in advance!
    magneto
    Sunday, March 09, 2008 12:01 AM

Answers

  • Hi,

    You can use an float array to store the points, and use the Grapics.DrawLines() method to draw the points, see my sample below for the details.

    Code Snippet

    public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            protected override void OnPaint(PaintEventArgs e)

            {

                //I just draw a Sin graphics between 0-2π for example

                float x0 = 100f;

                float y0 = 100f;

              

                //Assume the graphics width is 200pixels

                //so there're 200 points

                PointF[] points = new PointF[200];

                for (int j = 0; j < 200; j++)

                {

                    points[j] = new PointF();

                    points[j].X = x0 + j;

                    points[j].Y = y0 -
                      (float)(Math.Sin((2 * Math.PI * j) / 200) * (200 / (2 * Math.PI)));

                }

     

                using (Pen p = new Pen(Color.Blue))

                {

                    p.EndCap = LineCap.ArrowAnchor;

                    //Draw X-coordinate

                    e.Graphics.DrawLine(p, x0, y0, x0 + 250, y0);

                    //Draw Y-coordinate

                    e.Graphics.DrawLine(p, x0, y0 + 80, x0, y0 - 80);

                }

     

                e.Graphics.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, x0, y0 );

                e.Graphics.DrawString("π", SystemFonts.DefaultFont, Brushes.Blue, x0 + 100, y0);

                e.Graphics.DrawString("2π", SystemFonts.DefaultFont, Brushes.Blue, x0 + 200, y0);

     

                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

                e.Graphics.DrawLines(Pens.Blue, points);

                base.OnPaint(e);

            }

        }

     

     

     

       
    Best Regards,

    Zhi-xin Ye.

    Thursday, March 13, 2008 8:17 AM

All replies

  • Most likely you will have to draw them 1 pixel at a time.

     

    You can do something like (psuedo code):

     

    Create blank bitmap

    Figure out 'x-step' size*

    Set initial X value

    loop from 0 to width of bitmap

        figure out y

        scale x,y so they properly fit on bitmap**

        increase X value by x-step

    end loop

    Display bitmap

     

    * Say you want to plot from -pi to +pi, and your bitmap is 200 wide. Each pixel would be (+pi - -pi)/200. This is the x-step

     

    ** bitmaps have 0,0 in the upper left corner, with the Y value heading down. To plot properly you'll have to figure out how you are going to move and flip the image so it will fit in the bitmap. Check out Regions for another way to deal with this.

     

     

    Sunday, March 09, 2008 12:33 AM
  • But i don't understand how can I draw X, Y cordinates of each point because they will not always be integer number.
    When X,Y(2.5;6.78) what I have to do?
    Sunday, March 09, 2008 9:04 AM
  • Hi,

    You can use an float array to store the points, and use the Grapics.DrawLines() method to draw the points, see my sample below for the details.

    Code Snippet

    public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            protected override void OnPaint(PaintEventArgs e)

            {

                //I just draw a Sin graphics between 0-2π for example

                float x0 = 100f;

                float y0 = 100f;

              

                //Assume the graphics width is 200pixels

                //so there're 200 points

                PointF[] points = new PointF[200];

                for (int j = 0; j < 200; j++)

                {

                    points[j] = new PointF();

                    points[j].X = x0 + j;

                    points[j].Y = y0 -
                      (float)(Math.Sin((2 * Math.PI * j) / 200) * (200 / (2 * Math.PI)));

                }

     

                using (Pen p = new Pen(Color.Blue))

                {

                    p.EndCap = LineCap.ArrowAnchor;

                    //Draw X-coordinate

                    e.Graphics.DrawLine(p, x0, y0, x0 + 250, y0);

                    //Draw Y-coordinate

                    e.Graphics.DrawLine(p, x0, y0 + 80, x0, y0 - 80);

                }

     

                e.Graphics.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, x0, y0 );

                e.Graphics.DrawString("π", SystemFonts.DefaultFont, Brushes.Blue, x0 + 100, y0);

                e.Graphics.DrawString("2π", SystemFonts.DefaultFont, Brushes.Blue, x0 + 200, y0);

     

                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

                e.Graphics.DrawLines(Pens.Blue, points);

                base.OnPaint(e);

            }

        }

     

     

     

       
    Best Regards,

    Zhi-xin Ye.

    Thursday, March 13, 2008 8:17 AM
  • Thanks to all :)

    vinu
    • Proposed as answer by Vinaya Kumar Monday, May 07, 2012 8:00 PM
    Friday, December 02, 2011 7:36 PM
  • Hi i have only 4 points can make sine wave using c#.net blow my coding please guys help me

     float x0 = 400f;
                float y0 =400f;
                PointF[] points = new PointF[5];

                int j = 0;
                //do
                //{

                float[] dec = new float[10];
                dec[0] = 1.84f;
                dec[1] = 0.86f;
                dec[2] = 0.59f;
                dec[3] = 0.44f;
                dec[4] = 0.35f;
               



                for (j = 0; j < 5; j++)
                {
                    points[j] = new PointF();
                    points[j].X = (x0 + (dec[j]);

                    points[j].Y = (y0 - ((float)(Math.Sin((2 * Math.PI * j) / 30) * (30 / (2 * Math.PI)))));
                   // points[j].Y = y0 - (float)(Math.Sin(j));
                }

                using (Pen p = new Pen(Color.Blue))
                {
                    p.EndCap = LineCap.ArrowAnchor;

                    //Draw X-coordinate

                    e.Graphics.DrawLine(p, x0, y0, x0 + 600, y0);

                    //Draw Y-coordinate
                    e.Graphics.DrawLine(p, x0, y0 + 400, x0, y0 - 600);
                }

                e.Graphics.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, x0, y0);
                e.Graphics.DrawString("p", SystemFonts.DefaultFont, Brushes.Blue, x0 + 1000, y0);
                e.Graphics.DrawString("2p", SystemFonts.DefaultFont, Brushes.Blue, x0 + 2000, y0);

                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                e.Graphics.DrawLines(Pens.Blue, points);
                base.OnPaint(e);

    Friday, December 14, 2012 1:32 PM