📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_graficar_Click(object sender, EventArgs e)
{
int n = dataGridView1.RowCount; //AQUI SE TOMA EL NUMERO DE CELDAS QUE ESTAN ACTIVAS EN EL DATAGRID
float[] x = new float[n-1]; //DECLARO UN ARREGLO DE NUMERO FLOTANTE CON DIMENSION N-1 PORQUE EN EL DATAGRID UNA CELDA NO SE USA
float[] y = new float[n-1];
//TOMO LOS DATOS DEL DATAGRID Y LOS ALMACENO EN LOS ARREGLOS RESPECTIVOS DE X Y F(X)
for (int i = 0; i < n-1; i++)
{
x[i] = float.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString());
y[i] = float.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
}
//ENVIO LOS ARREGLOS DE X E Y PARA LA FUNCION LAGRAGE JUNTO CON EL NUMERO DE PUNTOS INGRESADOS
lagrange(x, y, n - 1);
}
// AQUI LA FUNCION LAGRANGE CALCULA EL VALOR DE LA INCOGNITA EN EL POLINOMIO Y GRAFICA
public void lagrange(float[] x, float[] y, int n)
{
float producto = 1;
float suma = 0;
PointF[] puntos=new PointF[133]; //DECLARO UN ARREGLO DE PUNTOS PARA PODER GRAFICAR LA FUNCION CON UNA LINEA CONTINUA
int p=0;
Graphics DispositivoGrafico = pictureBox1.CreateGraphics(); //DECLARO UN OBJETO TIPO GRAPHICS EL CUAL ES DONDE SE GRAFICARA LA FUNCION
DispositivoGrafico.Clear(Color.White);
DispositivoGrafico.DrawLine(new Pen(Color.Red, 1), pictureBox1.Width / 16, 0, pictureBox1.Width / 16, Height); //DIBUJA EL EJE DE LAS Y
DispositivoGrafico.DrawLine(new Pen(Color.Red, 1), 0, pictureBox1.Height / 2, Width, pictureBox1.Height / 2); //DIBUJO EL EJE DE LAS X
//A CONTINUACION ESTA EL CALCULO MEDIANTE LA FORMULA DE LAGRANGE
for (float k = 0; k < 13.3F; k += 0.1F)
{
suma = 0;
for (int i = 0; i < n; i++)
{
producto = 1;
for (int j = 0; j < n; j++)
{
if (i != j)
{
producto = producto * ((k - x[j]) / (x[i] - x[j]));
}
}
suma = suma + producto * y[i];
}
puntos[p] = new PointF((pictureBox1.Width / 16 + (k * 50)), (pictureBox1.Height / 2 - (suma * 50))); //AQUI ALMACENO LAS COORDENADAS EN EL ARREGLOS DE PUNTOS
p++;
}
for (int i = 0; i < n-1; i++)
DispositivoGrafico.DrawRectangle(new Pen(Color.Red, 2), pictureBox1.Width / 16 + (x[i] * 50), pictureBox1.Height / 2 - (y[i] * 50), 1, 1); //DIBUJO DE COLOR ROJO LOS PUNTOS INGRESADOS
DispositivoGrafico.DrawBeziers(new Pen(Color.Blue, 2), puntos); //ESTA SENTENCIA GRAFICA LOS PUNTOS DEL ARREGLO CON UNA SOLA LINEA CONTINUA
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -