1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Geometry._2d.Integer32.Rectangle;
interface
uses
Boolean,
Geometry._2d.Dimension,
Geometry._2d.Integer32.Point,
Geometry._2d.Integer32.Size,
Integer._32,
Integer._32.Interval,
Text;
type
TInteger32Rectangle = record
private
function GetTopLeft(): TInteger32Point;
procedure SetTopLeft(const Value: TInteger32Point);
function GetTopRight(): TInteger32Point;
procedure SetTopRight(const Value: TInteger32Point);
function GetBottomRight(): TInteger32Point;
procedure SetBottomRight(const Value: TInteger32Point);
procedure SetRange(Dimension: T2dDimension; const Value: TInteger32Interval);
function GetRange(Dimension: T2dDimension): TInteger32Interval;
function GetBottomLeft(): TInteger32Point;
procedure SetBottomLeft(const Value: TInteger32Point);
function GetSize(): TInteger32Size;
procedure SetSize(const Value: TInteger32Size);
function GetWidth(): TInteger32;
procedure SetWidth(const Value: TInteger32);
function GetHeight(): TInteger32;
procedure SetHeight(const Value: TInteger32);
public
Left: TInteger32;
Top: TInteger32;
Right: TInteger32;
Bottom: TInteger32;
function Inset(Value: TInteger32): TInteger32Rectangle; overload;
function InsetVertically(Value: TInteger32): TInteger32Rectangle;
function InsetHorizontally(Value: TInteger32): TInteger32Rectangle;
function Inset(const Insets: TInteger32Rectangle): TInteger32Rectangle; overload;
function Outset(Value: TInteger32): TInteger32Rectangle; overload;
function Outset(const Insets: TInteger32Rectangle): TInteger32Rectangle; overload;
function Offset(const Offset: TInteger32Point): TInteger32Rectangle;
function MoveTo(const TopLeft: TInteger32Point): TInteger32Rectangle;
function Resize(const Size: TInteger32Size): TInteger32Rectangle;
function Absolute(): TInteger32Rectangle;
function Union(const OtherRectangle: TInteger32Rectangle): TInteger32Rectangle;
function Intersect(const OtherRectangle: TInteger32Rectangle): TInteger32Rectangle;
function CenterIn(const Bounds: TInteger32Rectangle): TInteger32Point;
function GetPoints(): TArray<TInteger32Point>;
function ToText(): TText;
function GetArea(): TInteger32;
function IsValid(): TBoolean;
function Contains(const Point: TInteger32Point): TBoolean; overload;
function Contains(const Rectangle: TInteger32Rectangle): TBoolean; overload;
function OverlapsWith(const Rectangle: TInteger32Rectangle): TBoolean;
function Center(): TInteger32Point;
property TopLeft: TInteger32Point read GetTopLeft write SetTopLeft;
property TopRight: TInteger32Point read GetTopRight write SetTopRight;
property BottomLeft: TInteger32Point read GetBottomLeft write SetBottomLeft;
property BottomRight: TInteger32Point read GetBottomRight write SetBottomRight;
property Range[Dimension: T2dDimension]: TInteger32Interval read GetRange write SetRange;
property Size: TInteger32Size read GetSize write SetSize;
property Width: TInteger32 read GetWidth write SetWidth;
property Height: TInteger32 read GetHeight write SetHeight;
public
class var
Null: TInteger32Rectangle;
Largest: TInteger32Rectangle;
class procedure Initialize(); static;
class function New(Left, Top, Right, Bottom: TInteger32): TInteger32Rectangle; overload; static;
class function New(const TopLeft: TInteger32Point; const BottomRight: TInteger32Point): TInteger32Rectangle; overload; static;
class function New(const Position: TInteger32Point; const Size: TInteger32Size): TInteger32Rectangle; overload; static;
class function New(const Columns: TInteger32Interval; const Rows: TInteger32Interval): TInteger32Rectangle; overload; static;
class function NewBoundingRectangle(const Points: array of TInteger32Point): TInteger32Rectangle; static;
class function NewRow(const Columns: TInteger32Interval; Top: TInteger32): TInteger32Rectangle; static;
class operator Implicit(const Position: TInteger32Point): TInteger32Rectangle;
class operator Implicit(const Size: TInteger32Size): TInteger32Rectangle;
class operator NotEqual(const Left: TInteger32Rectangle; const Right: TInteger32Rectangle): TBoolean;
end;
implementation
uses
Exception.Enumeration,
Integer._32.TextFormat,
Mathematics;
{ TInteger32Rectangle }
class function TInteger32Rectangle.New(Left, Top, Right, Bottom: TInteger32): TInteger32Rectangle;
begin
Result.Left := Left;
Result.Top := Top;
Result.Right := Right;
Result.Bottom := Bottom;
end;
function TInteger32Rectangle.Absolute: TInteger32Rectangle;
begin
if Left < Right then
begin
Result.Left := Left;
Result.Right := Right;
end
else
begin
Result.Left := Right;
Result.Right := Left;
end;
if Top < Bottom then
begin
Result.Top := Top;
Result.Bottom := Bottom;
end
else
begin
Result.Top := Bottom;
Result.Bottom := Top;
end;
end;
function TInteger32Rectangle.Contains(const Point: TInteger32Point): TBoolean;
begin
Result :=
(Point.X >= Left) and (Point.X <= Right) and
(Point.Y >= Top) and (Point.Y <= Bottom);
end;
function TInteger32Rectangle.Center(): TInteger32Point;
begin
Result.X := (Left + Right) div 2;
Result.Y := (Bottom + Top) div 2;
end;
function TInteger32Rectangle.CenterIn(const Bounds: TInteger32Rectangle): TInteger32Point;
begin
Result := Bounds.TopLeft + TInteger32Point.New((Bounds.Width - Width) div 2, (Bounds.Height - Height) div 2);
end;
function TInteger32Rectangle.Contains(const Rectangle: TInteger32Rectangle): TBoolean;
begin
Result :=
(Rectangle.Left >= Self.Left)
and (Rectangle.Right <= Self.Right)
and (Rectangle.Top >= Self.Top)
and (Rectangle.Bottom <= Self.Bottom);
end;
function TInteger32Rectangle.GetPoints(): TArray<TInteger32Point>;
begin
SetLength(Result, 4);
Result[0] := TInteger32Point.New(Left, Top);
Result[1] := TInteger32Point.New(Right, Top);
Result[2] := TInteger32Point.New(Right, Bottom);
Result[3] := TInteger32Point.New(Left, Bottom);
end;
function TInteger32Rectangle.GetRange(Dimension: T2dDimension): TInteger32Interval;
begin
case Dimension of
T2dDimension.Horizontal:
Result := TInteger32Interval.New(Left, Right);
T2dDimension.Vertical:
Result := TInteger32Interval.New(Top, Bottom);
else
raise EUnknownEnumerationValue.Create('Unknown dimension');
end;
end;
function TInteger32Rectangle.GetSize(): TInteger32Size;
begin
Result.Width := Width;
Result.Height := Height;
end;
function TInteger32Rectangle.GetTopLeft(): TInteger32Point;
begin
Result := TInteger32Point.New(Left, Top);
end;
function TInteger32Rectangle.GetTopRight(): TInteger32Point;
begin
Result := TInteger32Point.New(Right, Top);
end;
function TInteger32Rectangle.GetWidth(): TInteger32;
begin
Result := Right - Left + 1;
end;
function TInteger32Rectangle.GetHeight(): TInteger32;
begin
Result := Bottom - Top + 1;
end;
function TInteger32Rectangle.GetArea(): TInteger32;
var
Size: TInteger32Size;
begin
Size := Self.Size;
Result := Size.Width * Size.Height;
end;
function TInteger32Rectangle.GetBottomLeft(): TInteger32Point;
begin
Result := TInteger32Point.New(Left, Bottom);
end;
function TInteger32Rectangle.GetBottomRight(): TInteger32Point;
begin
Result := TInteger32Point.New(Right, Bottom);
end;
function TInteger32Rectangle.Inset(Value: TInteger32): TInteger32Rectangle;
begin
Result.Left := Left + Value;
Result.Right := Right - Value;
Result.Top := Top + Value;
Result.Bottom := Bottom - Value;
end;
class procedure TInteger32Rectangle.Initialize();
begin
Null := TInteger32Rectangle.New(High(TInteger32), High(TInteger32), Low(TInteger32), Low(TInteger32));
Largest := TInteger32Rectangle.New(Low(TInteger32), Low(TInteger32), High(TInteger32), High(TInteger32));
end;
function TInteger32Rectangle.Inset(const Insets: TInteger32Rectangle): TInteger32Rectangle;
begin
Result.Left := Left + Insets.Left;
Result.Top := Top + Insets.Top;
Result.Right := Right - Insets.Right;
Result.Bottom := Bottom - Insets.Bottom;
end;
function TInteger32Rectangle.InsetHorizontally(Value: TInteger32): TInteger32Rectangle;
begin
Result.Left := Left + Value;
Result.Top := Top;
Result.Right := Right - Value;
Result.Bottom := Bottom;
end;
function TInteger32Rectangle.InsetVertically(Value: TInteger32): TInteger32Rectangle;
begin
Result.Left := Left;
Result.Top := Top + Value;
Result.Right := Right;
Result.Bottom := Bottom - Value;
end;
function TInteger32Rectangle.Intersect(const OtherRectangle: TInteger32Rectangle): TInteger32Rectangle;
begin
Result.Left := Maximum(Left, OtherRectangle.Left);
Result.Top := Maximum(Top, OtherRectangle.Top);
Result.Right := Minimum(Right, OtherRectangle.Right);
Result.Bottom := Minimum(Bottom, OtherRectangle.Bottom);
end;
function TInteger32Rectangle.IsValid(): TBoolean;
begin
Result := (Left <= Right) and (Top <= Bottom);
end;
function TInteger32Rectangle.MoveTo(const TopLeft: TInteger32Point): TInteger32Rectangle;
begin
Result.BottomRight := BottomRight + (TopLeft - Self.TopLeft);
Result.TopLeft := TopLeft;
end;
class function TInteger32Rectangle.New(const TopLeft: TInteger32Point; const BottomRight: TInteger32Point): TInteger32Rectangle;
begin
Result.TopLeft := TopLeft;
Result.BottomRight := BottomRight;
end;
class operator TInteger32Rectangle.Implicit(const Size: TInteger32Size): TInteger32Rectangle;
begin
Result := New(0, 0, Size.Width - 1, Size.Height - 1);
end;
class function TInteger32Rectangle.New(const Position: TInteger32Point; const Size: TInteger32Size): TInteger32Rectangle;
begin
Result.TopLeft := Position;
Result.Right := Result.Left + Size.Width - 1;
Result.Bottom := Result.Top + Size.Height - 1;
end;
class operator TInteger32Rectangle.Implicit(const Position: TInteger32Point): TInteger32Rectangle;
begin
Result.TopLeft := Position;
Result.BottomRight := Position;
end;
class function TInteger32Rectangle.New(const Columns: TInteger32Interval; const Rows: TInteger32Interval): TInteger32Rectangle;
begin
Result := New(Columns.Low, Rows.Low, Columns.High, Rows.High);
end;
class function TInteger32Rectangle.NewBoundingRectangle(const Points: array of TInteger32Point): TInteger32Rectangle;
var
i: TInteger32;
Point: TInteger32Point;
begin
Result.TopLeft := Points[0];
Result.BottomRight := Points[0];
for i := 1 to High(Points) do
begin
Point := Points[i];
Result.Left := Minimum(Result.Left, Point.X);
Result.Right := Maximum(Result.Right, Point.X);
Result.Top := Minimum(Result.Top, Point.Y);
Result.Bottom := Maximum(Result.Bottom, Point.Y);
end;
end;
class function TInteger32Rectangle.NewRow(const Columns: TInteger32Interval; Top: TInteger32): TInteger32Rectangle;
begin
Result := New(Columns.Low, Top, Columns.High, Top);
end;
class operator TInteger32Rectangle.NotEqual(const Left: TInteger32Rectangle; const Right: TInteger32Rectangle): TBoolean;
begin
Result :=
(Left.Left <> Right.Left)
or (Left.Top <> Right.Top)
or (Left.Right <> Right.Right)
or (Left.Bottom <> Right.Bottom);
end;
function TInteger32Rectangle.Offset(const Offset: TInteger32Point): TInteger32Rectangle;
begin
Result.Left := Left + Offset.X;
Result.Right := Right + Offset.X;
Result.Top := Top + Offset.Y;
Result.Bottom := Bottom + Offset.Y;
end;
function TInteger32Rectangle.Outset(const Insets: TInteger32Rectangle): TInteger32Rectangle;
begin
Result.Left := Left - Insets.Left;
Result.Top := Top - Insets.Top;
Result.Right := Right + Insets.Right;
Result.Bottom := Bottom + Insets.Bottom;
end;
function TInteger32Rectangle.Outset(Value: TInteger32): TInteger32Rectangle;
begin
Result.Left := Left - Value;
Result.Right := Right + Value;
Result.Top := Top - Value;
Result.Bottom := Bottom + Value;
end;
function TInteger32Rectangle.OverlapsWith(const Rectangle: TInteger32Rectangle): TBoolean;
var
Intersection: TInteger32Rectangle;
begin
Intersection := Intersect(Rectangle);
Result := Intersection.IsValid();
end;
function TInteger32Rectangle.Resize(const Size: TInteger32Size): TInteger32Rectangle;
begin
Result.TopLeft := TopLeft;
Result.Size := Size;
end;
procedure TInteger32Rectangle.SetBottomLeft(const Value: TInteger32Point);
begin
Left := Value.X;
Bottom := Value.Y;
end;
procedure TInteger32Rectangle.SetBottomRight(const Value: TInteger32Point);
begin
Right := Value.X;
Bottom := Value.Y;
end;
procedure TInteger32Rectangle.SetHeight(const Value: TInteger32);
begin
Bottom := Top + Value - 1;
end;
procedure TInteger32Rectangle.SetRange(Dimension: T2dDimension; const Value: TInteger32Interval);
begin
case Dimension of
T2dDimension.Horizontal:
begin
Left := Value.Low;
Right := Value.High;
end;
T2dDimension.Vertical:
begin
Top := Value.Low;
Bottom := Value.High;
end;
else
raise EUnknownEnumerationValue.Create('Unknown dimension');
end;
end;
procedure TInteger32Rectangle.SetSize(const Value: TInteger32Size);
begin
Right := Left + Value.Width - 1;
Bottom := Top + Value.Height - 1;
end;
procedure TInteger32Rectangle.SetTopLeft(const Value: TInteger32Point);
begin
Left := Value.X;
Top := Value.Y;
end;
procedure TInteger32Rectangle.SetTopRight(const Value: TInteger32Point);
begin
Right := Value.X;
Top := Value.Y;
end;
procedure TInteger32Rectangle.SetWidth(const Value: TInteger32);
begin
Right := Left + Value - 1;
end;
function TInteger32Rectangle.ToText(): TText;
begin
Result :=
'[ ' +
Integer32ToText(Left) + ', ' +
Integer32ToText(Top) + ', ' +
Integer32ToText(Right) + ', ' +
Integer32ToText(Bottom) +
' ]';
end;
function TInteger32Rectangle.Union(const OtherRectangle: TInteger32Rectangle): TInteger32Rectangle;
begin
Result.Left := Minimum(Left, OtherRectangle.Left);
Result.Top := Minimum(Top, OtherRectangle.Top);
Result.Right := Maximum(Right, OtherRectangle.Right);
Result.Bottom := Maximum(Bottom, OtherRectangle.Bottom);
end;
initialization
TInteger32Rectangle.Initialize();
end.
|