System.Types.TRectF

De RAD Studio API Documentation
Aller à : navigation, rechercher

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 Implicit(const Source: TRect): TRectF;
    class operator Explicit(const Source: TRectF): TRect;
    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;
    function CenterAt(const ADesignatedArea: TRectF): TRectF;
    function PlaceInto(const ADesignatedArea: TRectF; const AHorzAlign: THorzRectAlign = THorzRectAlign.Center;
      const AVertAlign: TVertRectAlign = TVertRectAlign.Center): TRectF;
    function SnapToPixel(const AScale: Single; const APlaceBetweenPixels: Boolean = True): TRectF;
    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);
  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);
  }
  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 };
};

Propriétés

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

Description

TRectF définit un rectangle, avec des coordonnées en points flottants.

TRectF représente l'emplacement et les dimensions d'un rectangle. Les coordonnées sont spécifiées soit sous la forme de quatre coordonnées Single distinctes représentant les bords gauche, supérieur, droit et inférieur, soit sous la forme de deux points représentant les emplacements des coins supérieur gauche et inférieur droit.

Typiquement, les valeurs TRectF représentent des emplacements en pixels, où l'origine du système de coordonnées en pixels correspond au coin supérieur gauche de l'écran (coordonnées écran) ou au coin supérieur gauche de la zone client d'un contrôle (coordonnées client). Quand une valeur TRectF représente un rectangle sur l'écran, par convention les bords supérieur et gauche sont considérés comme étant à l'intérieur du rectangle et les bords inférieur et droit à l'extérieur du rectangle. Cette convention permet à la largeur du rectangle d'être considérée Droite - Gauche et la hauteur d'être considérée Inférieure - Supérieure.

Voir aussi

Exemples de code