Object TRectangle

Unit

Declaration

type TRectangle = object(TObject)

Description

2D rectangle with integer coordinates. Useful for various 2D GUI operations.

The area covered by the rectangle starts in (Left,Bottom) pixel and spans (Width,Height) pixels. This means that the right-top pixel covered by the rectangle is (Left + Width - 1,Bottom + Height - 1). The rectangle is empty (Contains will always answer False) when either Width or Height are zero. Neither Width nor Height can ever be negative.

Hierarchy

  • TObject
  • TRectangle

Overview

Fields

Public Left: Integer;
Public Bottom: Integer;
Public Width: Cardinal;
Public Height: Cardinal;
Public internal const Empty: TRectangle = (Left: 0; Bottom: 0; Width: 0; Height: 0);

Methods

Public function IsEmpty: boolean;
Public function Contains(const X, Y: Integer): boolean;
Public function Contains(const Point: TVector2Single): boolean;
Public function Contains(const Point: TVector2Integer): boolean;
Public function CenterInside(const W, H: Cardinal): TRectangle;
Public function Grow(const Delta: Integer): TRectangle;
Public function Grow(const DeltaX, DeltaY: Integer): TRectangle;
Public function RemoveLeft(W: Cardinal): TRectangle;
Public function RemoveBottom(H: Cardinal): TRectangle;
Public function RemoveRight(W: Cardinal): TRectangle;
Public function RemoveTop(H: Cardinal): TRectangle;
Public function GrowLeft(const W: Cardinal): TRectangle;
Public function GrowBottom(const H: Cardinal): TRectangle;
Public function GrowRight(const W: Cardinal): TRectangle;
Public function GrowTop(const H: Cardinal): TRectangle;
Public function LeftPart(W: Cardinal): TRectangle;
Public function BottomPart(H: Cardinal): TRectangle;
Public function RightPart(W: Cardinal): TRectangle;
Public function TopPart(H: Cardinal): TRectangle;
Public function Middle: TVector2Integer; deprecated 'use Center';
Public function Center: TVector2Integer;
Public function ClampX(const X: Integer): Integer;
Public function ClampY(const Y: Integer): Integer;
Public function ScaleToWidth(const NewWidth: Cardinal): TRectangle;
Public function ScaleToHeight(const NewHeight: Cardinal): TRectangle;
Public function ScaleAroundCenter(const Factor: Single): TRectangle;
Public function ScaleAroundMiddle(const Factor: Single): TRectangle; deprecated 'use ScaleAroundCenter';
Public function ScaleAround0(const Factor: Single): TRectangle;
Public function ScaleWidthAround0(const Factor: Single): Cardinal;
Public function ScaleHeightAround0(const Factor: Single): Cardinal;
Public function FitInside(const R: TRectangle; const AlignHorizontal: THorizontalPosition = hpMiddle; const AlignVertical: TVerticalPosition = vpMiddle): TRectangle;
Public function AlignCore( const ThisPosition: THorizontalPosition; const OtherRect: TRectangle; const OtherPosition: THorizontalPosition; const X: Integer = 0): Integer;
Public function Align( const ThisPosition: THorizontalPosition; const OtherRect: TRectangle; const OtherPosition: THorizontalPosition; const X: Integer = 0): TRectangle;
Public function AlignCore( const ThisPosition: TVerticalPosition; const OtherRect: TRectangle; const OtherPosition: TVerticalPosition; const Y: Integer = 0): Integer;
Public function Align( const ThisPosition: TVerticalPosition; const OtherRect: TRectangle; const OtherPosition: TVerticalPosition; const Y: Integer = 0): TRectangle;
Public function ToString: string;
Public function Translate(const V: TVector2Integer): TRectangle;
Public function Collides(const R: TRectangle): boolean;

Properties

Public property Right: Integer read GetRight ;
Public property Top: Integer read GetTop ;
Public property LeftBottom: TVector2Integer read GetLeftBottom write SetLeftBottom;

Description

Fields

Public Left: Integer;
 
Public Bottom: Integer;
 
Public Width: Cardinal;
 
Public Height: Cardinal;
 
Public internal const Empty: TRectangle = (Left: 0; Bottom: 0; Width: 0; Height: 0);
 

Methods

Public function IsEmpty: boolean;
 
Public function Contains(const X, Y: Integer): boolean;
 
Public function Contains(const Point: TVector2Single): boolean;
 
Public function Contains(const Point: TVector2Integer): boolean;
 
Public function CenterInside(const W, H: Cardinal): TRectangle;

Return rectangle with given width and height centered in the middle of this rectangle. The given W, H may be smaller or larger than this rectangle sizes.

Public function Grow(const Delta: Integer): TRectangle;

Grow (when Delta > 0) or shrink (when Delta < 0) the rectangle, returning new value. This adds a margin of Delta pixels around all sides of the rectangle, so in total width grows by 2 * Delta, and the same for height. In case of shrinking, we protect from shrinking too much: the resulting width or height is set to zero (which makes a valid and empty rectangle) if shrinking too much.

Public function Grow(const DeltaX, DeltaY: Integer): TRectangle;
 
Public function RemoveLeft(W: Cardinal): TRectangle;

Returns the rectangle with a number of pixels from given side removed. Returns an empty rectangle if you try to remove too much.

Public function RemoveBottom(H: Cardinal): TRectangle;
 
Public function RemoveRight(W: Cardinal): TRectangle;
 
Public function RemoveTop(H: Cardinal): TRectangle;
 
Public function GrowLeft(const W: Cardinal): TRectangle;

Returns the rectangle with a number of pixels on given side added.

Public function GrowBottom(const H: Cardinal): TRectangle;
 
Public function GrowRight(const W: Cardinal): TRectangle;
 
Public function GrowTop(const H: Cardinal): TRectangle;
 
Public function LeftPart(W: Cardinal): TRectangle;

Returns the given side of the rectangle, cut down to given number of pixels from given side. This is similar to RemoveXxx methods, but here you specify which side to keep, as opposed to RemoveXxx methods where you specify which side you remove.

If the requested size is larger than current size (for example, W > Width for LeftPart) then the unmodified rectangle is returned.

Public function BottomPart(H: Cardinal): TRectangle;
 
Public function RightPart(W: Cardinal): TRectangle;
 
Public function TopPart(H: Cardinal): TRectangle;
 
Public function Middle: TVector2Integer; deprecated 'use Center';

Warning: this symbol is deprecated: use Center

 
Public function Center: TVector2Integer;
 
Public function ClampX(const X: Integer): Integer;

Clamp value to be within allowed horizontal range. That is, clamp to [Left, Right - 1].

Public function ClampY(const Y: Integer): Integer;

Clamp value to be within allowed vertical range. That is, clamp to [Bottom, Top - 1].

Public function ScaleToWidth(const NewWidth: Cardinal): TRectangle;
 
Public function ScaleToHeight(const NewHeight: Cardinal): TRectangle;
 
Public function ScaleAroundCenter(const Factor: Single): TRectangle;

Scale rectangle position and size around it's own Center point.

Since the scaling is independent in each axis, this handles "carefully" a half-empty rectangles (when one size is <= 0, but other is > 0). It scales correctly the positive dimension (not just returns Empty constant), leaving the other dimension (it's position and size) untouched.

Public function ScaleAroundMiddle(const Factor: Single): TRectangle; deprecated 'use ScaleAroundCenter';

Warning: this symbol is deprecated: use ScaleAroundCenter

 
Public function ScaleAround0(const Factor: Single): TRectangle;

Scale rectangle position and size around the (0,0) point.

Since the scaling is independent in each axis, this handles "carefully" a half-empty rectangles (when one size is <= 0, but other is > 0). It scales correctly the positive dimension (not just returns Empty constant), leaving the other dimension (it's position and size) untouched.

These details matter, e.g. when you set TUIControlSizeable.Width, but not TUIControlSizeable.Height, and then you expect the TUIControl.CalculatedWidth to work.

Public function ScaleWidthAround0(const Factor: Single): Cardinal;

Scale Width, in the same manner as ScaleAround0 would do.

Public function ScaleHeightAround0(const Factor: Single): Cardinal;

Scale Height, in the same manner as ScaleAround0 would do.

Public function FitInside(const R: TRectangle; const AlignHorizontal: THorizontalPosition = hpMiddle; const AlignVertical: TVerticalPosition = vpMiddle): TRectangle;

Scale and align us to fit inside rectangle R, preserving our aspect ratio.

Public function AlignCore( const ThisPosition: THorizontalPosition; const OtherRect: TRectangle; const OtherPosition: THorizontalPosition; const X: Integer = 0): Integer;

Align this rectangle within other rectangle by calculating new value for Left.

Public function Align( const ThisPosition: THorizontalPosition; const OtherRect: TRectangle; const OtherPosition: THorizontalPosition; const X: Integer = 0): TRectangle;
 
Public function AlignCore( const ThisPosition: TVerticalPosition; const OtherRect: TRectangle; const OtherPosition: TVerticalPosition; const Y: Integer = 0): Integer;

Align this rectangle within other rectangle by calculating new value for Bottom.

Public function Align( const ThisPosition: TVerticalPosition; const OtherRect: TRectangle; const OtherPosition: TVerticalPosition; const Y: Integer = 0): TRectangle;
 
Public function ToString: string;
 
Public function Translate(const V: TVector2Integer): TRectangle;

Move the rectangle. Empty rectangle after moving is still an empty rectangle.

Public function Collides(const R: TRectangle): boolean;

Does it have any common part with another rectangle.

Properties

Public property Right: Integer read GetRight ;

Right and top coordinates of the rectangle. Right is simply the Left + Width, Top is simply the Bottom + Height.

If you use this for drawing, note that the pixel with coordinates (Right, Top) is actually *outside* of the rectangle (by 1 pixel). That's because the rectangle starts at pixel (Left, Bottom) and spans the (Width, Height) pixels.

Public property Top: Integer read GetTop ;
 
Public property LeftBottom: TVector2Integer read GetLeftBottom write SetLeftBottom;
 

Generated by PasDoc 0.14.0.