2023년 7월 12일 수요일

FireMonkey TLine Demo

This video is created by connecting two planets orbiting the sun with different orbital periods with a line.

You can visually see the distance between two destination lines by the length of the line.



The length between the two points was calculated in the following way.

procedure TMForm.Draw_Line2P( x1,y1, x2,y2 : single;  setColor : cardinal );

var

  d, xtemp, ytemp, rAngle : single;

  drawLine : TLine;

begin

  if x1 > x2 then

  begin

    xtemp := x1;     ytemp := y1;

    x1 := x2;          y1 := y2;

    x2 := xtemp;     y2 := ytemp;

  end;


  d := SQRT( Power( x2-x1, 2 ) + Power( y2-y1, 2 ) );   // Uses System.Math

  rAngle := RadToDeg( ArcSin( (y2-y1)/d ));


  drawLine := TLine.Create( BLayout );

  drawLine.Parent := BLayout;

  drawLine.LineLocation := TLineLocation.Inner;

  drawLine.LineType := TLineType.Bottom;

  drawLine.RotationCenter.X := 0;

  drawLine.RotationCenter.Y := 0;

  drawLine.Stroke.Thickness := 1;

  drawLine.Stroke.Color := setColor;

  drawLine.Height := 1;

  drawLine.Width  := d;

  drawLine.Position.X := x1;

  drawLine.Position.Y := y1;

  drawLine.RotationAngle := rAngle;

end;

2023년 5월 3일 수요일

2023년 3월 13일 월요일

Using ChatGPT in Delphi

// ChatGPT Personal Key : https://beta.openai.com/account/api-keys





unit MainCGPT;

interface

uses

  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.JSON,

  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types, FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox,

  FMX.Memo, System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent, FMX.Layouts;

type

  TMForm = class(TForm)

    Memo_Ans: TMemo;

    Memo_HanQ: TMemo;

    BT_Question: TButton;

    NetHTTPClient1: TNetHTTPClient;

    NetHTTPRequest1: TNetHTTPRequest;

    Label1: TLabel;

    Label3: TLabel;

    Layout1: TLayout;

    Layout3: TLayout;

    SpeedButton1: TSpeedButton;

    procedure BT_QuestionClick(Sender: TObject);

    procedure NetHTTPClient1RequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse);

    procedure Memo_HanQDblClick(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  MForm: TMForm;


  // ChatGPT key :  https://beta.openai.com/account/api-keys

  Const MyGPTKey = 'mykey_1234abcd';   // Input your key


implementation

{$R *.fmx}

procedure TMForm.Memo_HanQDblClick(Sender: TObject);

begin

  Memo_HanQ.Lines.Clear;

end;


// Question *********************************************

procedure TMForm.BT_QuestionClick(Sender: TObject);

var

  LPostdata: string;

  LPostDataStream: TStringStream;

begin

  LPostData := '{' +

    '"model": "text-davinci-003",'+

    '"prompt": "' + Memo_HanQ.Text + '",'+

    '"max_tokens": 2048,'+

    '"temperature": 0'+

    '}';


  LPostDataStream := TStringStream.Create( LPostData, TEncoding.UTF8);


  NetHTTPClient1.CustomHeaders['Authorization'] := 'Bearer ' + MyGPTKey;

  NetHTTPClient1.CustomHeaders['Content-Type'] := 'application/json';


  LPostDataStream.Position := 0;

  NetHTTPClient1.Post('https://api.openai.com/v1/completions', LPostDataStream );

end;



// Answer ********************************************************************************

procedure TMForm.NetHTTPClient1RequestCompleted(const Sender: TObject; const AResponse: IHTTPResponse);

var

  LString, ansStr : string;

  LJson: TJsonObject;


begin

  if AResponse.StatusCode = 200 then

  begin

     LString := AResponse.ContentAsString;

     LJson := TJSONObject.ParseJSONValue(LString) as TJSONObject;

     try

       ansStr := LJson.GetValue('choices').A[0].FindValue('text').Value;

     finally

       LJson.Free;

     end;

  end

  else

    ansStr := 'HTTP response code: ' + AResponse.StatusCode.ToString;


  Memo_Ans.Lines.Clear;

  Memo_Ans.Lines.Add( ansStr );

end;

end.