| 1 | {$IFDEF INTERFACE}
|
|---|
| 2 |
|
|---|
| 3 | // TGRectangle<TGRectangleDimension> = class
|
|---|
| 4 | TGRectangle = class
|
|---|
| 5 | private
|
|---|
| 6 | function GetBottomLeft: TGRectanglePoint;
|
|---|
| 7 | function GetBottomRight: TGRectanglePoint;
|
|---|
| 8 | function GetHeight: TGRectangleDimension;
|
|---|
| 9 | function GetSize: TGRectanglePoint;
|
|---|
| 10 | function GetTopLeft: TGRectanglePoint;
|
|---|
| 11 | function GetTopRight: TGRectanglePoint;
|
|---|
| 12 | function GetWidth: TGRectangleDimension;
|
|---|
| 13 | function GetEmpty: Boolean;
|
|---|
| 14 | procedure SetBottom(const AValue: TGRectangleDimension);
|
|---|
| 15 | procedure SetBottomLeft(const AValue: TGRectanglePoint);
|
|---|
| 16 | procedure SetBottomRight(const AValue: TGRectanglePoint);
|
|---|
| 17 | procedure SetHeight(const AValue: TGRectangleDimension);
|
|---|
| 18 | procedure SetLeft(const AValue: TGRectangleDimension);
|
|---|
| 19 | procedure SetRight(const AValue: TGRectangleDimension);
|
|---|
| 20 | procedure SetSize(const AValue: TGRectanglePoint);
|
|---|
| 21 | procedure SetTop(const AValue: TGRectangleDimension);
|
|---|
| 22 | procedure SetTopLeft(const AValue: TGRectanglePoint);
|
|---|
| 23 | procedure SetTopRight(const AValue: TGRectanglePoint);
|
|---|
| 24 | procedure SetWidth(const AValue: TGRectangleDimension);
|
|---|
| 25 | procedure SetEmpty(const AValue: Boolean);
|
|---|
| 26 | public
|
|---|
| 27 | FLeft: TGRectangleDimension;
|
|---|
| 28 | FTop: TGRectangleDimension;
|
|---|
| 29 | FRight: TGRectangleDimension;
|
|---|
| 30 | FBottom: TGRectangleDimension;
|
|---|
| 31 | KeepSize: Boolean;
|
|---|
| 32 |
|
|---|
| 33 | procedure Assign(Source: TGRectangle);
|
|---|
| 34 | function IsPointInside(Pos: TGRectanglePoint): Boolean;
|
|---|
| 35 | function IsRectInside(Rect: TGRectangle): Boolean;
|
|---|
| 36 | procedure Intersect(Rect1, Rect2: TGRectangle);
|
|---|
| 37 | procedure IntersectWith(Rect: TGRectangle);
|
|---|
| 38 | procedure Union(Rect1, Rect2: TGRectangle);
|
|---|
| 39 | procedure UnionWith(Rect: TGRectangle);
|
|---|
| 40 |
|
|---|
| 41 | procedure SetRect(Left, Top, Width, Height: TGRectangleDimension);
|
|---|
| 42 | procedure SetBounds(Left, Top, Right, Bottom: TGRectangleDimension);
|
|---|
| 43 |
|
|---|
| 44 | property Left: TGRectangleDimension read FLeft write SetLeft;
|
|---|
| 45 | property Top: TGRectangleDimension read FTop write SetTop;
|
|---|
| 46 | property Right: TGRectangleDimension read FRight write SetRight;
|
|---|
| 47 | property Bottom: TGRectangleDimension read FBottom write SetBottom;
|
|---|
| 48 |
|
|---|
| 49 | property Width: TGRectangleDimension read GetWidth write SetWidth;
|
|---|
| 50 | property Height: TGRectangleDimension read GetHeight write SetHeight;
|
|---|
| 51 |
|
|---|
| 52 | property TopLeft: TGRectanglePoint read GetTopLeft write SetTopLeft;
|
|---|
| 53 | property TopRight: TGRectanglePoint read GetTopRight write SetTopRight;
|
|---|
| 54 | property BottomLeft: TGRectanglePoint read GetBottomLeft write SetBottomLeft;
|
|---|
| 55 | property BottomRight: TGRectanglePoint read GetBottomRight write SetBottomRight;
|
|---|
| 56 |
|
|---|
| 57 | property Size: TGRectanglePoint read GetSize write SetSize;
|
|---|
| 58 | property Empty: Boolean read GetEmpty write SetEmpty;
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | {$UNDEF INTERFACE}
|
|---|
| 63 | {$ENDIF}
|
|---|
| 64 |
|
|---|
| 65 | {$IFDEF IMPLEMENTATION_USES}
|
|---|
| 66 |
|
|---|
| 67 | uses Math;
|
|---|
| 68 |
|
|---|
| 69 | {$UNDEF IMPLEMENTATION_USES}
|
|---|
| 70 | {$ENDIF}
|
|---|
| 71 |
|
|---|
| 72 | {$IFDEF IMPLEMENTATION}
|
|---|
| 73 |
|
|---|
| 74 | { TGRectangle }
|
|---|
| 75 |
|
|---|
| 76 | function TGRectangle.GetBottomLeft: TGRectanglePoint;
|
|---|
| 77 | begin
|
|---|
| 78 | Result.X := Left;
|
|---|
| 79 | Result.Y := Bottom;
|
|---|
| 80 | end;
|
|---|
| 81 |
|
|---|
| 82 | function TGRectangle.GetBottomRight: TGRectanglePoint;
|
|---|
| 83 | begin
|
|---|
| 84 | Result.X := Right;
|
|---|
| 85 | Result.Y := Bottom;
|
|---|
| 86 | end;
|
|---|
| 87 |
|
|---|
| 88 | function TGRectangle.GetHeight: TGRectangleDimension;
|
|---|
| 89 | begin
|
|---|
| 90 | Result := Bottom - Top;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | function TGRectangle.GetSize: TGRectanglePoint;
|
|---|
| 94 | begin
|
|---|
| 95 | Result.X := Width;
|
|---|
| 96 | Result.Y := Height;
|
|---|
| 97 | end;
|
|---|
| 98 |
|
|---|
| 99 | function TGRectangle.GetTopLeft: TGRectanglePoint;
|
|---|
| 100 | begin
|
|---|
| 101 | Result.X := Left;
|
|---|
| 102 | Result.Y := Top;
|
|---|
| 103 | end;
|
|---|
| 104 |
|
|---|
| 105 | function TGRectangle.GetTopRight: TGRectanglePoint;
|
|---|
| 106 | begin
|
|---|
| 107 | Result.X := Right;
|
|---|
| 108 | Result.Y := Top;
|
|---|
| 109 | end;
|
|---|
| 110 |
|
|---|
| 111 | function TGRectangle.GetWidth: TGRectangleDimension;
|
|---|
| 112 | begin
|
|---|
| 113 | Result := Right - Left;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure TGRectangle.SetBottom(const AValue: TGRectangleDimension);
|
|---|
| 117 | begin
|
|---|
| 118 | if FBottom = AValue then exit;
|
|---|
| 119 | if KeepSize then FTop := FTop + (AValue - FBottom);
|
|---|
| 120 | FBottom := AValue;
|
|---|
| 121 | end;
|
|---|
| 122 |
|
|---|
| 123 | procedure TGRectangle.SetBottomLeft(const AValue: TGRectanglePoint);
|
|---|
| 124 | begin
|
|---|
| 125 | Left := AValue.X;
|
|---|
| 126 | Bottom := AValue.Y;
|
|---|
| 127 | end;
|
|---|
| 128 |
|
|---|
| 129 | procedure TGRectangle.SetBottomRight(const AValue: TGRectanglePoint);
|
|---|
| 130 | begin
|
|---|
| 131 | Right := AValue.X;
|
|---|
| 132 | Bottom := AValue.Y;
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | procedure TGRectangle.SetHeight(const AValue: TGRectangleDimension);
|
|---|
| 136 | begin
|
|---|
| 137 | Bottom := Top + AValue;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | procedure TGRectangle.SetLeft(const AValue: TGRectangleDimension);
|
|---|
| 141 | begin
|
|---|
| 142 | if FLeft = AValue then Exit;
|
|---|
| 143 | if KeepSize then FRight := FRight + (AValue - FLeft);
|
|---|
| 144 | FLeft := AValue;
|
|---|
| 145 | end;
|
|---|
| 146 |
|
|---|
| 147 | procedure TGRectangle.SetRight(const AValue: TGRectangleDimension);
|
|---|
| 148 | begin
|
|---|
| 149 | if FRight = AValue then Exit;
|
|---|
| 150 | if KeepSize then FLeft := FLeft + (AValue - FRight);
|
|---|
| 151 | FRight := AValue;
|
|---|
| 152 | end;
|
|---|
| 153 |
|
|---|
| 154 | procedure TGRectangle.SetSize(const AValue: TGRectanglePoint);
|
|---|
| 155 | begin
|
|---|
| 156 | Width := AValue.X;
|
|---|
| 157 | Height := AValue.Y;
|
|---|
| 158 | end;
|
|---|
| 159 |
|
|---|
| 160 | procedure TGRectangle.SetTop(const AValue: TGRectangleDimension);
|
|---|
| 161 | begin
|
|---|
| 162 | if FTop = AValue then Exit;
|
|---|
| 163 | if KeepSize then FBottom := FBottom + (AValue - FTop);
|
|---|
| 164 | FTop := AValue;
|
|---|
| 165 | end;
|
|---|
| 166 |
|
|---|
| 167 | procedure TGRectangle.SetTopLeft(const AValue: TGRectanglePoint);
|
|---|
| 168 | begin
|
|---|
| 169 | Left := AValue.X;
|
|---|
| 170 | Top := AValue.Y;
|
|---|
| 171 | end;
|
|---|
| 172 |
|
|---|
| 173 | procedure TGRectangle.SetTopRight(const AValue: TGRectanglePoint);
|
|---|
| 174 | begin
|
|---|
| 175 | Right := AValue.X;
|
|---|
| 176 | Top := AValue.Y;
|
|---|
| 177 | end;
|
|---|
| 178 |
|
|---|
| 179 | procedure TGRectangle.SetWidth(const AValue: TGRectangleDimension);
|
|---|
| 180 | begin
|
|---|
| 181 | Right := Left + AValue;
|
|---|
| 182 | end;
|
|---|
| 183 |
|
|---|
| 184 | procedure TGRectangle.Assign(Source: TGRectangle);
|
|---|
| 185 | begin
|
|---|
| 186 | Left := Source.Left;
|
|---|
| 187 | Top := Source.Top;
|
|---|
| 188 | Right := Source.Right;
|
|---|
| 189 | Bottom := Source.Bottom;
|
|---|
| 190 | KeepSize := Source.KeepSize;
|
|---|
| 191 | end;
|
|---|
| 192 |
|
|---|
| 193 | function TGRectangle.IsPointInside(Pos: TGRectanglePoint): Boolean;
|
|---|
| 194 | begin
|
|---|
| 195 | Result := (Pos.X >= Left) and (Pos.Y >= Top) and
|
|---|
| 196 | (Pos.X <= Right) and (Pos.Y <= Bottom);
|
|---|
| 197 | end;
|
|---|
| 198 |
|
|---|
| 199 | function TGRectangle.IsRectInside(Rect: TGRectangle): Boolean;
|
|---|
| 200 | begin
|
|---|
| 201 | Result := (Rect.Left >= Left) and (Rect.Top >= Top) and
|
|---|
| 202 | (Rect.Right <= Right) and (Rect.Bottom <= Bottom);
|
|---|
| 203 | end;
|
|---|
| 204 |
|
|---|
| 205 | procedure TGRectangle.Intersect(Rect1, Rect2: TGRectangle);
|
|---|
| 206 | begin
|
|---|
| 207 | if Rect1.Empty or Rect2.Empty then Empty := True
|
|---|
| 208 | else begin
|
|---|
| 209 | Left := Max(Rect1.Left, Rect2.Left);
|
|---|
| 210 | Top := Max(Rect1.Top, Rect2.Top);
|
|---|
| 211 | Right := Min(Rect1.Right, Rect2.Right);
|
|---|
| 212 | Bottom := Min(Rect1.Bottom, Rect2.Bottom);
|
|---|
| 213 | end;
|
|---|
| 214 | end;
|
|---|
| 215 |
|
|---|
| 216 | procedure TGRectangle.IntersectWith(Rect: TGRectangle);
|
|---|
| 217 | begin
|
|---|
| 218 | if Empty or Rect.Empty then Empty := True
|
|---|
| 219 | else begin
|
|---|
| 220 | Left := Max(Left, Rect.Left);
|
|---|
| 221 | Top := Max(Top, Rect.Top);
|
|---|
| 222 | Right := Min(Right, Rect.Right);
|
|---|
| 223 | Bottom := Min(Bottom, Rect.Bottom);
|
|---|
| 224 | end;
|
|---|
| 225 | end;
|
|---|
| 226 |
|
|---|
| 227 | procedure TGRectangle.Union(Rect1, Rect2: TGRectangle);
|
|---|
| 228 | begin
|
|---|
| 229 | if Rect1.Empty then Assign(Rect2)
|
|---|
| 230 | else
|
|---|
| 231 | if Rect2.Empty then Assign(Rect1)
|
|---|
| 232 | else begin
|
|---|
| 233 | Left := Min(Rect1.Left, Rect2.Left);
|
|---|
| 234 | Top := Min(Rect1.Top, Rect2.Top);
|
|---|
| 235 | Right := Max(Rect1.Right, Rect2.Right);
|
|---|
| 236 | Bottom := Max(Rect1.Bottom, Rect2.Bottom);
|
|---|
| 237 | end;
|
|---|
| 238 | end;
|
|---|
| 239 |
|
|---|
| 240 | procedure TGRectangle.UnionWith(Rect: TGRectangle);
|
|---|
| 241 | begin
|
|---|
| 242 | if Empty then Assign(Rect)
|
|---|
| 243 | else
|
|---|
| 244 | if not Rect.Empty then begin
|
|---|
| 245 | Left := Min(Left, Rect.Left);
|
|---|
| 246 | Top := Min(Top, Rect.Top);
|
|---|
| 247 | Right := Max(Right, Rect.Right);
|
|---|
| 248 | Bottom := Max(Bottom, Rect.Bottom);
|
|---|
| 249 | end;
|
|---|
| 250 | end;
|
|---|
| 251 |
|
|---|
| 252 | procedure TGRectangle.SetRect(Left, Top, Width, Height: TGRectangleDimension);
|
|---|
| 253 | begin
|
|---|
| 254 | Self.Left := Left;
|
|---|
| 255 | Self.Top := Top;
|
|---|
| 256 | Self.Width := Width;
|
|---|
| 257 | Self.Height := Height;
|
|---|
| 258 | end;
|
|---|
| 259 |
|
|---|
| 260 | procedure TGRectangle.SetBounds(Left, Top, Right, Bottom: TGRectangleDimension);
|
|---|
| 261 | begin
|
|---|
| 262 | Self.Left := Left;
|
|---|
| 263 | Self.Top := Top;
|
|---|
| 264 | Self.Right := Right;
|
|---|
| 265 | Self.Bottom := Bottom;
|
|---|
| 266 | end;
|
|---|
| 267 |
|
|---|
| 268 | function TGRectangle.GetEmpty: Boolean;
|
|---|
| 269 | begin
|
|---|
| 270 | Result := (Bottom <= Top) or (Right <= Left);
|
|---|
| 271 | end;
|
|---|
| 272 |
|
|---|
| 273 | procedure TGRectangle.SetEmpty(const AValue: Boolean);
|
|---|
| 274 | begin
|
|---|
| 275 | Top := 0;
|
|---|
| 276 | Bottom := 0;
|
|---|
| 277 | Left := 0;
|
|---|
| 278 | Right := 0;
|
|---|
| 279 | end;
|
|---|
| 280 |
|
|---|
| 281 | {$UNDEF IMPLEMENTATION}
|
|---|
| 282 | {$ENDIF}
|
|---|
| 283 |
|
|---|