source: trunk/Packages/TemplateGenerics/Generic/GenericRectangle.inc

Last change on this file was 1, checked in by chronos, 8 years ago
  • Imported initial version.
File size: 7.5 KB
Line 
1{$IFDEF INTERFACE}
2
3// TGRectangle<TGRectangleDimension> = class
4TGRectangle = class
5private
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);
26public
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;
59end;
60
61
62{$UNDEF INTERFACE}
63{$ENDIF}
64
65{$IFDEF IMPLEMENTATION_USES}
66
67uses Math;
68
69{$UNDEF IMPLEMENTATION_USES}
70{$ENDIF}
71
72{$IFDEF IMPLEMENTATION}
73
74{ TGRectangle }
75
76function TGRectangle.GetBottomLeft: TGRectanglePoint;
77begin
78 Result.X := Left;
79 Result.Y := Bottom;
80end;
81
82function TGRectangle.GetBottomRight: TGRectanglePoint;
83begin
84 Result.X := Right;
85 Result.Y := Bottom;
86end;
87
88function TGRectangle.GetHeight: TGRectangleDimension;
89begin
90 Result := Bottom - Top;
91end;
92
93function TGRectangle.GetSize: TGRectanglePoint;
94begin
95 Result.X := Width;
96 Result.Y := Height;
97end;
98
99function TGRectangle.GetTopLeft: TGRectanglePoint;
100begin
101 Result.X := Left;
102 Result.Y := Top;
103end;
104
105function TGRectangle.GetTopRight: TGRectanglePoint;
106begin
107 Result.X := Right;
108 Result.Y := Top;
109end;
110
111function TGRectangle.GetWidth: TGRectangleDimension;
112begin
113 Result := Right - Left;
114end;
115
116procedure TGRectangle.SetBottom(const AValue: TGRectangleDimension);
117begin
118 if FBottom = AValue then exit;
119 if KeepSize then FTop := FTop + (AValue - FBottom);
120 FBottom := AValue;
121end;
122
123procedure TGRectangle.SetBottomLeft(const AValue: TGRectanglePoint);
124begin
125 Left := AValue.X;
126 Bottom := AValue.Y;
127end;
128
129procedure TGRectangle.SetBottomRight(const AValue: TGRectanglePoint);
130begin
131 Right := AValue.X;
132 Bottom := AValue.Y;
133end;
134
135procedure TGRectangle.SetHeight(const AValue: TGRectangleDimension);
136begin
137 Bottom := Top + AValue;
138end;
139
140procedure TGRectangle.SetLeft(const AValue: TGRectangleDimension);
141begin
142 if FLeft = AValue then Exit;
143 if KeepSize then FRight := FRight + (AValue - FLeft);
144 FLeft := AValue;
145end;
146
147procedure TGRectangle.SetRight(const AValue: TGRectangleDimension);
148begin
149 if FRight = AValue then Exit;
150 if KeepSize then FLeft := FLeft + (AValue - FRight);
151 FRight := AValue;
152end;
153
154procedure TGRectangle.SetSize(const AValue: TGRectanglePoint);
155begin
156 Width := AValue.X;
157 Height := AValue.Y;
158end;
159
160procedure TGRectangle.SetTop(const AValue: TGRectangleDimension);
161begin
162 if FTop = AValue then Exit;
163 if KeepSize then FBottom := FBottom + (AValue - FTop);
164 FTop := AValue;
165end;
166
167procedure TGRectangle.SetTopLeft(const AValue: TGRectanglePoint);
168begin
169 Left := AValue.X;
170 Top := AValue.Y;
171end;
172
173procedure TGRectangle.SetTopRight(const AValue: TGRectanglePoint);
174begin
175 Right := AValue.X;
176 Top := AValue.Y;
177end;
178
179procedure TGRectangle.SetWidth(const AValue: TGRectangleDimension);
180begin
181 Right := Left + AValue;
182end;
183
184procedure TGRectangle.Assign(Source: TGRectangle);
185begin
186 Left := Source.Left;
187 Top := Source.Top;
188 Right := Source.Right;
189 Bottom := Source.Bottom;
190 KeepSize := Source.KeepSize;
191end;
192
193function TGRectangle.IsPointInside(Pos: TGRectanglePoint): Boolean;
194begin
195 Result := (Pos.X >= Left) and (Pos.Y >= Top) and
196 (Pos.X <= Right) and (Pos.Y <= Bottom);
197end;
198
199function TGRectangle.IsRectInside(Rect: TGRectangle): Boolean;
200begin
201 Result := (Rect.Left >= Left) and (Rect.Top >= Top) and
202 (Rect.Right <= Right) and (Rect.Bottom <= Bottom);
203end;
204
205procedure TGRectangle.Intersect(Rect1, Rect2: TGRectangle);
206begin
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;
214end;
215
216procedure TGRectangle.IntersectWith(Rect: TGRectangle);
217begin
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;
225end;
226
227procedure TGRectangle.Union(Rect1, Rect2: TGRectangle);
228begin
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;
238end;
239
240procedure TGRectangle.UnionWith(Rect: TGRectangle);
241begin
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;
250end;
251
252procedure TGRectangle.SetRect(Left, Top, Width, Height: TGRectangleDimension);
253begin
254 Self.Left := Left;
255 Self.Top := Top;
256 Self.Width := Width;
257 Self.Height := Height;
258end;
259
260procedure TGRectangle.SetBounds(Left, Top, Right, Bottom: TGRectangleDimension);
261begin
262 Self.Left := Left;
263 Self.Top := Top;
264 Self.Right := Right;
265 Self.Bottom := Bottom;
266end;
267
268function TGRectangle.GetEmpty: Boolean;
269begin
270 Result := (Bottom <= Top) or (Right <= Left);
271end;
272
273procedure TGRectangle.SetEmpty(const AValue: Boolean);
274begin
275 Top := 0;
276 Bottom := 0;
277 Left := 0;
278 Right := 0;
279end;
280
281{$UNDEF IMPLEMENTATION}
282{$ENDIF}
283
Note: See TracBrowser for help on using the repository browser.