提供: Appmethod Libraries
移動先: 案内検索

Object Pascal

  TRectF = record
    function GetWidth: Single;
    procedure SetWidth(const Value: Single);
    function GetHeight: Single;
    procedure SetHeight(const Value: Single);
    function GetSize: TSizeF;
    procedure SetSize(const Value: TSizeF);
    function GetLocation: TPointF;
    constructor Create(const Origin: TPointF); overload;                               // empty rect at given origin
    constructor Create(const Origin: TPointF; const Width, Height: Single); overload; // at TPoint of origin with width and height
    constructor Create(const Left, Top, Right, Bottom: Single); overload;              // at x, y with width and height
    constructor Create(const P1, P2: TPointF; Normalize: Boolean = False); overload;  // with corners specified by p1 and p2
    constructor Create(const R: TRectF; Normalize: Boolean = False); overload;
    constructor Create(const R: TRect; Normalize: Boolean = False); overload;
    class operator Equal(const Lhs, Rhs: TRectF): Boolean;
    class operator NotEqual(const Lhs, Rhs: TRectF): Boolean;
    class operator Add(const Lhs, Rhs: TRectF): TRectF;
    class operator Multiply(const Lhs, Rhs: TRectF): TRectF;
    class function Empty: TRectF; inline; static;
    { This method is to be deprecated. It stretches current rectangle into designated area similarly to FitInto,
      but only when current rectangle is bigger than the area; otherwise, it only centers it. }
    function Fit(const BoundsRect: TRectF): Single; // deprecated 'Please consider using FitInto instead.';
    { Stretches current rectangle into the designated area, preserving aspect ratio. Note that unlike Fit, when designated
      area is bigger than current rectangle, the last one will be stretched to fill designated area (while Fit would only
      center it). }
    function FitInto(const ADesignatedArea: TRectF; out Ratio: Single): TRectF; overload;
    function FitInto(const ADesignatedArea: TRectF): TRectF; overload;
    procedure NormalizeRect;
    function IsEmpty: Boolean;
    function Contains(const Pt: TPointF): Boolean; overload;
    function Contains(const R: TRectF): Boolean; overload;
    function IntersectsWith(const R: TRectF): Boolean;
    class function Intersect(const R1: TRectF; const R2: TRectF): TRectF; overload; static;
    procedure Intersect(const R: TRectF); overload;
    class function Union(const R1: TRectF; const R2: TRectF): TRectF; overload; static;
    procedure Union(const R: TRectF); overload;
    class function Union(const Points: Array of TPointF): TRectF; overload; static;
    procedure Offset(const DX, DY: Single); overload;
    procedure Offset(const Point: TPointF); overload;
    procedure SetLocation(const X, Y: Single); overload;
    procedure SetLocation(const Point: TPointF); overload;
    procedure Inflate(const DX, DY: Single); overload;
    procedure Inflate(const DL, DT, DR, DB: Single); overload;
    function CenterPoint: TPointF;
    function Ceiling: TRect;
    function Truncate: TRect;
    function Round: TRect;
    function EqualsTo(const R: TRectF; const Epsilon: Single = 0): Boolean;
    function SplitRect(SplitType: TSplitRectType; Size: Integer): TRect; overload;
    function SplitRect(SplitType: TSplitRectType; Percent: Double): TRect; overload;
    property Width: Single read GetWidth write SetWidth;
    property Height: Single read GetHeight write SetHeight;
    property Size: TSizeF read GetSize write SetSize;
    property Location: TPointF read GetLocation write SetLocation;
  case Integer of
    0: (Left, Top, Right, Bottom: Single);
    1: (TopLeft, BottomRight: TPointF);


struct TRectF {
  float left;
  float top;
  float right;
  float bottom;
  { init(0,0,0,0); }
  TRectF(const TPointF& TL) _ALWAYS_INLINE {
    init(TL.x, TL.y, TL.x, TL.y);
  TRectF(const TPointF& TL, float width, float height) _ALWAYS_INLINE {
    init (TL.x, TL.y, TL.x + width, TL.y + height);
  TRectF(float l, float t, float r, float b) _ALWAYS_INLINE {
    init(l, t, r, b);
  TRectF(const TPointF& TL, const TPointF& BR) _ALWAYS_INLINE {
    init(TL.x, TL.y, BR.x, BR.y);
  TRectF(const RECT& r) _ALWAYS_INLINE {
    init(r.left,, r.right, r.bottom);
  void init(float l, float t, float r, float b) {
    left = l; top = t;
    right = r; bottom = b;
  TPointF& TopLeft() _ALWAYS_INLINE
  { return *((TPointF* )this); }
  TPointF& BottomRight() _ALWAYS_INLINE
  { return *((TPointF* )this+1); }
  const TPointF& TopLeft() const _ALWAYS_INLINE
  { return *((TPointF* )this); }
  const TPointF& BottomRight() const _ALWAYS_INLINE
  { return *((TPointF* )this+1); }
  float Width() const _ALWAYS_INLINE
  { return right  - left; }
  float Height() const _ALWAYS_INLINE
  { return bottom - top ; }
  static TRectF Empty() _ALWAYS_INLINE
  { return TRectF(); }
  void Normalize() _ALWAYS_INLINE {
    if (top > bottom) {
      float temp  = top;
      top = bottom;
      bottom = temp;
    if (left > right) {
      float temp = left;
      left = right;
      right = temp;
  bool operator ==(const TRectF& rc) const
    return _sameValue(left, rc.left) && _sameValue(top, &&
           _sameValue(right, rc.right) && _sameValue(bottom, rc.bottom);
  bool operator !=(const TRectF& rc) const
  {  return !(rc == *this); }
  bool IsEmpty() const _ALWAYS_INLINE {
    return _sameValue(right, left) || _sameValue(bottom, top); // differs from Delphi version
  bool Contains(const TPointF& p) const _ALWAYS_INLINE {
    return ((p.x > left || _sameValue(p.x, left)) && (p.y > top || _sameValue(p.y, top)) && (p.x < right) && (p.y < bottom));
  bool PtInRect(const TPointF& p) const _ALWAYS_INLINE {
    return Contains(p);
  bool Contains(const TRectF& r) const _ALWAYS_INLINE {
    return Contains(r.TopLeft()) && Contains(r.BottomRight());
  bool Overlaps(const TRectF &r) const _ALWAYS_INLINE {
    return IntersectsWith(r);
  bool Intersects(const TRectF &r) const _ALWAYS_INLINE {
    return IntersectsWith(r);
  bool IntersectsWith(const TRectF &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 TRectF Intersect(const TRectF &r1, const TRectF &r2);
  void Intersect(const TRectF &r);
  void Union(const TRectF &r);
  static TRectF Union(const TRectF &r1, const TRectF &r2);
  static TRectF Union(const TPointF* points, int npoints) _ALWAYS_INLINE {
    TPointF tl, br;
    if (npoints > 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 TRectF(tl, br);
  void Offset(float DX, float DY) _ALWAYS_INLINE {
    left   += DX;
    right  += DX;
    top    += DY;
    bottom += DY;
  void SetLocation(float X, float Y) _ALWAYS_INLINE {
      Offset(X - left, Y - top);
  void SetLocation(const TPointF& p) _ALWAYS_INLINE {
      Offset(p.x - left, p.y - top);
  void Inflate(float DX, float DY) _ALWAYS_INLINE {
    left   -= DX;
    right  += DX;
    top    -= DY;
    bottom += DY;
  void Inflate(float l, float t, float r, float b) _ALWAYS_INLINE {
    left   -= l;
    right  += r;
    top    -= t;
    bottom += b;
  void NormalizeRect() _ALWAYS_INLINE {
    float temp;
    if (left > right)
      temp = left;
      left = right;
      right = temp;
    if (top > bottom)
      temp = top;
      top = bottom;
      bottom = temp;
  TPointF CenterPoint() const _ALWAYS_INLINE {
    return TPointF((left+right)/2.0F, (top+bottom)/2.0F);
  TRect Ceiling() const _ALWAYS_INLINE {
    return TRect(TopLeft().Ceiling(), BottomRight().Ceiling());
  TRect Truncate() const _ALWAYS_INLINE {
    return TRect(TopLeft().Truncate(), BottomRight().Truncate());
  TRect Round() const _ALWAYS_INLINE {
    return TRect(TopLeft().Round(), BottomRight().Round());
  TRectF CenteredRect(const TRectF &CenteredRect) const _ALWAYS_INLINE {
    float w = CenteredRect.Width();
    float h = CenteredRect.Height();
    float x = (right + left)/2.0F;
    float y = (top + bottom)/2.0F;
    return TRectF(x-w/2.0F, y-h/2.0F, x+w/2.0F, y+h/2.0F);
  float GetWidth() const _ALWAYS_INLINE {
    return right - left;
  void SetWidth(float width) _ALWAYS_INLINE {
    right = left + width;
  float GetHeight() const _ALWAYS_INLINE {
    return bottom - top;
  void SetHeight(float height) _ALWAYS_INLINE {
    bottom = top + height;
  TSizeF GetSize() const _ALWAYS_INLINE {
    return TSizeF(GetWidth(), GetHeight());
  void SetSize(const TSizeF& newSize) _ALWAYS_INLINE {
  TPointF GetLocation() const _ALWAYS_INLINE {
    return TPointF(left, top);
  static float __fastcall _sqrf(float i) _ALWAYS_INLINE {
    return i*i;
  static bool __fastcall _sameValue(float a, float b) _ALWAYS_INLINE {
    const float SINGLE_RESOLUTION = 1.25E-6f;
    const float SINGLE_ZERO =6.25E-37f;
    float _epsilon = (float) ((fabs(a) > fabs(b)) ? fabs(a): fabs(b)) * SINGLE_RESOLUTION;
    if (_epsilon == 0)
      _epsilon = SINGLE_ZERO; // both a and b are very little, _epsilon was 0 because of normalization
    return (a > b) ? ((a - b) <= _epsilon): ((b - a) <= _epsilon);
  __property float Left    = { read=left,   write=left   };
  __property float Top     = { read=top,    write=top    };
  __property float Right   = { read=right,  write=right  };
  __property float Bottom  = { read=bottom, write=bottom };
  __property TSizeF Size   = { read=GetSize, write=SetSize };
  __property TPointF Location = { read=GetLocation, write=SetLocation };


種類 可視性 ソース ユニット
System.Types System.Types


TRectF は、浮動小数点の座標で、四角形を定義します。

TRectF は、四角形の位置とサイズを表します。 この座標は、上下左右の側を表す 4 つの別個の Single 座標か、 左上隅および右下隅の位置を表す 2 つの点として表されます。

通常、TRectF 値はピクセル位置を表します。ピクセル座標系の原点は、画面の左上隅か、コントロールのクライアント領域の左上隅です(前者の場合は画面座標、後者の場合はクライアント座標になります)。 TRectF 値で画面上の矩形を表す場合、慣例的に、上辺と左辺は矩形の内部にあり、下辺と右辺は矩形の外部にあると見なされます。 この慣例により、矩形の幅は Right - Left、高さは Bottom - Top で、それぞれ与えられます。


コード サンプル