簡體   English   中英

如何在delphi中創建自定義繪制線

[英]how can I create a custom draw line in delphi

我想在我的 delphi 應用程序中的畫布上畫一條線,但需要它是一條 ****** 如何使用 * char 而不是破折號或點創建自定義線。

您可以使用Skia4Delphi庫來獲得該問題的通用解決方案。 下面是使用 TSkPaintBox 控件和 TSkPaintBox.OnDraw 事件的示例:

uses
  System.Math.Vectors, FMX.TextLayout, Skia, Skia.FMX;

procedure TForm1.SkPaintBox1Draw(ASender: TObject; const ACanvas: ISkCanvas;
  const ADest: TRectF; const AOpacity: Single);

  function GetTextPath(const AText: string): ISkPath;
  var
    LTextLayout: TTextLayout;
    LPathData: TPathData;
  begin
    LTextLayout := TTextLayoutManager.DefaultTextLayout.Create;
    try
      LTextLayout.BeginUpdate;
      try
        LTextLayout.Font.Size := 30;
        LTextLayout.Font.Style := [TFontStyle.fsBold];
        LTextLayout.Text := AText;
      finally
        LTextLayout.EndUpdate;
      end;
      LPathData := TPathData.Create;
      try
        LTextLayout.ConvertToPath(LPathData);
        Result := LPathData.ToSkPath;
      finally
        LPathData.Free;
      end;
    finally
      LTextLayout.Free;
    end;
  end;

var
  LPaint: ISkPaint;
  LTextPath: ISkPath;
  LPathBuilder: ISkPathBuilder;
begin
  LTextPath := GetTextPath('*');
  LPaint := TSkPaint.Create(TSkPaintStyle.Stroke);
  LPaint.AntiAlias := True;
  LPaint.Color := TAlphaColors.Black;
  LPaint.PathEffect := TSkPathEffect.Make1DPath(LTextPath, LTextPath.Bounds.Width + 2, 0, TSkPathEffect1DStyle.Rotate);
  LPathBuilder := TSkPathBuilder.Create;
  LPathBuilder.MoveTo(PointF(50, 100));
  LPathBuilder.LineTo(PointF(400, 290));
  ACanvas.DrawPath(LPathBuilder.Detach, LPaint);
end;

結果:

行輸出.png

此解決方案不僅限於星號和線條。 使用“@”和一個圓圈查看結果:

at-sign-output.png

一條線有一個方程,其形式為: Y = A * X + B

A是斜率, B是原點處的偏移量。

如果要從點(X1, Y1)到點(X2, Y2)畫一條線,首先要確定方程的AB常數:

A = (Y2 - Y1) / (X2 - X1)

有了 A 后,將 B 計算為:

B = Y1 - A * X1

現在您有了AB ,您可以使用它來計算X1X2之間的中間點。 一個簡單的循環就可以了。 X增加您希望*分隔的值。

注意:如果Y2 - Y1大於X2 - X1 ,則必須迭代Y而不是X

作為練習,我讓你編寫代碼......

我將使用線條的參數表示,參數是到目前為止繪制的線條長度。 這樣就可以畫出垂直線,並且可以實現星星的繪制等距,與線的斜率無關。

更准確地說:要從 A 點到 B 點畫一條線,計算線 L 的長度,然后計算線方向的單位向量 Dir。 在線上點 P 的公式為 P = A + t*Dir,其中 t 從 0 到 L。(這是偽代碼,可作為矢量符號讀取。)

這是一個執行此操作的簡單例程。

procedure DrawStarAt(P: TPointF; Radius: Single; aCanvas: TCanvas);
begin
  var
  r := RectF(P.X - Radius, P.Y - Radius, P.X + Radius, P.Y + Radius);
  aCanvas.FillText(r, '*', false, 1, [], TTextAlign.Center, TTextAlign.Center);
end;

procedure DrawStarLine(A, B: TPointF; aCanvas: TCanvas);
var
  // line length
  L,
  // line parameter
  t,
  // step for t
  dt,
  // Radius of the text rectangle
  Radius: Single;

  // Point to be drawn
  P,
  // unit vector for line direction
  Direction: TPointF;
  n: integer;
begin
  aCanvas.BeginScene;
  aCanvas.Fill.Color := TAlphaColorRec.Black;
  Radius := aCanvas.TextWidth('*');
  L := sqrt(sqr(B.X - A.X) + sqr(B.Y - A.Y));
  n:=trunc(L/Radius);
  //adjust dt so the last star is drawn exactly at B
  dt:=L/n;
  if L = 0 then
  begin
    DrawStarAt(A, Radius, aCanvas);
    aCanvas.EndScene;
    exit;
  end;
  Direction := PointF((B.X - A.X) / L, (B.Y - A.Y) / L);
  t := 0;
  while t < L do
  begin
    P := PointF(A.X + t * Direction.X, A.Y + t * Direction.Y);
    DrawStarAt(P, Radius, aCanvas);
    t := t + dt;
  end;
  DrawStarAt(B, Radius, aCanvas);
  aCanvas.EndScene;
end;

計算機科學家 Jack Bresenham 設計了一種算法,可以在整數網格上快速繪制直線。 該算法僅使用整數變量,不需要除法或乘法。
您可以直接在 Bresenham 代碼中編寫星號,但使用回調過程要簡潔得多:您可以使用回調函數作為額外參數調用 Bresenham 過程。 每次算法計算出線上的一個點時,它都會調用回調過程,傳遞 X 和 Y 坐標。
最大的優點是您可以編寫一個通用的 Bresenham 過程,並且只在回調中編寫不同的操作,具體取決於您是要繪制點還是星號。
它是這樣的。
您定義一個回調過程類型:

type
  TCallbackProc = procedure(X, Y: Integer) of Object;

寫出回調過程的動作。 這里我畫了一個像素。 你改變這條線來畫一個星號:

procedure TForm1.DrawPixel(X, Y: Integer);
begin
  Image1.Canvas.Pixels[X, Y] := clBlack;
end;

然后是 Bresenham 程序本身。 回想一下,該過程找到了線上的所有點,但不知道如何處理它們,因此它將它們傳遞給回調過程:

procedure TForm1.Bresenham(X1, Y1, X2, Y2: Integer; CallbackProc: TCallBackProc);
var
  Dx, Dy, Sx, Sy, Error, E2: Integer;
  Done: Boolean;
begin
  Dx := Abs(X2 - X1);
  if X1 < X2 then
    Sx := 1
  else
    Sx := -1;
  Dy := -Abs(Y2 - Y1);
  if Y1 < Y2 then
    Sy := 1
  else
    Sy := -1;
  Error := Dx + Dy;

  while True do
  begin
    if Assigned(CallbackProc) then
      CallbackProc(X1, Y1);
    if (X1 = X2) and (Y1 = Y2) then
      Exit;
    E2 := 2 * Error;
    if E2 >= Dy then
    begin
      if X1 = X2 then
      Exit;
      Error := Error + Dy;
      X1 := X1 + Sx;
    end;
    if E2 <= Dx then
    begin
      if Y1 = Y2 then
        Exit;
      Error := Error + Dx;
      Y1 := Y1 + Sy;
    end;
  end;
end;

用法:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Bresenham(100, 50, 250, 350, DrawPixel);
  Bresenham(100, 50, 250, 350, DrawDaisies);
  Bresenham(100, 50, 250, 350, DrawSquirrels);
end;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM