using GenDanDoZad;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _03_1_Aproksymacja
{
  public partial class Form1 : Form
  {
    Log log;
    Random rnd = new Random();
    public Form1()
    {
      InitializeComponent();
      log = new Log(textBox1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      double[] v = new double[5];
      double[] w = new double[5];
      for (int i = 0; i < v.Length; i++)
      {
        v[i] = rnd.NextDouble() * 10 - 5;
        w[i] = v[i] * v[i] * 3 - 2 * v[i] + 20;
      }

      log.l("Wektor v", v);

      double[,] A = new double[5, 5];
      for (int i = 0; i < A.GetLength(0); i++)
        for (int j = 0; j < A.GetLength(1); j++)
          A[i, j] = rnd.NextDouble() * 30 - 15;

      log.l("Macierz A", A);

      ChartForm.ShowPoints(v, w, "seria A");
    }

    private void button2_Click(object sender, EventArgs e)
    {
      double[] x = new double[50];
      double[] y = new double[50];
      double x1 = -10;
      double x2 = 10;
      double dx = (x2 - x1) / (x.Length - 1);
      for (int i = 0; i < x.Length; i++)
      {
        x[i] = x1 + i * dx;
        y[i] = x[i] * x[i] * 3 - 2 * x[i] + 20;
      }

      ChartForm.ShowLines(x,y,"seria");
    }


    //======================================================

    Poli poli = new Poli();
    double[] X;
    double[] Y;

    private void button3_Click(object sender, EventArgs e)
    {
      poli.n = (int)numericUpDown1.Value;
      poli.m = (int)numericUpDown2.Value;
      poli.x = new double[poli.n];
      poli.y = new double[poli.n];
      double x1 = -5;
      double x2 = 5;
      double dx = (x2 - x1) / (poli.n - 1);
      for (int k = 0; k < poli.n; k++)
      {
        poli.x[k] = x1 + k * dx;
        poli.y[k] =
          0.25 * Math.Pow(poli.x[k], 5) +
          0.11 * Math.Pow(poli.x[k], 4) +
          0.33 * Math.Pow(poli.x[k], 3) +
          1.5 * Math.Pow(poli.x[k], 2) +
          4.2 * Math.Pow(poli.x[k], 1) +
          20 +
          rnd.NextDouble() * 200 - 100;
      }
      ChartForm.ShowPoints(poli.x, poli.y, "punkty");
      log.l("x:", poli.x, true);
      log.l("y:", poli.y, true);
    }

    private void button4_Click(object sender, EventArgs e)
    {
      poli.WyznaczA();
      log.l($"wsp. wielomianu stopnia {poli.m}", poli.a);
    }

    private void button5_Click(object sender, EventArgs e)
    {
      double[] ya = new double[poli.n];
      for (int i = 0; i < poli.n; i++)
        ya[i] = poli.PoliczDlaX(poli.x[i]);

      ChartForm.ShowLines(poli.x, ya, "wielomian aproksymujący");
    }
  }
}
