Home > Software design >  Scale graph of function
Scale graph of function

Time:05-01

I'm trying to make a Windows forms app that will display graphs of functions.

enter image description here

How could I stretch the graph so it would go more to the sides ?

  private void Graphs_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawLine(new Pen(Color.Black), 0, ClientSize.Height / 2, ClientSize.Width, ClientSize.Height / 2);
        e.Graphics.DrawLine(new Pen(Color.Black), ClientSize.Width / 2, 0, ClientSize.Width / 2, ClientSize.Height);
        for (double x = -100; x < 100; x  = 0.1)
        {
            double y = x * x * -1;
            //y = x * -1;   
            //y = Math.Sqrt(x) * -1;
            //y = 1 / x;
            try
            {
                e.Graphics.FillEllipse(new SolidBrush(Color.Red), (ClientSize.Width / 2) - 5   Convert.ToInt32(x), (ClientSize.Height / 2) - 5   Convert.ToInt32(y), 10, 10);

            }
            catch (Exception)
            {
            
            }
        }
    }

CodePudding user response:

Add separate scaling factors for x and y; currently you have y = x²| -100<x<100, so y is between 0 and 10,000.

There isn't a 1:1 aspect ratio which will show that curve in a square box, so you can either show less of it or scale the axes differently.

I'd fix the viewport, to say -100<=x<=100 and 0<=y<=10000 then calculate xScale and yScale from ClientSize and the viewport extents.

  • Related