This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmap_rect.pas
241 lines (189 loc) · 5.09 KB
/
map_rect.pas
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
{ This file is a part of Map editor for VCMI project
Copyright (C) 2016-2017 Alexander Shishkin [email protected]
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit map_rect;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, editor_types;
type
{ TMapRect }
TMapRect = object
FTopLeft: TMapCoord;
FWidth,FHeight: SizeInt;
constructor Create();
constructor SetFromCenter(X,Y, Width,Height: integer);
constructor SetFromCorners(AFirst, ASecond:TMapCoord);
constructor SetFromCorners(X1,Y1,X2,Y2:Integer);
function Left(): integer; inline;
function Right(): integer; inline;
function Top(): integer; inline;
function Bottom(): integer; inline;
function TopLeft():TMapCoord; inline;
function TopRight():TMapCoord; inline;
function BottomLeft():TMapCoord; inline;
function BottomRight():TMapCoord; inline;
function Intersect(Other: TMapRect):TMapRect;
procedure CombineWith(Other: TMapRect);
procedure Clear(); inline;
function IsEmpty: Boolean; inline;
procedure Iterate(Callback: TMapCoordForEach);
procedure Iterate(Callback: TMapCoordForEachO);
end;
implementation
uses
Math;
{ TMapRect }
constructor TMapRect.Create;
begin
Clear();
end;
function TMapRect.Left: integer;
begin
Result := FTopLeft.x;
end;
function TMapRect.Right: integer;
begin
Result := FTopLeft.x + FWidth-1;
end;
function TMapRect.Top: integer;
begin
Result := FTopLeft.y;
end;
function TMapRect.Bottom: integer;
begin
Result := FTopLeft.y + FHeight-1;
end;
function TMapRect.TopLeft: TMapCoord;
begin
Result := FTopLeft;
end;
function TMapRect.TopRight: TMapCoord;
begin
Result.X := Right();
Result.Y := Top();
end;
function TMapRect.BottomLeft: TMapCoord;
begin
Result.X := Left();
Result.Y := Bottom();
end;
function TMapRect.BottomRight: TMapCoord;
begin
Result.X := Right();
Result.Y := Bottom();
end;
function TMapRect.Intersect(Other: TMapRect): TMapRect;
var
intersects: Boolean;
begin
intersects := (Right() >= Other.Left())
and (Other.Right() >= Left())
and (Bottom() >= Other.Top())
and (Other.Bottom() >= Top());
Result.Create();
if intersects then
begin
Result.FTopLeft.X:= max(Left(),Other.Left());
Result.FTopLeft.Y:= max(Top(),Other.Top());
Result.FWidth:= Min(Right(),Other.Right()) - Result.Left+1;
Result.FHeight:= Min(Bottom(),Other.Bottom()) - Result.Top+1;
end;
end;
procedure TMapRect.CombineWith(Other: TMapRect);
var
new_topleft, new_bottomright: TMapCoord;
begin
if IsEmpty then
begin
if not Other.IsEmpty then
SetFromCorners(Other.TopLeft, Other.BottomRight());
end
else
begin
new_topleft.Reset(Min(Left(), Other.Left()), Min(Top(), Other.Top()));
new_bottomright.Reset(Max(Right(), Other.Right()), Max(Bottom(), Other.Bottom()));
SetFromCorners(new_topleft,new_bottomright);
end;
end;
procedure TMapRect.Clear;
begin
FTopLeft.Clear();
FHeight:=0;
FWidth:=0;
end;
function TMapRect.IsEmpty: Boolean;
begin
Result := (FHeight = 0) and (FWidth = 0);
end;
procedure TMapRect.Iterate(Callback: TMapCoordForEach);
var
Current: TMapCoord;
Stop: Boolean;
i,j: SizeInt;
begin
Stop := false;
for i := 0 to FWidth - 1 do
begin
for j := 0 to FHeight - 1 do
begin
Current.X := FTopLeft.X+i;
Current.Y := FTopLeft.Y+j;
Callback(Current, Stop);
if Stop then Exit;
end;
end;
end;
procedure TMapRect.Iterate(Callback: TMapCoordForEachO);
var
Current: TMapCoord;
Stop: Boolean;
i,j: SizeInt;
begin
Stop := false;
for i := 0 to FWidth - 1 do
begin
for j := 0 to FHeight - 1 do
begin
Current.X := FTopLeft.X+i;
Current.Y := FTopLeft.Y+j;
Callback(Current, Stop);
if Stop then Exit;
end;
end;
end;
constructor TMapRect.SetFromCenter(X, Y, Width, Height: integer);
begin
Assert(width mod 2 = 1);
Assert(Height mod 2 = 1);
Clear();
FTopLeft.X:= X - (Width-1) div 2;
FTopLeft.Y:= Y - (Height-1) div 2;
FWidth:=Width;
FHeight:=Height;
end;
constructor TMapRect.SetFromCorners(AFirst, ASecond: TMapCoord);
begin
Clear();
FTopLeft.Reset(Min(AFirst.X,ASecond.X), Min(AFirst.Y,ASecond.Y));
FWidth := abs(AFirst.X-ASecond.X)+1;
FHeight := abs(AFirst.Y-ASecond.Y)+1;
end;
constructor TMapRect.SetFromCorners(X1, Y1, X2, Y2: Integer);
var
AFirst, ASecond: TMapCoord;
begin
AFirst.Reset(x1,y1);
ASecond.Reset(x2,y2);
SetFromCorners(AFirst, ASecond);
end;
end.