summaryrefslogtreecommitdiff
path: root/plugins/Utils.pas/uRect.pas
blob: 6d383ef518e0f55d30fdf930c9b15d7f26ca9c93 (plain)
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
unit uRect;

interface

uses windows;

{
type
  PPoint = ^TPoint;
  TPoint = packed record
    X: Longint;
    Y: Longint;
  end;

  PSmallPoint = ^TSmallPoint;
  TSmallPoint = packed record
    x: SmallInt;
    y: SmallInt;
  end;

  PRect = ^TRect;
  TRect = packed record
    case Integer of
      0: (Left, Top, Right, Bottom: Longint);
      1: (TopLeft, BottomRight: TPoint);
  end;
}
{
-function SetRect(var lprc: TRect; xLeft, yTop, xRight, yBottom: Integer): BOOL; stdcall;
-function CopyRect(var lprcDst: TRect; const lprcSrc: TRect): BOOL; stdcall;
-function InflateRect(var lprc: TRect; dx, dy: Integer): BOOL; stdcall;
function SubtractRect(var lprcDst: TRect; const lprcSrc1, lprcSrc2: TRect): BOOL; stdcall;
-function SetRectEmpty(var lprc: TRect): BOOL; stdcall;
-function IntersectRect(var lprcDst: TRect; const lprcSrc1, lprcSrc2: TRect): BOOL; stdcall;
-function UnionRect(var lprcDst: TRect; const lprcSrc1, lprcSrc2: TRect): BOOL; stdcall;
-function OffsetRect(var lprc: TRect; dx, dy: Integer): BOOL; stdcall;
-function IsRectEmpty(const lprc: TRect): BOOL; stdcall;
-function EqualRect(const lprc1, lprc2: TRect): BOOL; stdcall;
-function PtInRect(const lprc: TRect; pt: TPoint): BOOL; stdcall;
}

function  Point(X, Y: Integer): TPoint; overload;
procedure Point(var pt:TPoint; X, Y: Integer); overload;

function SmallPoint(X, Y: Integer): TSmallPoint; overload;
function SmallPoint(XY: LongWord): TSmallPoint; overload;

function  CenterPoint(const Rect: TRect): TPoint; overload;
procedure CenterPoint(const Rect: TRect; var pt:TPoint); overload;
function  PointInRect(const P: TPoint; const Rect: TRect): Boolean;
function  PtInRect   (const Rect: TRect; const P: TPoint): Boolean;

function  Rect   (Left, Top, Right, Bottom: Integer): TRect; overload;
procedure Rect   (var Rect:TRect; Left, Top, Right, Bottom: Integer); overload;
procedure SetRect(var Rect:TRect; Left, Top, Right, Bottom: Integer);
function  CopyRect(var Rect: TRect; const R1: TRect):Boolean;
function  SetRectEmpty(var Rect: TRect): Boolean;

function  Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect; overload;
procedure Bounds(var Rect:TRect; ALeft, ATop, AWidth, AHeight: Integer); overload;

function EqualRect(const R1, R2: TRect): Boolean;
function IsRectInside(const R1, R2: TRect): Boolean;
function IsRectEmpty(const Rect: TRect): Boolean;
function InflateRect(var Rect: TRect; dx, dy: Integer): Boolean;
function OffsetRect(var Rect: TRect; DX: Integer; DY: Integer): Boolean;
function IntersectRect(var Rect: TRect; const R1, R2: TRect): Boolean; overload;
function IntersectRect(const R1, R2: TRect): Boolean; overload;
function UnionRect(var Rect: TRect; const R1, R2: TRect): Boolean;


implementation

function Point(X, Y: Integer): TPoint;
begin
  Result.X := X;
  Result.Y := Y;
end;

procedure Point(var pt:TPoint; X, Y: Integer);
begin
  pt.X := X;
  pt.Y := Y;
end;

function SmallPoint(X, Y: Integer): TSmallPoint;
begin
  Result.X := X;
  Result.Y := Y;
end;

function SmallPoint(XY: LongWord): TSmallPoint;
begin
  Result.X := SmallInt(XY and $0000FFFF);
  Result.Y := SmallInt(XY shr 16);
end;

function PointInRect(const P: TPoint; const Rect: TRect): Boolean;
begin
  Result :=
    (P.X >= Rect.Left)  and
    (P.X <  Rect.Right) and
    (P.Y >= Rect.Top)   and
    (P.Y <  Rect.Bottom);
end;

function PtInRect(const Rect: TRect; const P: TPoint): Boolean;
begin
  Result :=
    (P.X >= Rect.Left)  and
    (P.X <  Rect.Right) and
    (P.Y >= Rect.Top)   and
    (P.Y <  Rect.Bottom);
end;

function CenterPoint(const Rect: TRect): TPoint;
begin
  with Rect do
  begin
    Result.X := (Right  - Left) div 2 + Left;
    Result.Y := (Bottom - Top ) div 2 + Top;
  end;
end;

procedure CenterPoint(const Rect: TRect; var pt:TPoint);
begin
  with Rect do
  begin
    pt.X := (Right  - Left) div 2 + Left;
    pt.Y := (Bottom - Top ) div 2 + Top;
  end;
end;

//----- TRect -----

function Rect(Left, Top, Right, Bottom: Integer): TRect;
begin
  Result.Left   := Left;
  Result.Top    := Top;
  Result.Bottom := Bottom;
  Result.Right  := Right;
end;

procedure Rect(var Rect:TRect; Left, Top, Right, Bottom: Integer);
begin
  Rect.Left   := Left;
  Rect.Top    := Top;
  Rect.Bottom := Bottom;
  Rect.Right  := Right;
end;

procedure SetRect(var Rect:TRect; Left, Top, Right, Bottom: Integer);
begin
  Rect.Left   := Left;
  Rect.Top    := Top;
  Rect.Bottom := Bottom;
  Rect.Right  := Right;
end;

function CopyRect(var Rect: TRect; const R1: TRect): Boolean;
begin
  Rect := R1;
  Result := not IsRectEmpty(Rect);
end;

function SetRectEmpty(var Rect: TRect): Boolean;
begin
  FillChar(Rect,SizeOf(Rect),0);
  Result := True;
end;

function EqualRect(const R1, R2: TRect): Boolean;
begin
  Result :=
    (R1.Left   = R2.Left)  and
    (R1.Right  = R2.Right) and
    (R1.Top    = R2.Top)   and
    (R1.Bottom = R2.Bottom);
end;

function IsRectInside(const R1, R2: TRect): Boolean;
begin
  Result :=
    (R1.Left   >= R2.Left)  and
    (R1.Right  <= R2.Right) and
    (R1.Top    >= R2.Top)   and
    (R1.Bottom <= R2.Bottom);
end;

function IsRectEmpty(const Rect: TRect): Boolean;
begin
  Result := (Rect.Right <= Rect.Left) or (Rect.Bottom <= Rect.Top);
end;

function IntersectRect(const R1, R2: TRect): Boolean;
var
  ml,mr,mt,mb:longint;
begin
  if R1.Left   > R2.Left   then ml := R1.Left   else ml := R2.Left;
  if R1.Right  < R2.Right  then mr := R1.Right  else mr := R2.Right;
  if R1.Top    > R2.Top    then mt := R1.Top    else mt := R2.Top;
  if R1.Bottom < R2.Bottom then mb := R1.Bottom else mb := R2.Bottom;

  Result := (ml < mr) and (mt < mb);
end;

function IntersectRect(var Rect: TRect; const R1, R2: TRect): Boolean;
var
  lRect:TRect;
begin
  lRect := R1;
  if R2.Left   > R1.Left   then lRect.Left   := R2.Left;
  if R2.Top    > R1.Top    then lRect.Top    := R2.Top;
  if R2.Right  < R1.Right  then lRect.Right  := R2.Right;
  if R2.Bottom < R1.Bottom then lRect.Bottom := R2.Bottom;
  Result := not IsRectEmpty(lRect);
  if not Result then FillChar(lRect, SizeOf(lRect), 0);
  Rect:=lRect;
end;

function UnionRect(var Rect: TRect; const R1, R2: TRect): Boolean;
begin
  Rect := R1;
  if not IsRectEmpty(R2) then
  begin
    if R2.Left   < R1.Left   then Rect.Left   := R2.Left;
    if R2.Top    < R1.Top    then Rect.Top    := R2.Top;
    if R2.Right  > R1.Right  then Rect.Right  := R2.Right;
    if R2.Bottom > R1.Bottom then Rect.Bottom := R2.Bottom;
  end;
  Result := not IsRectEmpty(Rect);
  if not Result then FillChar(Rect, SizeOf(Rect), 0);
end;

function InflateRect(var Rect: TRect; dx, dy: Integer): Boolean;
begin
  with Rect do
  begin
    Left   := Left   - dx;
    Right  := Right  + dx;
    Top    := Top    - dy;
    Bottom := Bottom + dy;
  end;
  Result := not IsRectEmpty(Rect);
end;

function OffsetRect(var Rect: TRect; DX: Integer; DY: Integer): Boolean;
begin
  if @Rect <> nil then // Test to increase compatiblity with Windows
  begin
    Inc(Rect.Left  , DX);
    Inc(Rect.Right , DX);
    Inc(Rect.Top   , DY);
    Inc(Rect.Bottom, DY);
    Result := True;
  end
  else
    Result := False;
end;

function Bounds(ALeft, ATop, AWidth, AHeight: Integer): TRect;
begin
  with Result do
  begin
    Left   := ALeft;
    Top    := ATop;
    Right  := ALeft + AWidth;
    Bottom := ATop  + AHeight;
  end;
end;

procedure Bounds(var Rect:TRect; ALeft, ATop, AWidth, AHeight: Integer);
begin
  with Rect do
  begin
    Left   := ALeft;
    Top    := ATop;
    Right  := ALeft + AWidth;
    Bottom := ATop  + AHeight;
  end;
end;

end.