How to resolve the algorithm Perlin noise step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Perlin noise step by step in the XPL0 programming language

Table of Contents

Problem Statement

The Perlin noise is a kind of gradient noise invented by Ken Perlin around the end of the twentieth century and still currently heavily used in computer graphics, most notably to procedurally generate textures or heightmaps. The Perlin noise is basically a pseudo-random mapping of

R

d

{\displaystyle \mathbb {R} ^{d}}

into

R

{\displaystyle \mathbb {R} }

with an integer

d

{\displaystyle d}

which can be arbitrarily large but which is usually 2, 3, or 4. Either by using a dedicated library or by implementing the algorithm, show that the Perlin noise (as defined in 2002 in the Java implementation below) of the point in 3D-space with coordinates 3.14, 42, 7 is 0.13691995878400012. Note: this result assumes 64 bit IEEE-754 floating point calculations. If your language uses a different floating point representation, make a note of it and calculate the value accurate to 15 decimal places, or your languages accuracy threshold if it is less. Trailing zeros need not be displayed.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Perlin noise step by step in the XPL0 programming language

Source code in the xpl0 programming language

func real Noise;  real X, Y, Z;
real U, V, W;
int IX, IY, IZ, P(512), A, AA, AB, B, BA, BB;

    func real Fade;  real T;
    return T*T*T * (T*(T*6.-15.) + 10.);

    func real Lerp;  real T, A, B;
    return A + T*(B-A);

    func real Grad;  int Hash;  real X, Y, Z;
    int  H;  real U, V;
    [H:= Hash & $0F;                        \convert low 4 bits of hash code
    U:= if H<8 then X else Y;               \ into 12 gradient directions
    V:= if H<4 then Y else if H=12 or H=14 then X else Z;
    return (if (H&1) = 0 then U else -U) + (if (H&2) = 0 then V else -V);
    ];

    proc Final;
    int  Permutation, I;
    [Permutation:= [
    151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
    140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247,
    120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57,
    177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74,
    165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60,
    211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65,
    25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200,
    196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64,
    52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212,
    207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213,
    119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9,
    129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112,
    104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179,
    162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181,
    199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254,
    138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66,
    215, 61, 156, 180];
    for I:= 0 to 255 do
        [P(I):= Permutation(I);
        P(256+I):= P(I);
        ];
    ];

[Final;
IX:= fix(Floor(X)) & $FF;                               \find unit cube that
IY:= fix(Floor(Y)) & $FF;                               \ contains point
IZ:= fix(Floor(Z)) & $FF;
X:= X - Floor(X);                                       \find relative X,Y,Z
Y:= Y - Floor(Y);                                       \ of point in cube
Z:= Z - Floor(Z);
U:= Fade(X);                                            \compute fade curves
V:= Fade(Y);                                            \ for each of X,Y,Z
W:= Fade(Z);
A:= P(IX  )+IY;  AA:= P(A)+IZ;  AB:= P(A+1)+IZ;         \hash coordinates of
B:= P(IX+1)+IY;  BA:= P(B)+IZ;  BB:= P(B+1)+IZ;         \ the 8 cube corners,

return Lerp(W, Lerp(V, Lerp(U, Grad(P(AA  ), X   , Y   , Z   ),         \and add
                               Grad(P(BA  ), X-1., Y   , Z   )),        \blended
                       Lerp(U, Grad(P(AB  ), X   , Y-1., Z   ),         \results
                               Grad(P(BB  ), X-1., Y-1., Z   ))),       \from  8
               Lerp(V, Lerp(U, Grad(P(AA+1), X   , Y   , Z-1.),         \corners
                               Grad(P(BA+1), X-1., Y   , Z-1.)),        \of cube
                       Lerp(U, Grad(P(AB+1), X   , Y-1., Z-1.),
                               Grad(P(BB+1), X-1., Y-1., Z-1.))));
];

[Format(1, 17);
RlOut(0, Noise(3.14, 42., 7.));
]

  

You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the Java programming language
You may also check:How to resolve the algorithm Here document step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Compiler/virtual machine interpreter step by step in the Icon programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the Fortran programming language