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_2_Aproksymacja
{
  public partial class Form1 : Form
  {
    Log log;
    Poli poli = new Poli();
    Random rnd = new Random();

    public Form1()
    {
      InitializeComponent();
      log = new Log(textBox1);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      double[] v = new double[5];

      for (int i = 0; i < v.Length; i++)
        v[i] = rnd.NextDouble() * 100 - 50;

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

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

      log.l("Macierz M:", M);
    }

    private void button2_Click(object sender, EventArgs e)
    {
      int n = 100;
      double[] x = new double[n];
      double[] y = new double[n];
      double[] y2 = new double[n];
      double x1 = -10;
      double x2 = 10;
      double dx = (x2 - x1) / (n - 1);
      for (int i = 0; i < n; i++)
      {
        x[i] = x1 + i * dx;
        y[i] = 3 * x[i] * x[i] - 2 * x[i] + 30;
        y2[i] = 3.5 * x[i] * x[i] - 1.3 * x[i] + 30;
      }
      ChartForm.ShowPoints(x, y, "wielomian A");
      ChartForm.ShowLines(x, y2, "wielomian B");

    }

    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.Oblicz_a();
      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");
    }
  }
}
