17.4 TRect

The TRect object is declared as follows:

    TRect = OBJECT
       A, B: TPoint;
       FUNCTION Empty: Boolean;
       FUNCTION Equals (R: TRect): Boolean;
       FUNCTION Contains (P: TPoint): Boolean;
       PROCEDURE Copy (R: TRect);
       PROCEDURE Union (R: TRect);
       PROCEDURE Intersect (R: TRect);
       PROCEDURE Move (ADX, ADY: Sw_Integer);
       PROCEDURE Grow (ADX, ADY: Sw_Integer);
       PROCEDURE Assign (XA, YA, XB, YB: Sw_Integer);
    END;

TRect.Empty

Declaration:
Function TRect.Empty: Boolean;
Description:
Empty returns True if the rectangle defined by the corner points A, B has zero or negative surface.
Errors:
None.
See also:
TRect.Equals (556), TRect.Contains (556)

Listing: objectex/ex1.pp


Program ex1;

{ Program to demonstrate TRect.Empty }

Uses objects;


Var ARect,BRect : TRect;
    P : TPoint;

begin
  With ARect.A do
    begin
    X:=10;
    Y:=10;
    end;
  With ARect.B do
    begin
    X:=20;
    Y:=20;
    end;
  { Offset B by (5,5) }
  With BRect.A do
    begin
    X:=15;
    Y:=15;
    end;
  With BRect.B do
    begin
    X:=25;
    Y:=25;
    end;
  { Point }
  With P do
    begin
    X:=15;
    Y:=15;
    end;
  Writeln ('A empty : ',ARect.Empty);
  Writeln ('B empty : ',BRect.Empty);
  Writeln ('A Equals B : ',ARect.Equals(BRect));
  Writeln ('A Contains (15,15) : ',ARect.Contains(P));
end.

TRect.Equals

Declaration:
Function TRect.Equals (R: TRect): Boolean;
Description:
Equals returns True if the rectangle has the same corner points A,B as the rectangle R, and False otherwise.
Errors:
None.
See also:
Empty (555), Contains (556)

For an example, see TRect.Empty (555)

TRect.Contains

Declaration:
Function TRect.Contains (P: TPoint): Boolean;
Description:
Contains returns True if the point P is contained in the rectangle (including borders), False otherwise.
Errors:
None.
See also:
Intersect (558), Equals (556)

TRect.Copy

Declaration:
Procedure TRect.Copy (R: TRect);
Description:
Assigns the rectangle R to the object. After the call to Copy, the rectangle R has been copied to the object that invoked Copy.
Errors:
None.
See also:
Assign (560)

Listing: objectex/ex2.pp


Program ex2;

{ Program to demonstrate TRect.Copy }

Uses objects;

Var ARect,BRect,CRect : TRect;

begin
  ARect.Assign(10,10,20,20);
  BRect.Assign(15,15,25,25);
  CRect.Copy(ARect);
  If ARect.Equals(CRect) Then
    Writeln ('ARect equals CRect')
  Else
    Writeln ('ARect does not equal CRect !');
end.

TRect.Union

Declaration:
Procedure TRect.Union (R: TRect);
Description:
Union enlarges the current rectangle so that it becomes the union of the current rectangle with the rectangle R.
Errors:
None.
See also:
Intersect (558)

Listing: objectex/ex3.pp


Program ex3;

{ Program to demonstrate TRect.Union }

Uses objects;


Var ARect,BRect,CRect : TRect;

begin
  ARect.Assign(10,10,20,20);
  BRect.Assign(15,15,25,25);
  { CRect is union of ARect and BRect }
  CRect.Assign(10,10,25,25);
  { Calculate it explicitly}
  ARect.Union(BRect);
  If ARect.Equals(CRect) Then
    Writeln ('ARect equals CRect')
  Else
    Writeln ('ARect does not equal CRect !');
end.

TRect.Intersect

Declaration:
Procedure TRect.Intersect (R: TRect);
Description:
Intersect makes the intersection of the current rectangle with R. If the intersection is empty, then the rectangle is set to the empty rectangle at coordinate (0,0).
Errors:
None.
See also:
Union (557)

Listing: objectex/ex4.pp


Program ex4;

{ Program to demonstrate TRect.Intersect }

Uses objects;


Var ARect,BRect,CRect : TRect;

begin
  ARect.Assign(10,10,20,20);
  BRect.Assign(15,15,25,25);
  { CRect is intersection of ARect and BRect }
  CRect.Assign(15,15,20,20);
  { Calculate it explicitly}
  ARect.Intersect(BRect);
  If ARect.Equals(CRect) Then
    Writeln ('ARect equals CRect')
  Else
    Writeln ('ARect does not equal CRect !');
  BRect.Assign(25,25,30,30);
  Arect.Intersect(BRect);
  If ARect.Empty Then
    Writeln ('ARect is empty');
end.

TRect.Move

Declaration:
Procedure TRect.Move (ADX, ADY: Sw_Integer);
Description:
Move moves the current rectangle along a vector with components (ADX,ADY). It adds ADX to the X-coordinate of both corner points, and ADY to both end points.
Errors:
None.
See also:
Grow (560)

Listing: objectex/ex5.pp


Program ex5;

{ Program to demonstrate TRect.Move }

Uses objects;


Var ARect,BRect : TRect;

begin
  ARect.Assign(10,10,20,20);
  ARect.Move(5,5);
  // Brect should be where new ARect is.
  BRect.Assign(15,15,25,25);
  If ARect.Equals(BRect) Then
    Writeln ('ARect equals BRect')
  Else
    Writeln ('ARect does not equal BRect !');
end.

TRect.Grow

Declaration:
Procedure TRect.Grow (ADX, ADY: Sw_Integer);
Description:
Grow expands the rectangle with an amount ADX in the X direction (both on the left and right side of the rectangle, thus adding a length 2*ADX to the width of the rectangle), and an amount ADY in the Y direction (both on the top and the bottom side of the rectangle, adding a length 2*ADY to the height of the rectangle.

ADX and ADY can be negative. If the resulting rectangle is empty, it is set to the empty rectangle at (0,0).

Errors:
None.
See also:
Move (559).

Listing: objectex/ex6.pp


Program ex6;

{ Program to demonstrate TRect.Grow }

Uses objects;


Var ARect,BRect : TRect;

begin
  ARect.Assign(10,10,20,20);
  ARect.Grow(5,5);
  // Brect should be where new ARect is.
  BRect.Assign(5,5,25,25);
  If ARect.Equals(BRect) Then
    Writeln ('ARect equals BRect')
  Else
    Writeln ('ARect does not equal BRect !');
end.

TRect.Assign

Declaration:
Procedure Trect.Assign (XA, YA, XB, YB: Sw_Integer);
Description:
Assign sets the corner points of the rectangle to (XA,YA) and (Xb,Yb).
Errors:
None.
See also:
Copy (557)

For an example, see TRect.Copy (557).