Graph Drawing With Newton and Lagrange Interpolation Methods in C#
This code for drawing a points array’s graph with lagrange or Newton Interpolation method.

Drawing graph with interpolation methods
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace lagrange
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int x, i = 0, n, flag = 0,lagrange_flag=0,newton_flag=0;
public static int y;
public static Point[] point_array = new Point[30];
public static string name;
private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Brushes.Blue);
pen.Width = 1;
Pen k = new Pen(Brushes.Red);
k.Width = 2;
if (flag > 0)
{
Point[] point_array2 = new Point[i];
Point[] point_array3 = new Point[i];
for (int s = 0; s < i; s++)
{
point_array2[s].X = point_array3[s].X = point_array[s].X;
point_array2[s].Y = point_array3[s].Y = point_array[s].Y;
}
for (int h = 0; h < i; h++)
{
e.Graphics.DrawRectangle(pen, point_array[h].X, point_array[h].Y, 4, 4);
}
/*
#################################################################
# Lagrange's İnterpolation #
# #
#################################################################
*/
if (lagrange_flag == 1)
{
for (float d = point_array2[0].X; d < point_array2[point_array2.Length - 1].X; d += 0.05f)
{
float fx = 0;
for (int v = 0; v < point_array2.Length; v++)
{
float lg = 1;
for (int y = 0; y < point_array2.Length; y++)
{
if (v != y)
{
lg *= (d - point_array2[y].X) / (point_array2[v].X - point_array2[y].X);
}
}
fx += lg * point_array2[v].Y;
}
e.Graphics.DrawEllipse(k, d, fx, 1f, 1f);
}
}
/*
#################################################################
# Newton's İnterpolation #
# #
#################################################################
*/
if (newton_flag==1)
{
float carpim = 1;
for (float d = point_array2[0].X; d < point_array2[point_array2.Length - 1].X; d += 0.05f)
{
float fx = 0;
float[,] dizi2 = new float[i, i];
for (int l = 0; l < point_array2.Length; l++)
{
dizi2[0,Convert.ToInt32( l)] = point_array2[l].Y;
}
for (int v = 1; v < point_array2.Length; v++)
{
for (int y = v; y < point_array2.Length; y++)
{
dizi2[v, y] = (float)(dizi2[v - 1, y]-dizi2[v - 1, y - 1] ) / (point_array2[y].X - point_array2[y - v].X);
}
} int t;
for (int o = 0; o < point_array2.Length; o++)
{
for (t = 0; t < o; t++)
{
carpim*=( d-point_array2[t].X);
}
carpim *= dizi2[o, o];
fx += carpim;
carpim = 1;
}
e.Graphics.DrawEllipse(k, d, fx, 1f, 1f);
}
}
}
if (flag == 0)
{
for (int h = 0; h < i; h++)
{
Font newfont = new Font("Verdana", 6);
PointF points = new PointF(point_array[h].X, point_array[h].Y);
string cumle = "{" + point_array[h].X + " , " + point_array[h].Y + "}";
e.Graphics.DrawRectangle(pen, point_array[h].X, point_array[h].Y, 4, 4);
e.Graphics.DrawString(cumle, newfont, Brushes.Black, points);
}
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
point_array[i].X = e.X;
point_array[i].Y = e.Y;
n = i + 1;
listBox1.Items.Add(n + ") " + x + " " + y);
i++;
if (flag > 0)
{
int l;
int p;
for (int j = 0; j < i - 1; j++)
{
if (point_array[j].X > point_array[j + 1].X)
{
l = point_array[j + 1].X;
p = point_array[j + 1].Y;
point_array[j + 1].X = point_array[j].X;
point_array[j + 1].Y = point_array[j].Y;
point_array[j].X = l;
point_array[j].Y = p;
j = -1;
}
}
listBox1.Items.Clear();
for (int t = 0; t < i; t++)
{
listBox1.Items.Add(t + 1 + ") " + point_array[t].X + " " + point_array[t].Y);
}
this.Refresh();
}
this.Refresh();
}
private void button3_Click(object sender, EventArgs e)
{
lagrange_flag = 1;
newton_flag = 0;
int b;
int c;
for (int j = 0; j < i - 1; j++)
{
if (point_array[j].X > point_array[j + 1].X)
{
b = point_array[j + 1].X;
c = point_array[j + 1].Y;
point_array[j + 1].X = point_array[j].X;
point_array[j + 1].Y = point_array[j].Y;
point_array[j].X = b;
point_array[j].Y = c;
j = -1;
}
}
listBox1.Items.Clear();
for (int t = 0; t < i; t++)
{
listBox1.Items.Add(t + 1 + ") " + point_array[t].X + " " + point_array[t].Y);
}
flag++;
this.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
int b;
int c;
newton_flag = 1;
lagrange_flag = 0;
for (int j = 0; j < i - 1; j++)
{
if (point_array[j].X > point_array[j + 1].X)
{
b = point_array[j + 1].X;
c = point_array[j + 1].Y;
point_array[j + 1].X = point_array[j].X;
point_array[j + 1].Y = point_array[j].Y;
point_array[j].X = b;
point_array[j].Y = c;
j = -1;
}
}
listBox1.Items.Clear();
for (int t = 0; t < i; t++)
{
listBox1.Items.Add(t + 1 + ") " + point_array[t].X + " " + point_array[t].Y);
}
flag++;
this.Refresh();
}
private void button4_Click(object sender, EventArgs e)
{
i = 0; flag = 0; lagrange_flag = 0; newton_flag = 0;
listBox1.Items.Clear();
for (int s = 0; s < point_array.Length; s++)
{
point_array[s].X = 0;
point_array[s].Y = 0;
}
this.Refresh();
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
}
}