Line | |
---|
1 | unit PerlinNoise;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils;
|
---|
7 |
|
---|
8 | function IntNoise(X: Integer): Double;
|
---|
9 |
|
---|
10 | function LinearInterpolate(A, B, X: Double): Double;
|
---|
11 | function CosineInterpolate(A, B, X: Double): Double;
|
---|
12 | function CubicInterpolate(V0, V1, V2, V3, X: Double): Double;
|
---|
13 |
|
---|
14 |
|
---|
15 | implementation
|
---|
16 |
|
---|
17 | function IntNoise(X: Integer): Double;
|
---|
18 | var
|
---|
19 | Xl: Integer;
|
---|
20 | begin
|
---|
21 | Xl := (X shl 13) xor X;
|
---|
22 | Result := (Xl * (Xl * Xl * 15731 + 789221) + 1376312589) and $7fffffff;
|
---|
23 | Result := 1.0 - (Result / 1073741824.0);
|
---|
24 | end;
|
---|
25 |
|
---|
26 | function LinearInterpolate(A, B, X: Double): Double;
|
---|
27 | begin
|
---|
28 | Result := A * (1 - X) + B * X;
|
---|
29 | end;
|
---|
30 |
|
---|
31 | function CosineInterpolate(A, B, X: Double): Double;
|
---|
32 | var
|
---|
33 | F, Ft: Double;
|
---|
34 | begin
|
---|
35 | Ft := X * Pi;
|
---|
36 | F := (1.0 - Cos(Ft)) * 0.5;
|
---|
37 | Result := A * (1 - F) + B * F;
|
---|
38 | end;
|
---|
39 |
|
---|
40 | function CubicInterpolate(V0, V1, V2, V3, X: Double): Double;
|
---|
41 | var
|
---|
42 | P, Q, R, S: Double;
|
---|
43 | begin
|
---|
44 | P := (V3 - V2) - (V0 - V1);
|
---|
45 | Q := (V0 - V1) - P;
|
---|
46 | R := V2 - V0;
|
---|
47 | S := V1;
|
---|
48 |
|
---|
49 | Result := P * X * X * X + Q * X * X + R * X + S;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | end.
|
---|
Note:
See
TracBrowser
for help on using the repository browser.