Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, rxParsing, Rxtconst;
- type
- TForm1 = class(TForm)
- Edit: TEdit;
- Button: TButton;
- NameGraph: TLabel;
- Graph: TImage;
- xFrom: TEdit;
- xTo: TEdit;
- LabelFrom: TLabel;
- LabelTo: TLabel;
- IntegrateLabel: TLabel;
- procedure OnClick(Sender: TObject);
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- function integrate(formula: String; xFrom: Integer; xTo: Integer; count: Integer): Extended;
- var
- i: Integer;
- Res: Extended; // Результат
- x: Integer;
- newFormula: String;
- begin
- Res := 0;
- Randomize();
- // Собственно вычисляем интеграл Монте Карло
- for i := 1 to count do begin
- //x := Random(2 * (xTo - xFrom)) - (xFrom - xTo);
- x := Random(xTo - xFrom + 1) + xFrom;
- newFormula := stringreplace(formula, 'x', IntToStr(x), [rfReplaceAll, rfIgnoreCase]);
- Res := Res + rxParsing.GetFormulaValue(newFormula);
- end;
- Res := Res / count;
- // Возвращяем результат
- Exit(Res);
- end;
- function drowLines(Graph : TImage): Integer;
- begin
- // Ось Y
- Graph.Canvas.Pen.Color := RGB(255, 255, 255);
- Graph.Canvas.LineTo(Round(Graph.Width / 2), 0);
- Graph.Canvas.Pen.Color := RGB(255, 60, 255);
- Graph.Canvas.LineTo(Round(Graph.Width / 2), Graph.Height);
- // Ось X
- Graph.Canvas.Pen.Color := RGB(255, 255, 255);
- Graph.Canvas.LineTo(0, Round(Graph.Height / 2));
- Graph.Canvas.Pen.Color := RGB(255, 60, 255);
- Graph.Canvas.LineTo(Graph.Width + 100, Round(Graph.Height / 2));
- Exit(0);
- end;
- procedure TForm1.OnClick(Sender: TObject);
- var
- x: Integer;
- formula: String;
- newFormula: String;
- y: Extended;
- integral: Extended;
- begin
- // Стираем наше поле
- Graph.Canvas.FillRect(Graph.Canvas.ClipRect);
- // Чертим линии oX oY
- drowLines(Graph);
- // Основная формула, в которой есть 'x'
- // Позже мы будем заменять 'x' на 'x + значение функции'
- formula := Edit.Text;
- // Установка ширины рисования
- Graph.Canvas.Pen.Width := 3;
- // Устанавливаем точку, от кторой будем чертить в начало нашей функции
- // Нужно, чтобы не было линии из 0:0 к началу черчения
- Graph.Canvas.Pen.Color := RGB(255, 255, 255);
- Graph.Canvas.LineTo(999, 999);
- x := Round(Graph.Width / 2) + StrToInt(xFrom.Text);
- newFormula := stringreplace(formula, 'x', IntToStr(x - Round(Graph.Width / 2)), [rfReplaceAll, rfIgnoreCase]);
- y := Round(Graph.Height / 2) - rxParsing.GetFormulaValue(newFormula);
- Graph.Canvas.LineTo(x, Round(y));
- Graph.Canvas.Pen.Color := RGB(60, 255, 60);
- // Чертим наш график :)
- for x := Round(Graph.Width / 2) + StrToInt(xFrom.Text) to Round(Graph.Width / 2) + StrToInt(xTo.Text) do
- begin
- newFormula := stringreplace(formula, 'x', IntToStr(x - Round(Graph.Width / 2)), [rfReplaceAll, rfIgnoreCase]);
- y := Round(Graph.Height / 2) - rxParsing.GetFormulaValue(newFormula);
- Graph.Canvas.LineTo(x, Round(y));
- end;
- // Вызываем функцию для интегрирования :)
- Integral := integrate(formula, StrToInt(xFrom.Text), StrToInt(xTo.Text), 10000);
- // Изменяем текст
- IntegrateLabel.Caption := 'Интеграл: ' + FloatToStr(Integral);
- end;
- end.
Advertisement