unit KonvKernelUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.Buttons, MaticeUnit;

type
  TKonvKernellForm = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Panel1: TPanel;
    StringGrid1: TStringGrid;
    Button1: TButton;
    Label1: TLabel;
    Button2: TButton;
    BitBtn2: TBitBtn;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  MaticeJadra: TMatice;
  end;

var
  KonvKernellForm: TKonvKernellForm;
  velikost: Integer;
  sigma: Double;
  function Gauss(x, y, sig: Double): Double;
  function Vzdalenost(x1, y1, x2, y2: Double): Double;

implementation

{$R *.dfm}
function Gauss(x, y, sig: Double): Double;
begin
  Result := Exp(-(sqr(x)+sqr(y))/(2*sqr(sig)));
end;

function Vzdalenost(x1, y1, x2, y2: Double): Double;
begin
  Result := sqrt(sqr(x2-x1)+sqr(y2-y1));
end;

procedure TKonvKernellForm.Button1Click(Sender: TObject);
var
  i, j, StredX, StredY: Integer;
  d: Double;
begin
  sigma := StrToFloatDef(Edit1.Text, 3);
  Edit1.Text := FloatToStr(sigma);
  MaticeJadra := TMatice.Create(2*velikost + 1, 2*velikost + 1);
  StredX := velikost;
  StredY := velikost;
  for i := 0 to 2*velikost do
    for j := 0 to 2*velikost do begin
      if Vzdalenost(StredX, StredY, j, i) <= velikost then begin
        MaticeJadra.elem[i, j] := Gauss(j - velikost, i - velikost, sigma);
        StringGrid1.Cells[i, j] := FloatToStrF(MaticeJadra.elem[i, j], ffGeneral, 4, 2);
      end;
    end;
end;

procedure TKonvKernellForm.Button2Click(Sender: TObject);
var
  i, j: Integer;
begin
  velikost := StrToIntDef(Edit2.Text, 3);
  Edit2.Text := IntToStr(velikost);
  StringGrid1.ColCount := (2*velikost + 1);
  StringGrid1.RowCount := StringGrid1.ColCount;
  for i := 0 to 2*velikost do
    for j := 0 to 2*velikost do
      StringGrid1.Cells[i, j] := IntToStr(0);
  StringGrid1.Cells[velikost, velikost] := IntToStr(1);
end;

procedure TKonvKernellForm.FormCreate(Sender: TObject);
var
  i, j: Integer;
begin
  velikost := 3;
  sigma := 5;
  StringGrid1.ColCount := (2*velikost + 1);
  StringGrid1.RowCount := StringGrid1.ColCount;
  for i := 0 to 2*velikost do
    for j := 0 to 2*velikost do
      StringGrid1.Cells[i, j] := IntToStr(0);
  StringGrid1.Cells[velikost, velikost] := IntToStr(1);
  Edit2.Text := IntToStr(velikost);
  Edit1.Text := FloatToStr(sigma);
end;


end.
