TRectContains (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example illustrates how to use Contains method to test whether a point or a rectangle is located within the rectangle.

Code

uses
  System.SysUtils,
  System.Types;

var
  aRectangle : TRect;
  aPoint1,aPoint2: TPoint;
  flag1,flag2: Boolean;

begin
    aPoint1.Create(1,4);
    aRectangle.Create(aPoint1,10,10);
    writeln(Format('%d %d %d %d',[aRectangle.Left,aRectangle.Top,aRectangle.Right,aRectangle.Bottom]));
    //The console will output: 1 4 11 14, representing the coordinates of the corners of aRectangle

    aRectangle2.Create(aPoint1,5,5);

    flag1:= aRectangle1.Contains(aRectangle2);
    if flag1 then
           writeln('aRectangle1 contains aRectangle2')
    else
           writeln('aRectangle1 does not contain aRectangle2');
    // The console will output: aRectangle1 contains aRectangle2  
 
    aPoint2.Create(40,34);
    flag2:= aRectangle2.Contains(aPoint2);
    if flag2 then
            writeln('aRectangle1 contains aPoint2')
    else
            writeln('aRectangle1 does not contain aPoint2');
    // The console will output: aRectangle1 does not contain aPoint2
 
end.

Uses