表示: Delphi C++
表示設定

System.Types.TRect

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

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: Longint);
    1: (TopLeft, BottomRight: TPoint);
  end;

C++

struct TRect: public RECT{
TRect() { init(0,0,0,0); }
TRect(const TPoint& TL)
{
init(TL.x, TL.y, TL.x, TL.y);
}
TRect(const TPoint& TL, int width, int height)
{
init (TL.x, TL.y, TL.x + width, TL.y + height);
}
TRect(int l, int t, int r, int b)
{
init(l, t, r, b);
}
TRect(const TPoint& TL, const TPoint& BR)
{
init(TL.x, TL.y, BR.x, BR.y);
Normalize();
}
TRect(const RECT& r)
{
init(r.left, r.top, r.right, r.bottom);
}
void init(int l, int t, int r, int b)
{
left = l; top = t;
right = r; bottom = b;
}
TPoint& TopLeft()                 { return *((TPoint* )this); }
TPoint& BottomRight()             { return *((TPoint* )this+1); }
const TPoint& TopLeft() const     { return *((TPoint* )this); }
const TPoint& BottomRight() const { return *((TPoint* )this+1); }
int Width() const  { return right  - left; }
int Height() const { return bottom - top ; }
static TRect Empty() { return TRect(); }
void Normalize()
{
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); }
bool IsEmpty() const
{
return (right == left) || (bottom == top);
}
bool Contains(const TPoint& p) const
{
return ((p.x >= left) && (p.y >= top) && (p.x < right) && (p.y < bottom));
}
bool PtInRect(const TPoint& p) const
{
return Contains(p);
}
bool Contains(const TRect& r) const
{
return Contains(r.TopLeft()) && Contains(r.BottomRight());
}
bool Overlaps(const TRect &r) const
{
return IntersectsWith(r);
}
bool Intersects(const TRect &r) const
{
return IntersectsWith(r);
}
bool IntersectsWith(const TRect &r) const
{
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) {
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)
{
left   += DX;
right  += DX;
top    += DY;
bottom += DY;
}
void SetLocation(int X, int Y)
{
Offset(X - left, Y - top);
}
void SetLocation(const TPoint& p)
{
Offset(p.x - left, p.y - top);
}
void Inflate(int DX, int DY)
{
left   -= DX;
right  += DX;
top    -= DY;
bottom += DY;
}
void Inflate(int l, int t, int r, int b)
{
left   -= l;
right  += r;
top    -= t;
bottom += b;
}
void NormalizeRect()
{
int i;
if (left > right)
{
i = left;
left = right;
right = i;
}
if (top > bottom)
{
i = top;
top = bottom;
bottom = i;
}
}
TPoint CenterPoint() const
{
return TPoint((left+right)/2, (top+bottom)/2);
}
TRect CenteredRect(const TRect &CenteredRect) const
{
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);
}
#if 0
TRect SplitRect(TSplitRectType SplitType, int Size) const;
TRect SplitRect(TSplitRectType SplitType, double Percent) const;
#endif
#if defined(_Windows)
bool SubtractRect(const TRect &R1, const TRect &R2)
{
return ::SubtractRect(this, &R1, &R2) != 0;
}
#endif
LONG GetWidth() const {
return right - left;
}
void SetWidth(LONG width) {
right = left + width;
}
LONG GetHeight() const {
return bottom - top;
}
void SetHeight(LONG height) {
bottom = top + height;
}
TSize GetSize() const {
TSize r;
r.cx = GetWidth();
r.cy = GetHeight();
return r;
}
void SetSize(const TSize& newSize) {
SetWidth(newSize.cx);
SetHeight(newSize.cy);
}
TPoint GetLocation() const {
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 };
};

プロパティ

種類 可視性 ソース ユニット
struct
class
public
System.Types.pas
System.Types.hpp
System.Types System.Types


説明

TRect は矩形を定義したものです。

TRect は矩形の大きさを表します。 座標は、左辺、上辺、右辺、下辺を表す 4 つの別個の整数か、左上隅と右下隅の位置を表す 2 つの点で指定されます。

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

関連項目

以前のバージョン
他の言語