System.Types.TRectF

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

TRectF = record

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);
  }
  TRectF CenterAt(const TRectF& Bounds) const _ALWAYS_INLINE {
    TRectF result = *this;
    result.Offset(-result.left, -result.top);
    result.Offset(_roundf((Bounds.Width() - result.Width()) / 2.0f),
                  _roundf((Bounds.Height() - result.Height()) / 2.0f));
    result.Offset(Bounds.left, Bounds.top);
    return result;
  }
  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);
  }
  TRectF FitInto(const TRectF& DesignatedArea,
                 float& Ratio) const;
  TRectF FitInto(const TRectF &DesignatedArea) const _ALWAYS_INLINE {
    float Ratio;
    return FitInto(DesignatedArea,Ratio);
  }
  TRectF SnapToPixel(float Scale,
                     bool PlaceBetweenPixels = true) const _ALWAYS_INLINE {
    if (Scale <= 0.0f)
      Scale = 1.0f;
    TRectF result;
    result.left = _roundf(left * Scale) / Scale;
    result.top = _roundf(top * Scale) / Scale;
    result.SetWidth(_roundf( Width() * Scale) / Scale);
    result.SetHeight(_roundf( Height() * Scale) / Scale);
    if (PlaceBetweenPixels) {
      Scale /= 2.0f;
      result.Offset(Scale, Scale);
    }
    return result;
  }
  TRectF PlaceInto(const TRectF &DesignatedArea, THorzRectAlign HorzAlign, TVertRectAlign VertAlign) const;
  __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 };
};

Properties

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

Description

TRectF defines a rectangle, with coordinates in floating points.

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

Typically, TRectF 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 TRectF 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


Code Examples