| 1 | unit URectangle;
|
|---|
| 2 |
|
|---|
| 3 | // Date: 2011-03-20
|
|---|
| 4 |
|
|---|
| 5 | {$mode Delphi}{$H+}
|
|---|
| 6 |
|
|---|
| 7 | interface
|
|---|
| 8 |
|
|---|
| 9 | uses
|
|---|
| 10 | Classes, SysUtils;
|
|---|
| 11 |
|
|---|
| 12 | type
|
|---|
| 13 | { TRectangle }
|
|---|
| 14 |
|
|---|
| 15 | TRectangle = class
|
|---|
| 16 | private
|
|---|
| 17 | function GetBottomLeft: TPoint;
|
|---|
| 18 | function GetBottomRight: TPoint;
|
|---|
| 19 | function GetHeight: Integer;
|
|---|
| 20 | function GetSize: TPoint;
|
|---|
| 21 | function GetTopLeft: TPoint;
|
|---|
| 22 | function GetTopRight: TPoint;
|
|---|
| 23 | function GetTRect: TRect;
|
|---|
| 24 | function GetWidth: Integer;
|
|---|
| 25 | procedure SetBottom(const AValue: Integer);
|
|---|
| 26 | procedure SetBottomLeft(const AValue: TPoint);
|
|---|
| 27 | procedure SetBottomRight(const AValue: TPoint);
|
|---|
| 28 | procedure SetHeight(const AValue: Integer);
|
|---|
| 29 | procedure SetLeft(const AValue: Integer);
|
|---|
| 30 | procedure SetRight(const AValue: Integer);
|
|---|
| 31 | procedure SetSize(const AValue: TPoint);
|
|---|
| 32 | procedure SetTop(const AValue: Integer);
|
|---|
| 33 | procedure SetTopLeft(const AValue: TPoint);
|
|---|
| 34 | procedure SetTopRight(const AValue: TPoint);
|
|---|
| 35 | procedure SetTRect(const AValue: TRect);
|
|---|
| 36 | procedure SetWidth(const AValue: Integer);
|
|---|
| 37 | public
|
|---|
| 38 | FLeft: Integer;
|
|---|
| 39 | FTop: Integer;
|
|---|
| 40 | FRight: Integer;
|
|---|
| 41 | FBottom: Integer;
|
|---|
| 42 | KeepSize: Boolean;
|
|---|
| 43 |
|
|---|
| 44 | property Left: Integer read FLeft write SetLeft;
|
|---|
| 45 | property Top: Integer read FTop write SetTop;
|
|---|
| 46 | property Right: Integer read FRight write SetRight;
|
|---|
| 47 | property Bottom: Integer read FBottom write SetBottom;
|
|---|
| 48 |
|
|---|
| 49 | procedure Assign(Source: TRectangle);
|
|---|
| 50 | function IsInside(Pos: TPoint): Boolean;
|
|---|
| 51 |
|
|---|
| 52 | property Width: Integer read GetWidth write SetWidth;
|
|---|
| 53 | property Height: Integer read GetHeight write SetHeight;
|
|---|
| 54 |
|
|---|
| 55 | property TopLeft: TPoint read GetTopLeft write SetTopLeft;
|
|---|
| 56 | property TopRight: TPoint read GetTopRight write SetTopRight;
|
|---|
| 57 | property BottomLeft: TPoint read GetBottomLeft write SetBottomLeft;
|
|---|
| 58 | property BottomRight: TPoint read GetBottomRight write SetBottomRight;
|
|---|
| 59 |
|
|---|
| 60 | property Size: TPoint read GetSize write SetSize;
|
|---|
| 61 |
|
|---|
| 62 | property AsTRect: TRect read GetTRect write SetTRect;
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 | implementation
|
|---|
| 66 |
|
|---|
| 67 | { TRectangle }
|
|---|
| 68 |
|
|---|
| 69 | function TRectangle.GetBottomLeft: TPoint;
|
|---|
| 70 | begin
|
|---|
| 71 | Result.X := Left;
|
|---|
| 72 | Result.Y := Bottom;
|
|---|
| 73 | end;
|
|---|
| 74 |
|
|---|
| 75 | function TRectangle.GetBottomRight: TPoint;
|
|---|
| 76 | begin
|
|---|
| 77 | Result.X := Right;
|
|---|
| 78 | Result.Y := Bottom;
|
|---|
| 79 | end;
|
|---|
| 80 |
|
|---|
| 81 | function TRectangle.GetHeight: Integer;
|
|---|
| 82 | begin
|
|---|
| 83 | Result := Bottom - Top;
|
|---|
| 84 | end;
|
|---|
| 85 |
|
|---|
| 86 | function TRectangle.GetSize: TPoint;
|
|---|
| 87 | begin
|
|---|
| 88 | Result := Point(Width, Height);
|
|---|
| 89 | end;
|
|---|
| 90 |
|
|---|
| 91 | function TRectangle.GetTopLeft: TPoint;
|
|---|
| 92 | begin
|
|---|
| 93 | Result.X := Left;
|
|---|
| 94 | Result.Y := Top;
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | function TRectangle.GetTopRight: TPoint;
|
|---|
| 98 | begin
|
|---|
| 99 | Result.X := Right;
|
|---|
| 100 | Result.Y := Top;
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | function TRectangle.GetTRect: TRect;
|
|---|
| 104 | begin
|
|---|
| 105 | Result.Left := Left;
|
|---|
| 106 | Result.Top := Top;
|
|---|
| 107 | Result.Bottom := Bottom;
|
|---|
| 108 | Result.Right := Right;
|
|---|
| 109 | end;
|
|---|
| 110 |
|
|---|
| 111 | function TRectangle.GetWidth: Integer;
|
|---|
| 112 | begin
|
|---|
| 113 | Result := Right - Left;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure TRectangle.SetBottom(const AValue: Integer);
|
|---|
| 117 | begin
|
|---|
| 118 | if FBottom = AValue then exit;
|
|---|
| 119 | if KeepSize then Inc(FTop, AValue - FBottom);
|
|---|
| 120 | FBottom := AValue;
|
|---|
| 121 | end;
|
|---|
| 122 |
|
|---|
| 123 | procedure TRectangle.SetBottomLeft(const AValue: TPoint);
|
|---|
| 124 | begin
|
|---|
| 125 | Left := AValue.X;
|
|---|
| 126 | Bottom := AValue.Y;
|
|---|
| 127 | end;
|
|---|
| 128 |
|
|---|
| 129 | procedure TRectangle.SetBottomRight(const AValue: TPoint);
|
|---|
| 130 | begin
|
|---|
| 131 | Right := AValue.X;
|
|---|
| 132 | Bottom := AValue.Y;
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | procedure TRectangle.SetHeight(const AValue: Integer);
|
|---|
| 136 | begin
|
|---|
| 137 | Bottom := Top + AValue;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | procedure TRectangle.SetLeft(const AValue: Integer);
|
|---|
| 141 | begin
|
|---|
| 142 | if FLeft = AValue then Exit;
|
|---|
| 143 | if KeepSize then Inc(FRight, AValue - FLeft);
|
|---|
| 144 | FLeft := AValue;
|
|---|
| 145 | end;
|
|---|
| 146 |
|
|---|
| 147 | procedure TRectangle.SetRight(const AValue: Integer);
|
|---|
| 148 | begin
|
|---|
| 149 | if FRight = AValue then Exit;
|
|---|
| 150 | if KeepSize then Inc(FLeft, AValue - FRight);
|
|---|
| 151 | FRight := AValue;
|
|---|
| 152 | end;
|
|---|
| 153 |
|
|---|
| 154 | procedure TRectangle.SetSize(const AValue: TPoint);
|
|---|
| 155 | begin
|
|---|
| 156 | Width := AValue.X;
|
|---|
| 157 | Height := AValue.Y;
|
|---|
| 158 | end;
|
|---|
| 159 |
|
|---|
| 160 | procedure TRectangle.SetTop(const AValue: Integer);
|
|---|
| 161 | begin
|
|---|
| 162 | if FTop = AValue then Exit;
|
|---|
| 163 | if KeepSize then Inc(FBottom, AValue - FTop);
|
|---|
| 164 | FTop := AValue;
|
|---|
| 165 | end;
|
|---|
| 166 |
|
|---|
| 167 | procedure TRectangle.SetTopLeft(const AValue: TPoint);
|
|---|
| 168 | begin
|
|---|
| 169 | Left := AValue.X;
|
|---|
| 170 | Top := AValue.Y;
|
|---|
| 171 | end;
|
|---|
| 172 |
|
|---|
| 173 | procedure TRectangle.SetTopRight(const AValue: TPoint);
|
|---|
| 174 | begin
|
|---|
| 175 | Right := AValue.X;
|
|---|
| 176 | Top := AValue.Y;
|
|---|
| 177 | end;
|
|---|
| 178 |
|
|---|
| 179 | procedure TRectangle.SetTRect(const AValue: TRect);
|
|---|
| 180 | begin
|
|---|
| 181 | Left := AValue.Left;
|
|---|
| 182 | Top := AValue.Top;
|
|---|
| 183 | Bottom := AValue.Bottom;
|
|---|
| 184 | Right := AValue.Right;
|
|---|
| 185 | end;
|
|---|
| 186 |
|
|---|
| 187 | procedure TRectangle.SetWidth(const AValue: Integer);
|
|---|
| 188 | begin
|
|---|
| 189 | Right := Left + AValue;
|
|---|
| 190 | end;
|
|---|
| 191 |
|
|---|
| 192 | procedure TRectangle.Assign(Source: TRectangle);
|
|---|
| 193 | begin
|
|---|
| 194 | Left := Source.Left;
|
|---|
| 195 | Top := Source.Top;
|
|---|
| 196 | Right := Source.Right;
|
|---|
| 197 | Bottom := Source.Bottom;
|
|---|
| 198 | end;
|
|---|
| 199 |
|
|---|
| 200 | function TRectangle.IsInside(Pos: TPoint): Boolean;
|
|---|
| 201 | begin
|
|---|
| 202 | Result := (Pos.X >= Left) and (Pos.Y >= Top) and
|
|---|
| 203 | (Pos.X <= Right) and (Pos.Y <= Bottom);
|
|---|
| 204 | end;
|
|---|
| 205 |
|
|---|
| 206 | end.
|
|---|
| 207 |
|
|---|