DelphiDabbler Code Snippets Database

"Mathematics" Category Contents

The following snippets belong to the Mathematics category.

AllDigitsDifferent

Checks if all the digits of the given integer are different to each other.

function AllDigitsDifferent(N: Int64): Boolean;
var
  UsedDigits: array[0..9] of Boolean; // records which digits have been used
  I: 0..9;  // loops through elements of UsedDigits
  M: 0..9;  // modulus after dividing by 10
begin
  N := Abs(N);
  Result := False;
  for I := 0 to 9 do
    UsedDigits[I] := False;
  while N > 0 do
  begin
    M := N mod 10;
    if UsedDigits[M] then
      Exit;
    UsedDigits[M] := True;
    N := N div 10;
  end;
  Result := True; // if we get here all digits are unique
end;

AllDigitsSame

Checks if all the digits of the given integer are the same.

function AllDigitsSame(N: Int64): Boolean;
var
  D: 0..9;  // sample digit from N
begin
  N := Abs(N);
  D := N mod 10;
  Result := False;
  while N > 0 do
  begin
    if N mod 10 <> D then
      Exit;
    N := N div 10;
  end;
  Result := True;
end;

DecimalToFraction

Converts the given decimal to a fraction. The numerator and denominator are passed out as floating point numbers in FractionNumerator and FractionDenominator respectively. AccuracyFactor determines how accurate the conversion is to be.

procedure DecimalToFraction(Decimal: Extended; out FractionNumerator: Extended;
  out FractionDenominator: Extended; const AccuracyFactor: Extended);
var
  DecimalSign: Extended;
  Z: Extended;
  PreviousDenominator: Extended;
  ScratchValue: Extended;
{$IFDEF FPC}
const
{$ELSE}
resourcestring
{$ENDIF}
  sTooSmall = 'Decimal too small to convert to fraction';
  sTooLarge = 'Decimal too large to convert to fraction';
const
  LargestDecimal: Extended = 1.0E+19;
  SmallestDecimal: Extended = 1.0E-19;
begin
  if Decimal < 0.0 then
    DecimalSign := -1.0
  else
    DecimalSign := 1.0;
  Decimal := Abs(Decimal);
  if Math.SameValue(Decimal, Int(Decimal)) then
  begin
    FractionNumerator := Decimal * DecimalSign;
    FractionDenominator := 1.0;
    Exit;
  end;
  if (Decimal < SmallestDecimal) then // X = 0 already taken care of
    raise SysUtils.EConvertError.Create(sTooSmall);
  if (Decimal > LargestDecimal) then
    raise SysUtils.EConvertError.Create(sTooLarge);
  Z := Decimal;
  PreviousDenominator := 0.0;
  FractionDenominator := 1.0;
  repeat
    Z := 1.0 / (Z - Int(Z));
    ScratchValue := FractionDenominator;
    FractionDenominator := FractionDenominator * Int(Z) + PreviousDenominator;
    PreviousDenominator := ScratchValue;
    FractionNumerator := Int(Decimal * FractionDenominator + 0.5) // Rounding
  until
    (
      Abs(
        Decimal - (FractionNumerator / FractionDenominator)
      ) < AccuracyFactor
    )
    or (Z = Int(Z));
  FractionNumerator := DecimalSign * FractionNumerator;
end;

DigitCount

Counts the number of digits in the given integer.

function DigitCount(AInteger: Int64): Integer;
begin
  if AInteger <> 0 then
  begin
    Result := 0;
    AInteger := Abs(AInteger);
    while AInteger > 0 do
    begin
      AInteger := AInteger div 10;
      Inc(Result);
    end;
  end
  else
    Result := 1;
end;

DigitCount2

Counts the number of digits in the given integer.

function DigitCount2(const AValue: Int64): Integer;
begin
  if AValue <> 0 then
    Result := Math.Floor(Math.Log10(Abs(AValue))) + 1
  else
    Result := 1;
end;

DigitCountR

Counts the number of digits in the given integer using recursion.

function DigitCountR(AValue: Int64): Integer;
begin
  if AValue mod 10 = AValue then
    Result := 1
  else
    Result := 1 + DigitCountR(AValue div 10)
end;

DigitSum

Returns the sum of the digits from the given integer, using recursion.

function DigitSum(AValue: Int64): Integer;
begin
  if AValue mod 10 = AValue then
    Result := AValue
  else
    Result := (AValue mod 10) + DigitSum(AValue div 10)
end;

DistanceBetweenPoints (TPointF overload)

Calculates the distance between two given points with double precision floating point valued coordinates.

function DistanceBetweenPoints(const P1, P2: TPointF): Extended; overload;
begin
  Result := Math.Hypot(P1.X - P2.X, P1.Y - P2.Y);
end;

DistanceBetweenPoints (TPoint overload)

Calculates the distance between two points with integer valued co-ordinates.

function DistanceBetweenPoints(const P1, P2: Types.TPoint): Extended; overload;
begin
  Result := Math.Hypot(P1.X - P2.X, P1.Y - P2.Y);
end;

Factorial

Calculates the factorial of the given number.

function Factorial(N: Byte): Int64;
var
  K: Integer; // loop control
begin
  Result := 1;
  if (N = 0) or (N = 1) then
    Exit;
  for K := 2 to N do
    Result := Result * K;
end;

GCD

Determines the greatest common divisor of two given non-zero integers.

function GCD(A, B: Integer): Integer;
var
  Temp: Integer; // used in swapping A & B
begin
  while B <> 0 do
  begin
    Temp := B;
    B := A mod Temp;
    A := Temp;
  end;
  Result := Abs(A);
end;

GCD2

Determines the greatest common divisor of two given non-zero integers.

function GCD2(const A, B: Integer): Integer;
begin
  if B = 0 then
    Result := Abs(A)
  else
    Result := GCD2(B, A mod B);
end;

IsPrime

Checks if the given number is prime.

function IsPrime(N: Integer): Boolean;
var
  Max: Integer;     // max divisor
  Divisor: Integer; // different divisors to try
begin
  Result := False;
  if N < 2 then
    Exit; // not a prime
  Result := True;
  if N = 2 then
    Exit; // 2 is prime
  if N mod 2 = 0 then
    Result := False; // even numbers > 2 are not prime
  Max := Trunc(Sqrt(N)) + 1;
  Divisor := 3;
  // test odd numbers
  while (Max > Divisor) and Result do
  begin
    if (N mod Divisor) = 0 then
      Result := False;
    Inc(Divisor, 2); // next odd number
  end;
end;

IsPrime2

Checks if the given number is prime.

function IsPrime2(Val: Integer): Boolean;
var
  X: Integer; // index
begin
  Result := (Val > 1);
  if Result then
  begin
    for X := (Val div 2) downto 2 do
    begin
      Result := Result and ((Val mod X) <> 0);
      if not Result then
    	Break;
    end;
  end;
end;

IsRectNormal

Checks if the given rectangle is normalized, i.e. Left <= Right and Top <= Bottom.

function IsRectNormal(const R: Windows.TRect): Boolean;
begin
  Result := (R.Left <= R.Right) and (R.Top <= R.Bottom);
end;

LCD

Returns the least common divisor of two given non-zero integers.

function LCD(A, B: Integer): Integer;
begin
  Result := Abs((A * B)) div GCD(A, B);
end;

MaxOfArray (Double overload)

Returns the maximum value contained in the given array of double precision floating point values. The array must not be empty.

function MaxOfArray(const A: array of Double): Double; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] > Result then
      Result := A[Idx];
end;

MaxOfArray (Extended overload)

Returns the maximum value contained in the given array of extended precision floating point values. The array must not be empty.

function MaxOfArray(const A: array of Extended): Extended; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] > Result then
      Result := A[Idx];
end;

MaxOfArray (Int64 overload)

Returns the maximum value contained in the given array of 64 bit integer values. The array must not be empty.

function MaxOfArray(const A: array of Int64): Int64; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] > Result then
      Result := A[Idx];
end;

MaxOfArray (Integer overload)

Returns the maximum value contained in the given array of integer values. The array must not be empty.

function MaxOfArray(const A: array of Integer): Integer; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] > Result then
      Result := A[Idx];
end;

MaxOfArray (Single overload)

Returns the maximum value contained in the given array of single precision floating point values. The array must not be empty.

function MaxOfArray(const A: array of Single): Single; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] > Result then
      Result := A[Idx];
end;

Mid (Double overload)

Returns the middle of three double precision floating point values.

function Mid(const A, B, C: Double): Double; overload;
begin
  Result := Math.Min(
    Math.Min(
      Math.Max(A, B), Math.Max(B, C)
    ),
    Math.Max(A, C)
  );
end;

Mid (Extended overload)

Returns the middle of three extended precision floating point values.

function Mid(const A, B, C: Extended): Extended; overload;
begin
  Result := Math.Min(
    Math.Min(
      Math.Max(A, B), Math.Max(B, C)
    ),
    Math.Max(A, C)
  );
end;

Mid (Int64 overload)

Returns the middle of three 64 bit integer values.

function Mid(const A, B, C: Int64): Int64; overload;
begin
  Result := Math.Min(
    Math.Min(
      Math.Max(A, B), Math.Max(B, C)
    ),
    Math.Max(A, C)
  );
end;

Mid (Integer overload)

Returns the middle of three integer values.

function Mid(const A, B, C: Integer): Integer; overload;
begin
  Result := Math.Min(
    Math.Min(
      Math.Max(A, B), Math.Max(B, C)
    ),
    Math.Max(A, C)
  );
end;

Mid (Single overload)

Returns the middle of three single precision floating point values.

function Mid(const A, B, C: Single): Single; overload;
begin
  Result := Math.Min(
    Math.Min(
      Math.Max(A, B), Math.Max(B, C)
    ),
    Math.Max(A, C)
  );
end;

MinMidMax (Double overload)

Finds the minimum, middle and maximum values of three double precision floating point numbers, A, B and C and returns them in Min, Mid and Max respectively.

procedure MinMidMax(const A, B, C: Double; out Min, Mid, Max: Double); overload;
var
  Temp: Double;
begin
  if A > B then
  begin
    Max := A;
    Min := B;
  end
  else
  begin
    Max := B;
    Min := A;
  end;
  if C > Max then
  begin
    Mid := Max;
    Max := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end
  else
  begin
    Mid := Min;
    Min := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end;
end;

MinMidMax (Extended overload)

Finds the minimum, middle and maximum values of three extended precision floating point numbers, A, B and C and returns them in Min, Mid and Max respectively.

procedure MinMidMax(const A, B, C: Extended; out Min, Mid, Max: Extended); 
  overload;
var
  Temp: Extended;
begin
  if A > B then
  begin
    Max := A;
    Min := B;
  end
  else
  begin
    Max := B;
    Min := A;
  end;
  if C > Max then
  begin
    Mid := Max;
    Max := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end
  else
  begin
    Mid := Min;
    Min := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end;
end;

MinMidMax (Int64 overload)

Finds the minimum, middle and maximum values of three 64 bit integers, A, B and C and returns them in Min, Mid and Max respectively.

procedure MinMidMax(const A, B, C: Int64; out Min, Mid, Max: Int64); overload;
var
  Temp: Int64;
begin
  if A > B then
  begin
    Max := A;
    Min := B;
  end
  else
  begin
    Max := B;
    Min := A;
  end;
  if C > Max then
  begin
    Mid := Max;
    Max := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end
  else
  begin
    Mid := Min;
    Min := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end;
end;

MinMidMax (Integer overload)

Finds the minimum, middle and maximum values of three integers, A, B and C and returns them in Min, Mid and Max respectively.

procedure MinMidMax(const A, B, C: Integer; out Min, Mid, Max: Integer);
  overload;
var
  Temp: Integer;
begin
  if A > B then
  begin
    Max := A;
    Min := B;
  end
  else
  begin
    Max := B;
    Min := A;
  end;
  if C > Max then
  begin
    Mid := Max;
    Max := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end
  else
  begin
    Mid := Min;
    Min := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end;
end;

MinMidMax (Single overload)

Finds the minimum, middle and maximum values of three single precision floating point numbers, A, B and C and returns them in Min, Mid and Max respectively.

procedure MinMidMax(const A, B, C: Single; out Min, Mid, Max: Single); overload;
var
  Temp: Single;
begin
  if A > B then
  begin
    Max := A;
    Min := B;
  end
  else
  begin
    Max := B;
    Min := A;
  end;
  if C > Max then
  begin
    Mid := Max;
    Max := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end
  else
  begin
    Mid := Min;
    Min := C;
    if Mid < Min then
    begin
      Temp := Mid;
      Mid := Min;
      Min := Temp;
    end;
  end;
end;

MinOfArray (Double overload)

Returns the minimum value contained in the given array of double precision floating point values. The array must not be empty.

function MinOfArray(const A: array of Double): Double; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] < Result then
      Result := A[Idx];
end;

MinOfArray (Extended overload)

Returns the minimum value contained in the given array of extended precision floating point values. The array must not be empty.

function MinOfArray(const A: array of Extended): Extended; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] < Result then
      Result := A[Idx];
end;

MinOfArray (Int64 overload)

Returns the minimum value contained in the given array of 64 bit integer values. The array must not be empty.

function MinOfArray(const A: array of Int64): Int64; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] < Result then
      Result := A[Idx];
end;

MinOfArray (Integer overload)

Returns the minimum value contained in the given array of integer values. The array must not be empty.

function MinOfArray(const A: array of Integer): Integer; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] < Result then
      Result := A[Idx];
end;

MinOfArray (Single overload)

Returns the minimum value contained in the given array of single precision floating point values. The array must not be empty.

function MinOfArray(const A: array of Single): Single; overload;
var
  Idx: Integer;
begin
  Assert(Length(A) > 0);
  Result := A[Low(A)];
  for Idx := Succ(Low(A)) to High(A) do
    if A[Idx] < Result then
      Result := A[Idx];
end;

MoveRectToOrigin

Translates the give rectangle to the origin. The top and left co-ordinates are set to zero and the bottom and right co-ordinates are adjusted accordingly.

function MoveRectToOrigin(const R: Types.TRect): Types.TRect;
begin
  Result := R;
  Types.OffsetRect(Result, -R.Left, -R.Top);
end;

NormaliseFractionSign

Normalises the sign of the fraction with numerator Num and denominator Denom so that Num takes the sign of the fraction and Denom is non-negative.

procedure NormaliseFractionSign(var Num, Denom: Integer);
begin
  if Denom < 0 then
  begin
    Num := -Num;
    Denom := -Denom;
  end;
end;

NormalizeRect

Normalises the given rectangle so that Left <= Right and Top <= Bottom.

function NormalizeRect(const R: Windows.TRect): Windows.TRect;
begin
  Result := R;
  if Result.Left > Result.Right then
    ExchangeInt(Result.Left, Result.Right);
  if Result.Top > Result.Bottom then
    ExchangeInt(Result.Top, Result.Bottom);
end;

Pow

Raises integer value Base to non-negative integer power Exponent and returns the result.

function Pow(const Base: Int64; const Exponent: Byte): Int64;
var
  I: Byte;
begin
  Result := 1;
  for I := 1 to Exponent do
    Result := Result * Base;
end;

RectArea

Returns the area of the given rectangle.

function RectArea(const R: Windows.TRect): Int64;
begin
  Result := Abs((R.Right - R.Left) * (R.Bottom - R.Top));
end;

RectHeight

Returns the height of the given rectangle. The return value is always non-negative even if the rectangle is not normalized.

function RectHeight(const R: Windows.TRect): Integer;
begin
  Result := R.Bottom - R.Top;
  if Result < 0 then
    Result := -Result;
end;

RectSize

Returns the size of the given rectangle. The returned dimensions are always non-negative even if the rectangle is not normalized.

function RectSize(const R: Windows.TRect): Windows.TSize;
begin
  Result.cx := RectWidth(R);
  Result.cy := RectHeight(R);
end;

RectWidth

Returns the width of the given rectangle. The return value is always non-negative even if the rectangle is not normalized.

function RectWidth(const R: Windows.TRect): Integer;
begin
  Result := R.Right - R.Left;
  if Result < 0 then
    Result := -Result;
end;

RepeatedDigits

Checks if the given integer contains only a single repeated digit.

function RepeatedDigits(N: Int64): Boolean;
begin
  N := Abs(N);
  if N > 0 then
    Result :=
      N = SysUtils.StrToInt64(
        StringOfChar(
          Char(48 + N mod 10),
          Succ(Math.Floor(Math.Log10(N)))
        )
      )
  else
    Result := True;
end;

ResizeRect (TSize overload)

Resizes rectangle R to size NewSize, leaving the top-left position unchanged. Returns the resized rectangle.

function ResizeRect(const R: Types.TRect; const NewSize: Types.TSize):
  Types.TRect; overload;
begin
  Result := ResizeRect(R, NewSize.CX, NewSize.CY);
end;

ResizeRect (Longint overload)

Resizes rectangle R to the width and height given by NewWidth and NewHeight, leaving the top-left position unchanged. Returns the resized rectangle.

function ResizeRect(const R: Types.TRect; const NewWidth, NewHeight: LongInt):
  Types.TRect; overload;
begin
  Result := Types.Bounds(R.Left, R.Top, NewWidth, NewHeight);
end;

ReverseNumber

Reverses the digits of the given integer and returns the resulting value. The given integer should be positive: zero is always returned for negative integers.

function ReverseNumber(AValue: Int64): Int64;
begin
  Result := 0;
  while AValue > 0 do
  begin
    Result := (Result * 10) + (AValue mod 10);
    AValue := AValue div 10;
  end;
end;

ReverseNumberR

Reverses the digits of the given integer, which must be non-negative, and returns the resulting value. Uses recursion.

function ReverseNumberR(AValue: Int64): Int64;
begin
  Assert(AValue >= 0);
  if AValue mod 10 = AValue then
    Result := AValue
  else
    Result := ((AValue mod 10) * Trunc(IntPower(10, Trunc(Log10(AValue)))))
      + ReverseNumberR(AValue div 10)
end;

SAR

Performs an arithmetic right shift operation on the given value and returns the result. Value is shifted right by Shift bits. Shift must be in the range 0..31 and is adjusted if it is not.

function SAR(Value: LongInt; Shift: Byte): LongInt;
begin
  Shift := Shift and 31;
  if Shift = 0 then
  begin
    Result := Value;
    Exit;
  end;
  Result := LongInt(LongWord(Value) shr Shift);
  if Value < 0 then
    Result := LongInt(LongWord(Result) or ($FFFFFFFF shl (32 - Shift)));
end;

ScaleRect

Scales the given rectangle by the given scale factor and returns the scaled rectangle.

function ScaleRect(const ARect: Types.TRect; AScaling: Double): Types.TRect;
begin
  Result.Left := System.Round(ARect.Left * AScaling);
  Result.Top := System.Round(ARect.Top * AScaling);
  Result.Right := System.Round(ARect.Right * AScaling);
  Result.Bottom := System.Round(ARect.Bottom * AScaling);
end;

SignOfFloat

Returns the sign of the given floating point value. Returns -1 if the value is positive, 0 if zero or +1 if negative.

function SignOfFloat(const Value: Extended): Integer;
begin
  if Value < 0.0 then
    Result := -1
  else if Value = 0.0 then
    Result := 0
  else
    Result := 1;
end;

SignOfInt

Returns the sign of the given integer. Returns -1 if the integer is positive, 0 if zero or +1 if negative.

function SignOfInt(const Value: Int64): Integer;
begin
  if Value < 0 then
    Result := -1
  else if Value = 0 then
    Result := 0
  else
    Result := 1;
end;

SimplifyFraction

Simplifies the fraction with numerator Num and denominator Denom to its lowest terms. If the fraction is already in its lowest terms then Num and Denom are left unchanged.

procedure SimplifyFraction(var Num, Denom: Int64);
var
  CommonFactor: Int64;  // greatest common factor of Num and Denom
begin
  Assert(Denom <> 0);
  CommonFactor := Abs(GCD(Num, Denom));
  Num := Num div CommonFactor;
  Denom := Denom div CommonFactor;
end;

StretchRect (asymmetric overload)

Stretches rectangle R by the given scaling factors and returns the result. The rectangle's width is scaled by ScalingX and its height by ScalingY. The top left corner of the rectangle is not changed.

function StretchRect(const R: Types.TRect; const ScalingX, ScalingY: Double):
  Types.TRect; overload;
{$IFDEF FPC}
const
{$ELSE}
resourcestring
{$ENDIF}
  sErrorMsg = 'StretchRect(): Rectangle bottom or right out of bounds.';
var
  NewW, NewH: Double;  // stretched width and height of rectangle
begin
  NewW := (R.Right - R.Left) * ScalingX;
  NewH := (R.Bottom - R.Top) * ScalingY;
  if (Abs(NewW + R.Left) > High(LongInt))
    or (Abs(NewH + R.Top) > High(LongInt)) then
    raise SysUtils.EOverflow.Create(sErrorMsg);
  Result := Types.Bounds(R.Left, R.Top, System.Round(NewW), System.Round(NewH));
end;

StretchRect (symmetric overload)

Stretches the rectangle R by scale factor Scaling and returns the result. Both width and height are stretched by the same scale factor. The top left corner is not changed.

function StretchRect(const R: Types.TRect; const Scaling: Double):
  Types.TRect; overload;
begin
  Result := StretchRect(R, Scaling, Scaling);
end;

ZoomRatio (Integer overload)

Calculates and returns the largest scaling that can be applied to a rectangle of width SrcWidth and height SrcHeight to fit it, without changing the aspect ratio, within a second rectangle of width DestWidth and height DestHeight.

function ZoomRatio(const DestWidth, DestHeight, SrcWidth, SrcHeight: Integer):
  Double; overload;
begin
  Result := Math.Min(DestWidth / SrcWidth, DestHeight / SrcHeight);
end;

ZoomRatio (TSize overload)

Calculates and returns the largest scaling that can be applied to a rectangle of size SrcSize to fit it, without changing the aspect ratio, within a second rectangle of size DestSize.

function ZoomRatio(const DestSize, SrcSize: Types.TSize): Double; overload;
begin
  Result := ZoomRatio(DestSize.cx, DestSize.cy, SrcSize.cx, SrcSize.cy);
end;

ZoomRatio (TRect overload)

Calculates and returns the laregest scaling that can be applied to rectangle SrcRect to fit it, without changing the aspect ratio, within rectangle DestRect.

function ZoomRatio(const DestRect, SrcRect: Types.TRect): Double; overload;
begin
  Result := ZoomRatio(RectSize(DestRect), RectSize(SrcRect));
end;

View the whole database.

Go to the DelphiDabbler website.