首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

再學GDI+[49]: 路徑基礎屬性查詢 - GetPointCount、GetPathPoints、GetLastPoint、GetBounds

     編輯:Delphi

前言

路徑的基本屬性查詢是進行幾何分析、碰撞偵測、邊界計算的基礎。本文示範四個核心查詢方法:總點數、所有頂點、最後一點、外接矩形。

四大查詢方法對照

方法回傳值用途
GetPointCountInteger配置陣列大小、迴圈邊界
GetPathPoints(points, count)填充 TGPPoint 陣列幾何分析、序列化、編輯器
GetLastPoint(var pt: TGPPointF)單一 TGPPointF路徑延續、連接下一段
GetBounds(var rect: TGPRect)外接矩形 TGPRect碰撞偵測、視窗裁剪、佈局

CheckBox 三選項即時顯示範例

  • CheckBox1:提取所有路徑頂點(藍色方塊)
  • CheckBox2:提取最後一個點(紅色方塊)
  • CheckBox3:顯示外接矩形(紅色框)

核心實作

procedure TForm1.FormPaint(Sender: TObject);
var
  g: TGPGraphics;
  path: TGPGraphicsPath;
  p: TGPPen;
  b: TGPBrush;
  pts: array[0..6] of TGPPoint;  {原始 7 點貝塞爾控制點}
  ptf: TGPPointF;
  ppts: array of TGPPoint;
  rect: TGPRect;
  i: Integer;
begin
  {建立雙貝塞爾路徑}
  path := TGPGraphicsPath.Create;
  path.AddBeziers(PGPPoint(@pts), Length(pts));
  
  g := TGPGraphics.Create(Canvas.Handle);
  p := TGPPen.Create(aclRed);
  g.DrawPath(p, path);  {繪製原始曲線}
  
  {灰色顯示原始控制點}
  p.SetColor(aclGray);
  for i := 0 to Length(pts)-1 do
    g.DrawRectangle(p, MakeRect(pts[i].X-2, pts[i].Y-2, 4, 4));
  
  b := TGPSolidBrush.Create(aclBlue);
  {CheckBox1: 所有路徑頂點}
  if CheckBox1.Checked then begin
    SetLength(ppts, path.GetPointCount);
    path.GetPathPoints(PGPPoint(ppts), Length(pts));
    for i := 0 to Length(ppts)-1 do
      g.FillRectangle(b, MakeRect(ppts[i].X-3, ppts[i].Y-3, 6, 6));
  end;
  
  {CheckBox2: 最後一點}
  TGPSolidBrush(b).SetColor(aclRed);
  if CheckBox2.Checked then begin
    path.GetLastPoint(ptf);
    g.FillRectangle(b, MakeRect(ptf.X-3, ptf.Y-3, 6, 6));
  end;
  
  {CheckBox3: 外接矩形}
  if CheckBox3.Checked then begin
    path.GetBounds(rect);
    g.DrawRectangle(p, rect);
  end;
  
  b.Free; p.Free; path.Free; g.Free;
end;

執行效果預覽

路徑屬性查詢效果

關鍵觀察重點

  • 控制點 vs 路徑頂點:原始 7 個控制點 vs Flatten 後的大量路徑頂點
  • GetPathPoints 精度:回傳 Integer 座標,曲線經過平坦化處理
  • GetLastPoint 精度:回傳 Single (TGPPointF),比 Integer 更精確
  • GetBounds 實用性:快速碰撞預檢、自動縮放適配

表單定義檔

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 235
  ClientWidth = 302
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  OnCreate = FormCreate
  OnPaint = FormPaint
  PixelsPerInch = 96
  TextHeight = 13
  object CheckBox1: TCheckBox ... {三個 CheckBox}
end
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved