source: Generics/NativeGenerics/Generic/GenericRectangle.pas

Last change on this file was 496, checked in by chronos, 7 years ago
  • Modified: New native generics classes working under FPC 3.0 transformed from TemplateGenerics package.
File size: 7.2 KB
Line 
1unit GenericRectangle;
2
3{$mode delphi}
4
5interface
6
7uses
8 GenericPoint;
9
10type
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
71implementation
72
73uses
74 Math;
75
76
77{ TGRectangle }
78
79function TGRectangle<T>.Max(Value1, Value2: T): T;
80begin
81 if Value1 > Value2 then Result := Value1
82 else Result := Value2;
83end;
84
85function TGRectangle<T>.Min(Value1, Value2: T): T;
86begin
87 if Value1 < Value2 then Result := Value1
88 else Result := Value2;
89end;
90
91function TGRectangle<T>.GetBottomLeft: TGPoint<T>;
92begin
93 Result.X := Left;
94 Result.Y := Bottom;
95end;
96
97function TGRectangle<T>.GetBottomRight: TGPoint<T>;
98begin
99 Result.X := Right;
100 Result.Y := Bottom;
101end;
102
103function TGRectangle<T>.GetHeight: T;
104begin
105 Result := Bottom - Top;
106end;
107
108function TGRectangle<T>.GetSize: TGPoint<T>;
109begin
110 Result.X := Width;
111 Result.Y := Height;
112end;
113
114function TGRectangle<T>.GetTopLeft: TGPoint<T>;
115begin
116 Result.X := Left;
117 Result.Y := Top;
118end;
119
120function TGRectangle<T>.GetTopRight: TGPoint<T>;
121begin
122 Result.X := Right;
123 Result.Y := Top;
124end;
125
126function TGRectangle<T>.GetWidth: T;
127begin
128 Result := Right - Left;
129end;
130
131procedure TGRectangle<T>.SetBottom(const AValue: T);
132begin
133 if FBottom = AValue then exit;
134 if KeepSize then FTop := FTop + (AValue - FBottom);
135 FBottom := AValue;
136end;
137
138procedure TGRectangle<T>.SetBottomLeft(const AValue: TGPoint<T>);
139begin
140 Left := AValue.X;
141 Bottom := AValue.Y;
142end;
143
144procedure TGRectangle<T>.SetBottomRight(const AValue: TGPoint<T>);
145begin
146 Right := AValue.X;
147 Bottom := AValue.Y;
148end;
149
150procedure TGRectangle<T>.SetHeight(const AValue: T);
151begin
152 Bottom := Top + AValue;
153end;
154
155procedure TGRectangle<T>.SetLeft(const AValue: T);
156begin
157 if FLeft = AValue then Exit;
158 if KeepSize then FRight := FRight + (AValue - FLeft);
159 FLeft := AValue;
160end;
161
162procedure TGRectangle<T>.SetRight(const AValue: T);
163begin
164 if FRight = AValue then Exit;
165 if KeepSize then FLeft := FLeft + (AValue - FRight);
166 FRight := AValue;
167end;
168
169procedure TGRectangle<T>.SetSize(const AValue: TGPoint<T>);
170begin
171 Width := AValue.X;
172 Height := AValue.Y;
173end;
174
175procedure TGRectangle<T>.SetTop(const AValue: T);
176begin
177 if FTop = AValue then Exit;
178 if KeepSize then FBottom := FBottom + (AValue - FTop);
179 FTop := AValue;
180end;
181
182procedure TGRectangle<T>.SetTopLeft(const AValue: TGPoint<T>);
183begin
184 Left := AValue.X;
185 Top := AValue.Y;
186end;
187
188procedure TGRectangle<T>.SetTopRight(const AValue: TGPoint<T>);
189begin
190 Right := AValue.X;
191 Top := AValue.Y;
192end;
193
194procedure TGRectangle<T>.SetWidth(const AValue: T);
195begin
196 Right := Left + AValue;
197end;
198
199procedure TGRectangle<T>.Assign(Source: TGRectangle<T>);
200begin
201 Left := Source.Left;
202 Top := Source.Top;
203 Right := Source.Right;
204 Bottom := Source.Bottom;
205 KeepSize := Source.KeepSize;
206end;
207
208function TGRectangle<T>.IsPointInside(Pos: TGPoint<T>): Boolean;
209begin
210 Result := (Pos.X >= Left) and (Pos.Y >= Top) and
211 (Pos.X <= Right) and (Pos.Y <= Bottom);
212end;
213
214function TGRectangle<T>.IsRectInside(Rect: TGRectangle<T>): Boolean;
215begin
216 Result := (Rect.Left >= Left) and (Rect.Top >= Top) and
217 (Rect.Right <= Right) and (Rect.Bottom <= Bottom);
218end;
219
220procedure TGRectangle<T>.Intersect(Rect1, Rect2: TGRectangle<T>);
221begin
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;
229end;
230
231procedure TGRectangle<T>.IntersectWith(Rect: TGRectangle<T>);
232begin
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;
240end;
241
242procedure TGRectangle<T>.Union(Rect1, Rect2: TGRectangle<T>);
243begin
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;
253end;
254
255procedure TGRectangle<T>.UnionWith(Rect: TGRectangle<T>);
256begin
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;
265end;
266
267procedure TGRectangle<T>.SetRect(Left, Top, Width, Height: T);
268begin
269 Self.Left := Left;
270 Self.Top := Top;
271 Self.Width := Width;
272 Self.Height := Height;
273end;
274
275procedure TGRectangle<T>.SetBounds(Left, Top, Right, Bottom: T);
276begin
277 Self.Left := Left;
278 Self.Top := Top;
279 Self.Right := Right;
280 Self.Bottom := Bottom;
281end;
282
283function TGRectangle<T>.GetEmpty: Boolean;
284begin
285 Result := (Bottom <= Top) or (Right <= Left);
286end;
287
288procedure TGRectangle<T>.SetEmpty(const AValue: Boolean);
289begin
290 Top := 0;
291 Bottom := 0;
292 Left := 0;
293 Right := 0;
294end;
295
296end.
Note: See TracBrowser for help on using the repository browser.