Show: Delphi C++
Display Preferences

System.Types.TRect

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

  TRect = record
  private
    function GetWidth: Integer;
    procedure SetWidth(const Value: Integer);
    function GetHeight: Integer;
    procedure SetHeight(const Value: Integer);
    function GetSize: TSize;
    procedure SetSize(const Value: TSize);
    function GetLocation: TPoint;
  public
    constructor Create(const Origin: TPoint); overload;                              // empty rect at given origin
    constructor Create(const Origin: TPoint; Width, Height: Integer); overload;      // at TPoint of origin with width and height
    constructor Create(const Left, Top, Right, Bottom: Integer); overload;           // at x, y with width and height
    constructor Create(const P1, P2: TPoint; Normalize: Boolean = False); overload;  // with corners specified by p1 and p2
    constructor Create(const R: TRect; Normalize: Boolean = False); overload;
    class operator Equal(const Lhs, Rhs: TRect): Boolean;
    class operator NotEqual(const Lhs, Rhs: TRect): Boolean;
    class operator Add(const Lhs, Rhs: TRect): TRect;
    class operator Multiply(const Lhs, Rhs: TRect): TRect;
    class function Empty: TRect; inline; static;
    procedure NormalizeRect;
    function IsEmpty: Boolean;
    function Contains(const Pt: TPoint): Boolean; overload;
    function Contains(const R: TRect): Boolean; overload;
    function IntersectsWith(const R: TRect): Boolean;
    class function Intersect(const R1: TRect; const R2: TRect): TRect; overload; static;
    procedure Intersect(const R: TRect); overload;
    class function Union(const R1: TRect; const R2: TRect): TRect; overload; static;
    procedure Union(const R: TRect); overload;
    class function Union(const Points: Array of TPoint): TRect; overload; static;
    procedure Offset(const DX, DY: Integer); overload;
    procedure Offset(const Point: TPoint); overload;
    procedure SetLocation(const X, Y: Integer); overload;
    procedure SetLocation(const Point: TPoint); overload;
    procedure Inflate(const DX, DY: Integer); overload;
    procedure Inflate(const DL, DT, DR, DB: Integer); overload;
    function CenterPoint: TPoint;
    function SplitRect(SplitType: TSplitRectType; Size: Integer): TRect; overload;
    function SplitRect(SplitType: TSplitRectType; Percent: Double): TRect; overload;
    property Width: Integer read GetWidth write SetWidth;
    property Height: Integer read GetHeight write SetHeight;
    property Size: TSize read GetSize write SetSize;
    property Location: TPoint read GetLocation write SetLocation;
  case Integer of
    0: (Left, Top, Right, Bottom: FixedInt);
    1: (TopLeft, BottomRight: TPoint);
  end;

C++

struct TRect: public RECT  {
  TRect() _ALWAYS_INLINE
  { init(0,0,0,0); }
  TRect(const TPoint& TL) _ALWAYS_INLINE {
    init(TL.x, TL.y, TL.x, TL.y);
  }
  TRect(const TPoint& TL, int width, int height) _ALWAYS_INLINE {
    init (TL.x, TL.y, TL.x + width, TL.y + height);
  }
  TRect(int l, int t, int r, int b) _ALWAYS_INLINE {
    init(l, t, r, b);
  }
  TRect(const TPoint& TL, const TPoint& BR) _ALWAYS_INLINE {
    init(TL.x, TL.y, BR.x, BR.y);
    Normalize();
  }
  TRect(const RECT& r) _ALWAYS_INLINE {
    init(r.left, r.top, r.right, r.bottom);
  }
  void init(int l, int t, int r, int b) _ALWAYS_INLINE {
    left = l; top = t;
    right = r; bottom = b;
  }
  TPoint& TopLeft() _ALWAYS_INLINE
  { return *((TPoint* )this); }
  TPoint& BottomRight() _ALWAYS_INLINE
  { return *((TPoint* )this+1); }
  const TPoint& TopLeft() const _ALWAYS_INLINE
  { return *((TPoint* )this); }
  const TPoint& BottomRight() const _ALWAYS_INLINE
  { return *((TPoint* )this+1); }
  int Width() const _ALWAYS_INLINE
  { return right  - left; }
  int Height() const _ALWAYS_INLINE
  { return bottom - top ; }
  static TRect Empty() _ALWAYS_INLINE
  { return TRect(); }
  void Normalize() _ALWAYS_INLINE {
    if (top > bottom) {
      top = top ^ bottom;
      bottom = top ^ bottom;
      top = top ^ bottom;
    }
    if (left > right) {
      left = left ^ right;
      right = left ^ right;
      left = left ^ right;
    }
  }
  bool operator ==(const TRect& rc) const  {
     return left ==  rc.left  && top==rc.top &&
            right == rc.right && bottom==rc.bottom;
  }
  bool operator !=(const TRect& rc) const {
    return !(rc==*this);
  }
  TRect operator+(const TRect& rc) const _ALWAYS_INLINE {
    return TRect::Union(*this, rc);
  }
  TRect operator*(const TRect& rc) const _ALWAYS_INLINE {
    return TRect::Intersect(*this, rc);
  }
  TRect& operator+=(const TRect& rhs) _ALWAYS_INLINE {
    Union(rhs);
    return *this;
  }
  TRect& operator*=(const TRect& rhs) _ALWAYS_INLINE {
    Intersect(rhs);
    return *this;
  }
  bool IsEmpty() const _ALWAYS_INLINE {
    return (right == left) || (bottom == top);
  }
  bool Contains(const TPoint& p) const _ALWAYS_INLINE {
    return ((p.x >= left) && (p.y >= top) && (p.x < right) && (p.y < bottom));
  }
  bool PtInRect(const TPoint& p) const _ALWAYS_INLINE {
    return Contains(p);
  }
  bool Contains(const TRect& r) const _ALWAYS_INLINE {
    return Contains(r.TopLeft()) && Contains(r.BottomRight());
  }
  bool Overlaps(const TRect &r) const _ALWAYS_INLINE {
    return IntersectsWith(r);
  }
  bool Intersects(const TRect &r) const _ALWAYS_INLINE {
    return IntersectsWith(r);
  }
  bool IntersectsWith(const TRect &r) const _ALWAYS_INLINE {
    return !( (BottomRight().x < r.TopLeft().x) ||
              (BottomRight().y < r.TopLeft().y) ||
              (r.BottomRight().x < TopLeft().x) ||
              (r.BottomRight().y < TopLeft().y) );
  }
  static TRect Intersect(const TRect &r1, const TRect &r2);
  void Intersect(const TRect &r);
  void Union(const TRect &r);
  static TRect Union(const TRect &r1, const TRect& r2);
  static TRect Union(const TPoint* points, int npoints) _ALWAYS_INLINE {
    TPoint tl, br;
    if (npoints > 0) {
      tl.SetLocation(points[0]);
      br.SetLocation(points[0]);
      for (int i = npoints; --i > 0;) {
        if (points[i].x < tl.x)     tl.x = points[i].x;
        if (points[i].x > br.x)     br.x = points[i].x;
        if (points[i].y < tl.y)     tl.y = points[i].y;
        if (points[i].y > br.y)     br.y = points[i].y;
      }
    }
    return TRect(tl, br);
  }
  bool  IntersectRect(const TRect &R1, const TRect &R2);
  bool  UnionRect(const TRect &R1, const TRect &R2);
  void Offset(int DX, int DY) _ALWAYS_INLINE {
    left   += DX;
    right  += DX;
    top    += DY;
    bottom += DY;
  }
  void Offset(const TPoint& p) _ALWAYS_INLINE { Offset(p.x, p.y); }
  void SetLocation(int X, int Y) _ALWAYS_INLINE {
      Offset(X - left, Y - top);
  }
  void SetLocation(const TPoint& p) _ALWAYS_INLINE {
      Offset(p.x - left, p.y - top);
  }
  void Inflate(int DX, int DY) _ALWAYS_INLINE {
    left   -= DX;
    right  += DX;
    top    -= DY;
    bottom += DY;
  }
  void Inflate(int l, int t, int r, int b) _ALWAYS_INLINE {
    left   -= l;
    right  += r;
    top    -= t;
    bottom += b;
  }
  void NormalizeRect() _ALWAYS_INLINE {
    int i;
    if (left > right)
    {
      i = left;
      left = right;
      right = i;
    }
    if (top > bottom)
    {
      i = top;
      top = bottom;
      bottom = i;
    }
  }
  TPoint CenterPoint() const _ALWAYS_INLINE {
    return TPoint((left+right)/2, (top+bottom)/2);
  }
  TRect CenteredRect(const TRect &CenteredRect) const _ALWAYS_INLINE {
    int w = CenteredRect.Width();
    int h = CenteredRect.Height();
    int x = (right + left)/2;
    int y = (top + bottom)/2;
    return TRect(x-w/2, y-h/2, x+(w+1)/2, y+(h+1)/2);
  }
  TRect SplitRect(TSplitRectType SplitType, int Size) const;
  TRect SplitRect(TSplitRectType SplitType, double Percent) const;
#if defined(_Windows)
  bool SubtractRect(const TRect &R1, const TRect &R2) _ALWAYS_INLINE {
    return ::SubtractRect(this, &R1, &R2) != 0;
  }
#endif
  LONG GetWidth() const _ALWAYS_INLINE {
    return right - left;
  }
  void SetWidth(LONG width) _ALWAYS_INLINE {
    right = left + width;
  }
  LONG GetHeight() const _ALWAYS_INLINE {
    return bottom - top;
  }
  void SetHeight(LONG height) _ALWAYS_INLINE {
    bottom = top + height;
  }
  TSize GetSize() const _ALWAYS_INLINE {
    TSize r;
    r.cx = GetWidth();
    r.cy = GetHeight();
    return r;
  }
  void SetSize(const TSize& newSize) _ALWAYS_INLINE {
    SetWidth(newSize.cx);
    SetHeight(newSize.cy);
  }
  TPoint GetLocation() const _ALWAYS_INLINE {
    return TPoint(left, top);
  }
  __property LONG Left    = { read=left,   write=left   };
  __property LONG Top     = { read=top,    write=top    };
  __property LONG Right   = { read=right,  write=right  };
  __property LONG Bottom  = { read=bottom, write=bottom };
  __property TSize Size   = { read=GetSize, write=SetSize };
  __property TPoint Location = { read=GetLocation, write=SetLocation };
};

Properties

Type Visibility Source Unit Parent
record
struct
public
System.Types.pas
SystemTypes.h
System.Types System.Types

Description

TRect defines a rectangle.

TRect represents the dimensions of a rectangle. The coordinates are specified as either four separate integers representing the left, top, right, and bottom sides, or as two points representing the locations of the upper-left and lower-right corners.

Typically, TRect values represent pixel locations, where the origin of the pixel coordinate system is in the upper-left corner of the screen (screen coordinates) or the upper-left corner of a control's client area (client coordinates). When a TRect value represents a rectangle on the screen, by convention the top and left edges are considered inside the rectangle and the bottom and right edges are considered outside the rectangle. This convention allows the width of the rectangle to be Right - Left and the height to be Bottom - Top.

See Also

Personal tools
In Other Languages
Previous Versions
Assistance