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