2016년 11월 2일 수요일

Draw Line any direction with TPath

Draw Line any direction with TPath

TLine object of firemonkey can draw a line to one direction.
If you want to draw line to any direction, you can use TPath.
TPath can be used drawing a curve normally.
But it is simple for line.



Sample Source

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects, FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Rectangle1: TRectangle;
    Button2: TButton;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    procedure Draw_Line(pBase: TControl; x1, y1, x2, y2,thikness:single; color:Cardinal );
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Draw_Line(pBase: TControl; x1, y1, x2, y2,thikness:single; color:Cardinal );
var
  LinePath : TPath;
  p1, p2 : TPointF;
begin
  LinePath := TPath.Create(pBase);
  LinePath.Parent := pBase;

  // p1 and p2 number is a only Direction
  // Drawing line of length is set by width and height of TPath.
  if ( x1 < x2 ) and ( y1 < y2 ) then
  begin
    p1.X := 0;     p1.Y := 0;        //  \
    p2.X := 100;   p2.Y := 100;

    LinePath.Position.X := x1;
    LinePath.Position.Y := y1;
    LinePath.Width  := x2 - x1;
    LinePath.Height := y2 - y1;
  end
  else if y1 = y2 then
  begin
    p1.X := 0;    p1.Y := 0;        //  -
    p2.X := 100;  p2.Y := 0;

    LinePath.Position.X := x1;
    LinePath.Position.Y := y1;
    LinePath.Width  := x2 - x1;
    LinePath.Height := 100;  // any value
  end
  else if x1 = x2 then
  begin
    p1.X := 0;   p1.Y := 0;        //  |
    p2.X := 0;   p2.Y := 100;

    LinePath.Position.X := x1;
    LinePath.Position.Y := y1;
    LinePath.Width  := 100;  // any value
    LinePath.Height := y2- y1
  end
  else if ( x1 < x2 ) and ( y1 > y2 ) then
  begin
    p1.X := 100;  p1.Y := 0;        //  /
    p2.X := 0;    p2.Y := 100;

    LinePath.Width  := x2 - x1;
    LinePath.Height := y1 - y2;

    LinePath.Position.X := x1;
    LinePath.Position.Y := y1 - LinePath.Height;
  end;

  LinePath.Stroke.Thickness := thikness;
  LinePath.Stroke.Color := color;
  LinePath.Data.MoveTo( p1 );
  LinePath.Data.LineTo( p2 );
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Draw_Line( Rectangle1, 0,0, 500,300, 2, $FFFF0000 );
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Draw_Line( Rectangle1, 0,0, 500,0, 4, $FFF0000FF);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Draw_Line( Rectangle1, 0,0, 0,300, 6, $FFFF00FF);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Draw_Line( Rectangle1, 0,300, 500,0, 4, $FF00FF00 );
end;



댓글 없음:

댓글 쓰기