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 _07_1_Calkowanie
{
  public partial class Form1 : Form
  {
    Log log;
    Preprocesor prep = new Preprocesor();
    Integrator integr = new Integrator();

    public Form1()
    {
      InitializeComponent();
      log = new Log(tbLog);
      log.SetFontSize(18);
    }

    double fun1(double x)
    {
      return Math.Exp(-x * x / 2);
    }

    private void button1_Click(object sender, EventArgs e)
    {
      prep.n = 10;
      prep.a = 0;
      prep.b = 2;
      prep.fun = fun1;
      prep.Oblicz();
      log.l("X:", prep.X, true);
      log.l("Y:", prep.Y, true);
      double pr0 = integr.Prostokaty(prep.X, prep.Y);
      log.l($"Met.prostok: {pr0}");
    }

    private void button2_Click(object sender, EventArgs e)
    {
      prep.n = 10;
      prep.a = 0;
      prep.b = 2;
      prep.fun = fun1;
      prep.Oblicz();
      log.l("X:", prep.X, true);
      log.l("Y:", prep.Y, true);
      double pr0 = integr.Trapezy(prep.X, prep.Y);
      log.l($"Met.trapez.: {pr0}");
    }

    private void button3_Click(object sender, EventArgs e)
    {
      prep.n = 6000;
      prep.a = 0;
      prep.b = 2;
      prep.fun = fun1;
      prep.Oblicz();
      log.l("X:", prep.X, true);
      log.l("Y:", prep.Y, true);
      double pr0 = integr.Simpson(prep.X, prep.Y);
      log.l($"Wzór Simpsona: {pr0}");
    }


    double c = 1.19628801332261;
    double eps = 0.0001;

    private void button4_Click(object sender, EventArgs e)
    {
      int N = 4;
      int j = N;
      double wyn = 0;
      do
      {
        prep.n = j;
        prep.a = 0;
        prep.b = 2;
        prep.fun = fun1;
        prep.Oblicz();
        wyn = integr.Prostokaty(prep.X, prep.Y);
        //log.l($"prost: {wyn}  n:{prep.n}");
        j = j + 2;
      } while (Math.Abs(wyn - c) > eps);
      log.l($"met.prostok: dla eps<={eps} wymaga n: {prep.n}");
    }

    private void button5_Click(object sender, EventArgs e)
    {
      int N = 4;
      int j = N;
      double wyn = 0;
      do
      {
        prep.n = j;
        prep.a = 0;
        prep.b = 2;
        prep.fun = fun1;
        prep.Oblicz();
        wyn = integr.Trapezy(prep.X, prep.Y);
        //log.l($"trap: {wyn}  n:{prep.n}");
        j = j + 2;
      } while (Math.Abs(wyn - c) > eps);
      log.l($"met.trap: dla eps<={eps} wymaga n: {prep.n}");
    }

    private void button6_Click(object sender, EventArgs e)
    {
      int N = 4;
      int j = N;
      double wyn = 0;
      do
      {
        prep.n = j;
        prep.a = 0;
        prep.b = 2;
        prep.fun = fun1;
        prep.Oblicz();
        wyn = integr.Simpson(prep.X, prep.Y);
        //log.l($"trap: {wyn}  n:{prep.n}");
        j = j + 2;
      } while (Math.Abs(wyn - c) > eps);
      log.l($"met.Simp.: dla eps<={eps} wymaga n: {prep.n}");
    }


    //funcja: f(x) = √(1+x)
    double PierXp1(double x) { return Math.Sqrt(1 + x); }

    //Całka z ∫f(x)dx = ∫√(x+1)dx = 2√[(x+1)^(3/2)]/3 (bez stałej całkowania) 
    double CPierw1PlusX(double x)
    {
      return 2 * Math.Pow(x + 1, 1.5) / 3;
    }


  }
}
