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) :
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();
}
}