From b48c67f49fcc3106fb6bf319e01642a92862ef16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Van=20Canneyt?= Date: Sun, 9 Jul 2023 14:39:18 +0200 Subject: [PATCH] * Fix epsilon, add TRectF.FitInto --- rtl/objpas/types.pp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/rtl/objpas/types.pp b/rtl/objpas/types.pp index c5afb20b35..be6aaa4697 100644 --- a/rtl/objpas/types.pp +++ b/rtl/objpas/types.pp @@ -33,7 +33,7 @@ const RT_RCDATA = WinTypes.RT_RCDATA deprecated 'Use WinTypes.RT_RCDATA instead'; {$endif} -Type +Const Epsilon: Single = 1E-40; Epsilon2: Single = 1E-30; @@ -246,7 +246,8 @@ type procedure Inflate(DX, DY: Single); procedure Inflate(DL, DT, DR, DB: Single); function CenterPoint: TPointF; - + function FitInto(const Dest: TRectF; out Ratio: Single): TRectF; overload; + function FitInto(const Dest: TRectF): TRectF; overload; procedure Union (const r: TRectF); inline; procedure Offset (const dx,dy : Single); inline; procedure Offset (DP: TPointF); inline; @@ -1101,6 +1102,29 @@ begin Result.Y := (Bottom-Top) / 2 + Top; end; +function TRectF.FitInto(const Dest: TRectF; out Ratio: Single): TRectF; +begin + if (Dest.Width<=0) or (Dest.Height<=0) then + begin + Ratio:=1.0; + exit(Self); + end; + Ratio:=Max(Self.Width / Dest.Width, Self.Height / Dest.Height); + if Ratio=0 then + exit(Self); + Result.Width:=Self.Width / Ratio; + Result.Height:=Self.Height / Ratio; + Result.Left:=Self.Left + (Self.Width - Result.Width) / 2; + Result.Top:=Self.Top + (Self.Height - Result.Height) / 2; +end; + +function TRectF.FitInto(const Dest: TRectF): TRectF; +var + Ratio: Single; +begin + Result:=FitInto(Dest,Ratio); +end; + function TRectF.Contains(Pt: TPointF): Boolean; begin Result := (Left <= Pt.X) and (Pt.X < Right) and (Top <= Pt.Y) and (Pt.Y < Bottom);