author | Xeli |
Mon, 06 Feb 2012 22:52:15 +0100 | |
changeset 6641 | eb0cdccf9ccf |
parent 6580 | 6155187bf599 |
child 6990 | 40e5af28d026 |
permissions | -rw-r--r-- |
6490 | 1 |
unit uLandOutline; |
2 |
||
3 |
interface |
|
4 |
||
5 |
uses uConsts, SDLh, uFloat; |
|
6 |
||
7 |
type TPixAr = record |
|
8 |
Count: Longword; |
|
9 |
ar: array[0..Pred(cMaxEdgePoints)] of TPoint; |
|
10 |
end; |
|
11 |
||
12 |
procedure DrawEdge(var pa: TPixAr; Color: Longword); |
|
13 |
procedure FillLand(x, y: LongInt); |
|
14 |
procedure BezierizeEdge(var pa: TPixAr; Delta: hwFloat); |
|
15 |
procedure RandomizePoints(var pa: TPixAr); |
|
16 |
||
17 |
implementation |
|
18 |
||
6491 | 19 |
uses uLandGraphics, uDebug, uVariables, uLandTemplates, uRandom, uUtils; |
6490 | 20 |
|
21 |
||
22 |
||
23 |
var Stack: record |
|
24 |
Count: Longword; |
|
25 |
points: array[0..8192] of record |
|
26 |
xl, xr, y, dir: LongInt; |
|
27 |
end |
|
28 |
end; |
|
29 |
||
30 |
procedure Push(_xl, _xr, _y, _dir: LongInt); |
|
31 |
begin |
|
32 |
TryDo(Stack.Count <= 8192, 'FillLand: stack overflow', true); |
|
33 |
_y:= _y + _dir; |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
34 |
if (_y < 0) or (_y >= LAND_HEIGHT) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
35 |
exit; |
6490 | 36 |
with Stack.points[Stack.Count] do |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
37 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
38 |
xl:= _xl; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
39 |
xr:= _xr; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
40 |
y:= _y; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
41 |
dir:= _dir |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
42 |
end; |
6490 | 43 |
inc(Stack.Count) |
44 |
end; |
|
45 |
||
46 |
procedure Pop(var _xl, _xr, _y, _dir: LongInt); |
|
47 |
begin |
|
48 |
dec(Stack.Count); |
|
49 |
with Stack.points[Stack.Count] do |
|
50 |
begin |
|
51 |
_xl:= xl; |
|
52 |
_xr:= xr; |
|
53 |
_y:= y; |
|
54 |
_dir:= dir |
|
55 |
end |
|
56 |
end; |
|
57 |
||
58 |
procedure FillLand(x, y: LongInt); |
|
59 |
var xl, xr, dir: LongInt; |
|
60 |
begin |
|
61 |
Stack.Count:= 0; |
|
62 |
xl:= x - 1; |
|
63 |
xr:= x; |
|
64 |
Push(xl, xr, y, -1); |
|
65 |
Push(xl, xr, y, 1); |
|
66 |
dir:= 0; |
|
67 |
while Stack.Count > 0 do |
|
68 |
begin |
|
69 |
Pop(xl, xr, y, dir); |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
70 |
while (xl > 0) and (Land[y, xl] <> 0) do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
71 |
dec(xl); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
72 |
while (xr < LAND_WIDTH - 1) and (Land[y, xr] <> 0) do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
73 |
inc(xr); |
6490 | 74 |
while (xl < xr) do |
75 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
76 |
while (xl <= xr) and (Land[y, xl] = 0) do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
77 |
inc(xl); |
6490 | 78 |
x:= xl; |
79 |
while (xl <= xr) and (Land[y, xl] <> 0) do |
|
80 |
begin |
|
81 |
Land[y, xl]:= 0; |
|
82 |
inc(xl) |
|
83 |
end; |
|
84 |
if x < xl then |
|
85 |
begin |
|
86 |
Push(x, Pred(xl), y, dir); |
|
87 |
Push(x, Pred(xl), y,-dir); |
|
88 |
end; |
|
89 |
end; |
|
90 |
end; |
|
91 |
end; |
|
92 |
||
93 |
procedure DrawEdge(var pa: TPixAr; Color: Longword); |
|
94 |
var i: LongInt; |
|
95 |
begin |
|
96 |
i:= 0; |
|
97 |
with pa do |
|
98 |
while i < LongInt(Count) - 1 do |
|
99 |
if (ar[i + 1].X = NTPX) then |
|
100 |
inc(i, 2) |
|
101 |
else |
|
102 |
begin |
|
103 |
DrawLine(ar[i].x, ar[i].y, ar[i + 1].x, ar[i + 1].y, Color); |
|
104 |
inc(i) |
|
105 |
end |
|
106 |
end; |
|
107 |
||
108 |
||
109 |
procedure Vector(p1, p2, p3: TPoint; var Vx, Vy: hwFloat); |
|
110 |
var d1, d2, d: hwFloat; |
|
111 |
begin |
|
112 |
Vx:= int2hwFloat(p1.X - p3.X); |
|
113 |
Vy:= int2hwFloat(p1.Y - p3.Y); |
|
114 |
||
115 |
d:= DistanceI(p2.X - p1.X, p2.Y - p1.Y); |
|
116 |
d1:= DistanceI(p2.X - p3.X, p2.Y - p3.Y); |
|
117 |
d2:= Distance(Vx, Vy); |
|
118 |
||
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
119 |
if d1 < d then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
120 |
d:= d1; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
121 |
if d2 < d then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
122 |
d:= d2; |
6490 | 123 |
|
124 |
d:= d * _1div3; |
|
125 |
||
126 |
if d2.QWordValue = 0 then |
|
127 |
begin |
|
128 |
Vx:= _0; |
|
129 |
Vy:= _0 |
|
130 |
end |
|
131 |
else |
|
132 |
begin |
|
133 |
d2:= _1 / d2; |
|
134 |
Vx:= Vx * d2; |
|
135 |
Vy:= Vy * d2; |
|
136 |
||
137 |
Vx:= Vx * d; |
|
138 |
Vy:= Vy * d |
|
139 |
end |
|
140 |
end; |
|
141 |
||
142 |
procedure AddLoopPoints(var pa, opa: TPixAr; StartI, EndI: LongInt; Delta: hwFloat); |
|
143 |
var i, pi, ni: LongInt; |
|
144 |
NVx, NVy, PVx, PVy: hwFloat; |
|
145 |
x1, x2, y1, y2: LongInt; |
|
146 |
tsq, tcb, t, r1, r2, r3, cx1, cx2, cy1, cy2: hwFloat; |
|
147 |
X, Y: LongInt; |
|
148 |
begin |
|
149 |
pi:= EndI; |
|
150 |
i:= StartI; |
|
151 |
ni:= Succ(StartI); |
|
152 |
{$HINTS OFF} |
|
153 |
Vector(opa.ar[pi], opa.ar[i], opa.ar[ni], NVx, NVy); |
|
154 |
{$HINTS ON} |
|
155 |
repeat |
|
156 |
inc(pi); |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
157 |
if pi > EndI then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
158 |
pi:= StartI; |
6490 | 159 |
inc(i); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
160 |
if i > EndI then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
161 |
i:= StartI; |
6490 | 162 |
inc(ni); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
163 |
if ni > EndI then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
164 |
ni:= StartI; |
6490 | 165 |
PVx:= NVx; |
166 |
PVy:= NVy; |
|
167 |
Vector(opa.ar[pi], opa.ar[i], opa.ar[ni], NVx, NVy); |
|
168 |
||
169 |
x1:= opa.ar[pi].x; |
|
170 |
y1:= opa.ar[pi].y; |
|
171 |
x2:= opa.ar[i].x; |
|
172 |
y2:= opa.ar[i].y; |
|
173 |
cx1:= int2hwFloat(x1) - PVx; |
|
174 |
cy1:= int2hwFloat(y1) - PVy; |
|
175 |
cx2:= int2hwFloat(x2) + NVx; |
|
176 |
cy2:= int2hwFloat(y2) + NVy; |
|
177 |
t:= _0; |
|
178 |
while t.Round = 0 do |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
179 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
180 |
tsq:= t * t; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
181 |
tcb:= tsq * t; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
182 |
r1:= (_1 - t*3 + tsq*3 - tcb); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
183 |
r2:= ( t*3 - tsq*6 + tcb*3); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
184 |
r3:= ( tsq*3 - tcb*3); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
185 |
X:= hwRound(r1 * x1 + r2 * cx1 + r3 * cx2 + tcb * x2); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
186 |
Y:= hwRound(r1 * y1 + r2 * cy1 + r3 * cy2 + tcb * y2); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
187 |
t:= t + Delta; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
188 |
pa.ar[pa.Count].x:= X; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
189 |
pa.ar[pa.Count].y:= Y; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
190 |
inc(pa.Count); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
191 |
TryDo(pa.Count <= cMaxEdgePoints, 'Edge points overflow', true) |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
192 |
end; |
6490 | 193 |
until i = StartI; |
194 |
pa.ar[pa.Count].x:= opa.ar[StartI].X; |
|
195 |
pa.ar[pa.Count].y:= opa.ar[StartI].Y; |
|
196 |
inc(pa.Count) |
|
197 |
end; |
|
198 |
||
199 |
procedure BezierizeEdge(var pa: TPixAr; Delta: hwFloat); |
|
200 |
var i, StartLoop: LongInt; |
|
201 |
opa: TPixAr; |
|
202 |
begin |
|
203 |
opa:= pa; |
|
204 |
pa.Count:= 0; |
|
205 |
i:= 0; |
|
206 |
StartLoop:= 0; |
|
207 |
while i < LongInt(opa.Count) do |
|
208 |
if (opa.ar[i + 1].X = NTPX) then |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
209 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
210 |
AddLoopPoints(pa, opa, StartLoop, i, Delta); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
211 |
inc(i, 2); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
212 |
StartLoop:= i; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
213 |
pa.ar[pa.Count].X:= NTPX; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
214 |
pa.ar[pa.Count].Y:= 0; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
215 |
inc(pa.Count); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
216 |
end else inc(i) |
6490 | 217 |
end; |
218 |
||
219 |
||
220 |
function CheckIntersect(V1, V2, V3, V4: TPoint): boolean; |
|
221 |
var c1, c2, dm: LongInt; |
|
222 |
begin |
|
223 |
dm:= (V4.y - V3.y) * (V2.x - V1.x) - (V4.x - V3.x) * (V2.y - V1.y); |
|
224 |
c1:= (V4.x - V3.x) * (V1.y - V3.y) - (V4.y - V3.y) * (V1.x - V3.x); |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
225 |
if dm = 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
226 |
exit(false); |
6490 | 227 |
|
228 |
c2:= (V2.x - V3.x) * (V1.y - V3.y) - (V2.y - V3.y) * (V1.x - V3.x); |
|
229 |
if dm > 0 then |
|
230 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
231 |
if (c1 < 0) or (c1 > dm) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
232 |
exit(false); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
233 |
if (c2 < 0) or (c2 > dm) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
234 |
exit(false) |
6490 | 235 |
end |
236 |
else |
|
237 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
238 |
if (c1 > 0) or (c1 < dm) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
239 |
exit(false); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
240 |
if (c2 > 0) or (c2 < dm) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
241 |
exit(false) |
6490 | 242 |
end; |
243 |
||
244 |
//AddFileLog('1 (' + inttostr(V1.x) + ',' + inttostr(V1.y) + ')x(' + inttostr(V2.x) + ',' + inttostr(V2.y) + ')'); |
|
245 |
//AddFileLog('2 (' + inttostr(V3.x) + ',' + inttostr(V3.y) + ')x(' + inttostr(V4.x) + ',' + inttostr(V4.y) + ')'); |
|
246 |
CheckIntersect:= true |
|
247 |
end; |
|
248 |
||
249 |
||
250 |
function CheckSelfIntersect(var pa: TPixAr; ind: Longword): boolean; |
|
251 |
var i: Longword; |
|
252 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
253 |
if (ind <= 0) or (ind >= Pred(pa.Count)) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
254 |
exit(false); |
6490 | 255 |
for i:= 1 to pa.Count - 3 do |
256 |
if (i <= ind - 1) or (i >= ind + 2) then |
|
257 |
begin |
|
258 |
if (i <> ind - 1) and |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
259 |
CheckIntersect(pa.ar[ind], pa.ar[ind - 1], pa.ar[i], pa.ar[i - 1]) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
260 |
exit(true); |
6490 | 261 |
if (i <> ind + 2) and |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
262 |
CheckIntersect(pa.ar[ind], pa.ar[ind + 1], pa.ar[i], pa.ar[i - 1]) then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
263 |
exit(true); |
6490 | 264 |
end; |
265 |
CheckSelfIntersect:= false |
|
266 |
end; |
|
267 |
||
268 |
procedure RandomizePoints(var pa: TPixAr); |
|
269 |
const cEdge = 55; |
|
270 |
cMinDist = 8; |
|
271 |
var radz: array[0..Pred(cMaxEdgePoints)] of LongInt; |
|
272 |
i, k, dist, px, py: LongInt; |
|
273 |
begin |
|
274 |
for i:= 0 to Pred(pa.Count) do |
|
275 |
begin |
|
276 |
radz[i]:= 0; |
|
277 |
with pa.ar[i] do |
|
278 |
if x <> NTPX then |
|
279 |
begin |
|
280 |
radz[i]:= Min(Max(x - cEdge, 0), Max(LAND_WIDTH - cEdge - x, 0)); |
|
281 |
radz[i]:= Min(radz[i], Min(Max(y - cEdge, 0), Max(LAND_HEIGHT - cEdge - y, 0))); |
|
282 |
if radz[i] > 0 then |
|
283 |
for k:= 0 to Pred(i) do |
|
284 |
begin |
|
285 |
dist:= Max(abs(x - pa.ar[k].x), abs(y - pa.ar[k].y)); |
|
286 |
radz[k]:= Max(0, Min((dist - cMinDist) div 2, radz[k])); |
|
287 |
radz[i]:= Max(0, Min(dist - radz[k] - cMinDist, radz[i])) |
|
288 |
end |
|
289 |
end; |
|
290 |
end; |
|
291 |
||
292 |
for i:= 0 to Pred(pa.Count) do |
|
293 |
with pa.ar[i] do |
|
294 |
if ((x and LAND_WIDTH_MASK) = 0) and ((y and LAND_HEIGHT_MASK) = 0) then |
|
295 |
begin |
|
296 |
px:= x; |
|
297 |
py:= y; |
|
298 |
x:= x + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3; |
|
299 |
y:= y + LongInt(GetRandom(7) - 3) * (radz[i] * 5 div 7) div 3; |
|
300 |
if CheckSelfIntersect(pa, i) then |
|
301 |
begin |
|
302 |
x:= px; |
|
303 |
y:= py |
|
304 |
end; |
|
305 |
end |
|
306 |
end; |
|
307 |
||
308 |
||
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6491
diff
changeset
|
309 |
end. |