# DelphiDabbler Code Snippets Database

## Snippet Selection

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

## BoundsF

Constructs and returns a TRectF record with the given top-left coordinate, width and height.

```function BoundsF(ALeft, ATop, AWidth, AHeight: Double): TRectF;
begin
Result.Left := ALeft;
Result.Top := ATop;
Result.Right := ALeft + AWidth;
Result.Bottom :=  ATop + AHeight;
end;```

## PointF

Constructs and returns a TPointF record with the given x & y coordinates.

```function PointF(const AX, AY: Double): TPointF;
begin
Result.X := AX;
Result.Y := AY;
end;```

## Range

Constructs and returns a TRange record with bounds A and B. The smaller of A and B is used as the lower bound and the larger as the upper bound. If both values are equal then the range will be empty.

```function Range(const A, B: Integer): TRange;
begin
if A <= B then
begin
Result.Lower := A;
Result.Upper := B;
end
else
begin
Result.Lower := B;
Result.Upper := A;
end;
end;```

## RectF

Constructs and returns a TRectF record with the given left, top, right & bottom coordinates.

```function RectF(const ALeft, ATop, ARight, ABottom: Double): TRectF;
begin
Result.Left := ALeft;
Result.Top := ATop;
Result.Right := ARight;
Result.Bottom := ABottom;
end;```

## Size

Constructs and returns a TSize record with the given dimensions.

```function Size(const ACX, ACY: Integer): Types.TSize;
begin
Result.cx := ACX;
Result.cy := ACY;
end;```

## TPointF

Encapsulates a point with double precision floating point coordinates.

```type
TPointF = record
X, Y: Double;  // x and y coordinates
end;```

## TRange

Encapsulates the upper and lower bounds of a range of values.

```type
TRange = record
Lower: Integer;  // lower bound of range
Upper: Integer;  // upper bound of range
end;```

## TRangeEx

Encapsulates a range of integers with a methods to test whether a value falls within the range and to adjust the value to fit.

```type
TRangeEx = record
// Minimum and maximum bounds of range.
Min, Max: Integer;
// Constructs record with given minimum and maximum bounds.
constructor Create(AMin, AMax: Integer);
// Checks if the given value is contained within the range.
function Contains(const Value: Integer): Boolean;
// Adjusts the given value to lie within the range, and returns it. If the
// value is less than Min, Min is returned. If the value is greater than Max
// then max is returned. If the value is in the range it is returned
// unchanged.
function Constrain(const Value: Integer): Integer;
end;

function TRangeEx.Constrain(const Value: Integer): Integer;
begin
if Value < Min then
Result := Min
else if Value > Max then
Result := Max
else
Result := Value;
end;

function TRangeEx.Contains(const Value: Integer): Boolean;
begin
Result := Math.InRange(Value, Min, Max);
end;

constructor TRangeEx.Create(AMin, AMax: Integer);
begin
Min := AMin;
Max := AMax;
end;```

## TRectF

Encapsulates a rectangle with double precision floating point size and position.

```type
TRectF = record
case Integer of
0: (Left, Top, Right, Bottom: Double);
1: (TopLeft, BottomRight: TPointF);
end;```

## TSizeEx

Record that represents the size, i.e. the width and height, of something. This is an extended version of the TSize record that features equality and inequality operator overloading, a test for zero size and a constructor. TSizeEx is assignment compatible and comparible with the Delphi RTL's TSize record.

```type
TSizeEx = record
public
// Width
CX: Integer;
// Height
CY: Integer;
// Constructs record with two given CX and CY field values
constructor Create(ACX, ACY: Integer);
// Enables TSize to be assigned to and compared with TSizeEx
class operator Implicit(S: Types.TSize): TSizeEx;
// Enables TSizeEx to be assigned to and compared with TSize
class operator Implicit(S: TSizeEx): Types.TSize;
// Tests for equality of TSizeEx records. Also works if one record is TSize.
class operator Equal(S1, S2: TSizeEx): Boolean;
// Tests for inequality of TSizeEx records. Also works if one record is
// TSize.
class operator NotEqual(S1, S2: TSizeEx): Boolean;
// Tests if a TSizeEx instance is zero (i.e. one of fields is zero)
function IsZero: Boolean;
end;

constructor TSizeEx.Create(ACX, ACY: Integer);
begin
CX := ACX;
CY := ACY;
end;

class operator TSizeEx.Equal(S1, S2: TSizeEx): Boolean;
begin
// zero records are special: can be zero when only one of CX or CY is zero
if S1.IsZero and S2.IsZero then
begin
Result := True;
Exit;
end;
Result := (S1.CX = S1.CX) and (S1.CY = S2.CY);
end;

class operator TSizeEx.Implicit(S: Types.TSize): TSizeEx;
begin
Result.CX := S.cx;
Result.CY := S.cy;
end;

class operator TSizeEx.Implicit(S: TSizeEx): Types.TSize;
begin
Result.cx := S.CX;
Result.cy := S.CY;
end;

function TSizeEx.IsZero: Boolean;
begin
Result := (CX = 0) or (CY = 0);
end;

class operator TSizeEx.NotEqual(S1, S2: TSizeEx): Boolean;
begin
Result := not (S1 = S2);
end;```

View the whole database.

Go to the DelphiDabbler website.