0

I've tried drawing mathematical curves in C# using WindowsForms PictureBox widget, but the result was not proportional to my intentions ( way too dense and weird) : enter image description here

This was supposed to be a sine function, like in this video: A Youtube Tutorial. I think that the problem is that the range of the axes in the curve is really large.

Do you know any method to limit the X and Y so it looks proportional?

Here is the code - Keep in mind I'm used to python's GUI so I have absolute no idea what I'm doing. Thanks!

public partial class Graphs : Form
{
    Pen pen = new Pen(Color.Red,4.0F);
    List<PointF> points = new List<PointF>();
    
    public Graphs()
    {
        InitializeComponent();
    }

    private void AddPoint(Func<float,float> function,float value)
    {
        float scale = pictureBox1.Height / 2;
        points.Add(new PointF(value, -function(value)*scale + scale)); // minus because y is upside down
    }
    private void AddPoints(float minValue,float maxValue,Func<float,float> function)
    {
        for (float i = minValue; i <= maxValue; i+=5)
        {
            AddPoint(function, i);
        }
    }
    private void Form1_Load(object sender, EventArgs e)
    { }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.Clear(Color.White);
        if(points.Count > 0)
            e.Graphics.DrawCurve(pen, points.ToArray());
        
    }
    private void Invalidate()
    {
        pictureBox1.Invalidate();
    }

    private float Sine(float number) => (float)Math.Sin(number);
   

    
    private void button1_Click(object sender, EventArgs e)
    {
        AddPoints(0, 1000, Sine);
        Invalidate();
    }
}
41 72 6c
  • 1,600
  • 5
  • 19
  • 30
Jonathan
  • 88
  • 1
  • 8
  • I don't see what the problem is. You are taking sin(x) function, which values goes from -1 to +1 and you are multiplying those values by 1 000. So the result is "vertically stretched" to range -1000 to +1000. The code seems to be working as written - the output is a plot of multiplied sine function. The difference in the amplitude is made by the step of 5, I guess. What exactly is the problem? – Matyas Sep 05 '21 at 11:31
  • 2
    Sine is expressed in radians. You're producing a bunch of points that barely *connect* as a curve. Change your `Sine` function to private `float Sine(float angle) => (float)Math.Sin(Math.PI * angle / 180.0) * -1.0f;`. Note `* -1.0f`, you can remove the sign from the function return value. -- Instead of adding `+ scale`, you should have, in the *canvas* Paint handler, something like: `e.Graphics.TranslateTransform(0, pictureBox1.Height / 2); e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;` – Jimi Sep 05 '21 at 13:56
  • 2
    BTW, remove this: `e.Graphics.Clear(Color.White);`. If need to generate new points with different settings, clear the `List` before calling `AddPoints()` -- BTW, there's no method to alter the Period, you can just modify the Amplitude here (i.e., you'll be drawing outside the canvas bounds in X). – Jimi Sep 05 '21 at 14:34
  • thanks for the help Jimi. It completely fails with other functions, but at least by converting to degrees I've got a nice wave ;) – Jonathan Sep 05 '21 at 15:18

0 Answers0