System.Types.TRectF

提供:RAD Studio API Documentation
移動: 案内, 検索

Delphi

  TRectF = record
  private
    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;
  public
    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;
    function Fit(BoundsRect: TRectF): Single;
    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 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);
  end;

C++

struct TRectF {  float left;
  float top;
  float right;
  float bottom;
  TRectF() _ALWAYS_INLINE
  { 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);
    Normalize();
  }
  TRectF(const RECT& r) _ALWAYS_INLINE {
    init(r.left, r.top, 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, rc.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) {
      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 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 {
    SetWidth(newSize.cx);
    SetHeight(newSize.cy);
  }
  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 };
};

目次

プロパティ

種類 可視性 ソース ユニット
record
struct
public
System.Types.pas
SystemTypes.h
System.Types System.Types

説明

TRectF は、座標が浮動小数点数で表される四角形を定義します。


TRectF は、四角形の位置とサイズを表します。座標は右下隅として指定されます。

座標の型は Single です。

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

関連項目


コード例

他の言語