DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

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;

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;

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;

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;

View the whole database.

Go to the DelphiDabbler website.