Perlin noise: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Perlin noise en FreeBASIC)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(32 intermediate revisions by 16 users not shown)
Line 1:
{{task}}
 
The   '''[[wp:Perlin noise|Perlin noise]]'''   is a kind of   [[wp:gradient noise|gradient noise]]   invented by   [[wp:Ken Perlin|Ken Perlin]]   around the end of the twentieth century and still currently heavily used in   [[wp:computer graphics|computer graphics]],   most notably to procedurally generate textures or heightmaps.
 
The Perlin noise is basically a &nbsp; [[random numbers|pseudo-random]] &nbsp; mapping of &nbsp; <big><big><math>\R^d</math></big></big> &nbsp; into &nbsp; <big><big><math>\R</math></big></big> &nbsp; with an integer &nbsp; <big><math>d</math></big> &nbsp; which can be arbitrarily large but which is usually &nbsp; 2, &nbsp; 3, &nbsp; or &nbsp; 4.
 
Either by using a dedicated library or by implementing the algorithm, show that the Perlin noise &nbsp; (as defined in 2002 in the Java implementation below) &nbsp; of the point in 3D-space with coordinates &nbsp; &nbsp; 3.14, &nbsp; 42, &nbsp; 7 &nbsp; &nbsp; is &nbsp; &nbsp; 0.13691995878400012.
 
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.''
Line 14 ⟶ 13:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V p = [-1] * 512
V 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,
Line 68 ⟶ 67:
grad(:p[BB + 1], x - 1, y - 1, z - 1))))
 
print(‘#.17’.format(perlin_noise(3.14, 42, 7)))</langsyntaxhighlight>
 
{{out}}
Line 74 ⟶ 73:
0.13691995878400011
</pre>
 
=={{header|Arturo}}==
{{trans|Java}}
<syntaxhighlight lang="arturo">
p: [
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
]
'p ++ p
 
fade: function [t] -> t * t * t * 10 + t * (t * 6) - 15
lerp: function [t a b] -> a + t * b - a
 
grad: function [hsh x y z][
h: and hsh 15
u: (h < 8)? -> x -> y
v: (h < 4)? -> y [(or? 12=h 14=h)? -> x -> z]
((0 = and h 1)? -> u -> neg u) + (0 = and h 2)? -> v -> neg v
]
 
noise: function [x y z][
X: and floor x 255
Y: and floor y 255
Z: and floor z 255
x: x - floor x
y: y - floor y
z: z - floor z
u: fade x
v: fade y
w: fade z
A: p\[X ] + Y
AA: p\[A ] + Z
AB: p\[A+1] + Z
B: p\[X+1] + Y
BA: p\[B ] + Z
BB: p\[B+1] + Z
lerp w lerp v lerp u grad p\[AA ] x y z
grad p\[BA ] x-1 y z
lerp u grad p\[AB ] x y-1 z
grad p\[BB ] x-1 y-1 z
lerp v lerp u grad p\[AA+1] x y z-1
grad p\[BA+1] x-1 y z-1
lerp u grad p\[AB+1] x y-1 z-1
grad p\[BB+1] x-1 y-1 z-1
]
 
print noise 3.14 42 7
</syntaxhighlight>
 
{{out}}
 
<pre>0.1369199587840001</pre>
 
=={{header|C}}==
A sign of how close C and Java are is this implementation which differs very little from Perlin's Java implementation. I only removed the static, public and final keywords and the Math class name from the floor calls, and it compiled without errors. Interestingly, in the paper, Perlin says that the benchmark implementation which was used to gauge the algorithm's performance was in C. One important improvement here is that the permutation data is externalized and read from a file. This way different textures and effects can be generated without having to change the source code.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 139 ⟶ 204:
return 0;
}
</syntaxhighlight>
</lang>
Permutation file, it should have 256 integers in the range [0,255]:
<pre>
Line 148 ⟶ 213:
C:\rosettaCode>perlin.exe perlinData.txt 3.14 42 7
Perlin Noise for (3.14,42,7) is 0.13691995878400012
</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cmath>
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
 
std::vector<int32_t> p(512, 0);
 
double fade(const double& t) {
return t * t * t * ( t * ( t * 6 - 15 ) + 10 );
}
 
double lerp(const double& t, const double& a, const double& b) {
return a + t * ( b - a );
}
 
double grad(const int32_t& hash, const double& x, const double& y, const double& z) {
const int32_t h = hash & 15; // CONVERT LOW 4 BITS OF HASH CODE
const double u = h < 8 ? x : y; // INTO 12 GRADIENT DIRECTIONS.
const double v = h < 4 ? y : ( h == 12 || h == 14 ) ? x : z;
return ( ( h & 1 ) == 0 ? u : -u ) + ( ( h & 2 ) == 0 ? v : -v );
}
 
double perlin_noise(double x, double y, double z) {
int32_t X = static_cast<int32_t>(floor(x)) & 0xff; // FIND UNIT CUBE THAT
int32_t Y = static_cast<int32_t>(floor(y)) & 0xff; // CONTAINS POINT.
int32_t Z = static_cast<int32_t>(floor(z)) & 0xff;
x -= floor(x); // FIND RELATIVE X,Y,Z
y -= floor(y); // OF POINT IN CUBE.
z -= floor(z);
 
const double u = fade(x); // COMPUTE FADE CURVES
const double v = fade(y); // FOR EACH OF X,Y,Z.
const double w = fade(z);
 
const int32_t A = p[X ] + Y;
const int32_t AA = p[A ] + Z;
const int32_t AB = p[A + 1] + Z; // HASH COORDINATES OF
const int32_t B = p[X + 1] + Y;
const int32_t BA = p[B ] + Z;
const int32_t BB = p[B + 1] + Z; // 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))));
}
 
void read_file(const std::string& file_name) {
std::ifstream file("../" + file_name);
if ( file.is_open() ) {
int32_t index = 0;
std::string line;
while( std::getline(file, line) ) {
std::istringstream stream(line);
std::string word;
while ( std::getline(stream, word, ',') ) {
const int32_t number = std::stoi(word);
p[index] = number;
p[256 + index] = number;
index++;
}
}
} else {
std::cout << "Cannot open file" << std::endl;
}
}
 
int main() {
read_file("permutation.txt");
 
std::cout << "Perlin noise applied to (3.14, 42.0, 7.0) gives " << std::setprecision(16)
<<perlin_noise(3.14, 42.0, 7.0) << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
Perlin noise applied to (3.14, 42.0, 7.0) gives 0.1369199587840001
</pre>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">;;;; Translation from: Java
 
(declaim (optimize (speed 3) (debug 0)))
Line 239 ⟶ 392:
 
(print (noise 3.14d0 42d0 7d0))
</syntaxhighlight>
</lang>
{{out}}
<pre>0.13691995878400012d0</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.math;
 
struct PerlinNoise {
Line 344 ⟶ 497:
im.savePGM("perlin_noise.pgm");
*/
}</langsyntaxhighlight>
{{out}}
<pre>0.13691995878400012</pre>
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Perlin_noise#Pascal Pascal].
 
 
=={{header|EasyLang}}==
{{trans|Lua}}
{{trans|D}}
 
[https://easylang.dev/show/#cod=bZbdctpIEIXveYqu2qot2C0TzYxmJF3kSVK+AGxiyjZQBBykp893hBBIxLHRzOnuM6d/NGT/49m+2w9z0ZlLmblQWOWsYhXZAAbzGUC0KlkM5qrcfAhWmPd45JmFZC4LFjJLFYC30qrK4PEYPQx8Kg475hK4MOcF5lZEYwFeFZa8iTt6gioOJcYVFjjEWfAWiSrYByvhgN7llcVkpXDiUCxBUlMol9L4dRyA0SUeyoVlqEwiiErJCtEQEUXprNSZjsxIxHtL0q8wakB6XmdkFMJb7ixGI9LnPDDlMGcesmAxN45DS4ItEQ4JMjMrVMrKCs4jH4dwn5VWchZCk1KGv5JVebAmzJGsi5WVLJPOkAQiKuUGP3gwDG3V1BMkRZUCX6qODlQFnFV0ndM6RCtpE0ZHWCYd9BYBVEV+bEQPu2SCl0ZbvbqoChUqiyqFJZc8bKYCRFWa0VCpyFvTkulJOglmhdJjNOgooxG+HShTMlTCMSkFZo2c15RxegFcagRYZ2BO85OsUqosmVp66lHnKjUJCBInMYI4XIRMlqdjJU0DoWOeCvtc08qSByXQrlIRc9PU4+sqWTQcqkRupYYMYvVPLwcpUulcK8JxI21VQCKYQq9JUSYIgZQCKG1GXmJIG0GOFBxvFkkyix5SjUtU1zN7nqx3B9vYcQdtmpjZnnf13+88Ns+T+WR92q5svXh5taPNZT68Hk+HLbv/+r9p/5HsCeaZ/U9Ks2v0x+thj9/ClgOGBV5t4JKgRe/987B4sbfFrzc7W23NJeSN26PFPncvFFrQZt2imSFfC+eF3ujP0NeCXj86V9e75kPXGgHngesD19OIKwwdnh498kc5zcAhDh2aBxHpUcSQofiLiKFHOU50LKLqaxIe5YzKlz1WrSW7b+rTBe16ud1tfr3e93EBz/pjx5nntpXdyC17uL6HVz3c3MPnM/gl3mlf1+zr275p2De3/Uk0muBzW9+v67Zu8/t93TZtMovMNPsaTvfMx7IF3QXMruiqRf0N9T26vBH4nmB5IVgOCZb+ht4I3uXbvgWwuEsAKZPlReG7v9mXnX16Vu/d7M4r3LH4nmVa1xfHziu/4/IjrrFvHOnyV13TpmkdW680UueH6ga+xUijH2oc+JYjpf5vSu8juolsb5/fNm2fX93zpCq/+9ltG6jFbPboFsnozq1AyEx31fb0uf486usrm+wPm+2xG/Yw5xbmi6KY/GPt1arhzHS9ctFL1rod/iHGzx7owjA92zf+m6OsukU2d53X5+5L79M3vgllvF5Tq93H7hDg4F//kq6OBCb9XV7S+eQP Run it]
 
<syntaxhighlight>
p[] = [ 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 to 256
p[] &= p[i]
.
func fade t .
return t * t * t * (t * (t * 6 - 15) + 10)
.
func lerp t a b .
return a + t * (b - a)
.
func grad hash x y z .
h = hash mod 16
if h = 0 or h = 12
return x + y
elif h = 1 or h = 14
return y - x
elif h = 2
return x - y
elif h = 3
return -x - y
elif h = 4
return x + z
elif h = 5
return z - x
elif h = 6
return x - z
elif h = 7
return -x - z
elif h = 8
return y + z
elif h = 9 or h = 13
return z - y
elif h = 10
return y - z
.
return -y - z
.
func noise x y z .
a = floor x mod 256
b = floor y mod 256
c = floor z mod 256
xx = x mod 1
yy = y mod 1
zz = z mod 1
u = fade xx
v = fade yy
w = fade zz
a0 = p[a + 1] + b
a1 = p[a0 + 1] + c
a2 = p[a0 + 2] + c
b0 = p[a + 2] + b
b1 = p[b0 + 1] + c
b2 = p[b0 + 2] + c
k1 = grad p[a1 + 1] xx yy zz
k2 = grad p[b1 + 1] (xx - 1) yy zz
k3 = grad p[a2 + 1] xx (yy - 1) zz
k4 = grad p[b2 + 1] (xx - 1) (yy - 1) zz
k5 = grad p[a1 + 2] xx yy (zz - 1)
k6 = grad p[b1 + 2] (xx - 1) yy (zz - 1)
k7 = grad p[a2 + 2] xx (yy - 1) (zz - 1)
k8 = grad p[b2 + 2] (xx - 1) (yy - 1) (zz - 1)
return lerp w (lerp v (lerp u k1 k2) (lerp u k3 k4)) (lerp v (lerp u k5 k6) (lerp u k7 k8))
.
numfmt 17 0
print noise 3.14 42 7
#
# demo
for y = 0 to 199
for x = 0 to 199
p = noise (x / 30) (y / 30) 0.1
move x / 2 y / 2
color3 p p p
rect 0.6 0.6
.
.
</syntaxhighlight>
 
=={{header|Evaldraw}}==
{{trans|C}}
 
This is a translation of the C version, with the gradient function borrowed from the Go version.
This evaldraw version computes the correct output. The array doesnt have to be 512 elements, we can rely on the wrap around when
accessing indices out of bounds since power of 2 arrays are anded with their size on access. Evaldraw has a built-in noise(x,y,z) function
that behaves like perlin noise, but does not give the exact same result as the original implementation in Java by Ken Perlin. There
is a comparison, so we can see the similarity in output visually.
 
[[File:Evaldrawperlinnoise.png|thumb|alt=builtin noise function is faster|Perlin noise function vs builtin noise]]
 
<syntaxhighlight lang="c">
static p[256] = {
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
};
 
()
{
cls(0);
x=3.14; y=42; z=7;
val = perlin_noise(x,y,z);
// expect val=0.13691995878400012
moveto(0,256);
setcol(255,255,255);
printf("Perlin Noise for (%g,%g,%g) is %.17lf\n",x,y,z,val);
val = noise(x,y,z);
printf("Evaldraw builtin noise for (%g,%g,%g) is %.17lf",x,y,z,val);
t = 2*klock(0);
freq = 8;
scale = freq/255;
for(y=0; y<256; y++)
for(x=0; x<256; x++)
{
val = 128+128 * perlin_noise(x * scale,y * scale,t);
setcol(48+.25*val,64+1*val,200+1.5*val);
setpix(x,y);
val = 128+128 * noise(x * scale,y * scale,t);
setcol(48+.25*val,64+1*val,200+1.5*val);
setpix(x+256,y);
}
}
 
fade(t) { t * t * t * (t * (t * 6 - 15) + 10); }
 
lerp(t,a,b) { a + t * (b - a); }
 
grad(hash, double x, double y, double z) {
// convert 4 bits of hash into 12 gradient vectors
h = int(hash % 15);
// Evaldraw rscript compiler doesnt handle
// chained terniary statements gracefully, use if-else instead.
if (h==0 || h==12)
return x + y;
else if (h==1 || h==14)
return y - x;
else if (h==2)
return x - y;
else if (h==3)
return -x - y;
else if (h==4)
return x + z;
else if (h==5)
return z - x;
else if (h==6)
return x - z;
else if (h==7)
return -x - z;
else if (h==8)
return y + z;
else if (h==9 || h==13)
return z - y;
else if (h==10)
return y - z;
// case 11, 16:
return -y - z;
}
perlin_noise(x,y,z) {
// Find cube corner from xyz
cubx = floor(x);
cuby = floor(y);
cubz = floor(z);
x -= floor(x);
y -= floor(y);
z -= floor(z);
// Curves for x,y,z
u = fade(x);
v = fade(y);
w = fade(z);
// Hash coords of 8 cube corners
A = p[cubx]+cuby; AA = p[A]+cubz; AB = p[A+1]+cubz,
B = p[cubx+1]+cuby; BA = p[B]+cubz; BB = p[B+1]+cubz;
// Blended results from 8 corners in cube
return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ),
grad(p[BA ], x-1, y , z )),
lerp(u, grad(p[AB ], x , y-1, z ),
grad(p[BB ], x-1, y-1, z ))),
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ),
grad(p[BA+1], x-1, y , z-1 )),
lerp(u, grad(p[AB+1], x , y-1, z-1 ),
grad(p[BB+1], x-1, y-1, z-1 ))));
}
</syntaxhighlight>
 
=={{header|Factor}}==
Line 354 ⟶ 713:
 
As this is a strict translation of applicative code that makes heavy use of local variables, it is highly non-idiomatic. In idiomatic code, we would never write a word as long as <code>noise</code> or name so many throwaway values. We would also abstract out repetitive patterns such as
<syntaxhighlight lang="text">x floor >integer 255 bitand :> X
y floor >integer 255 bitand :> Y
z floor >integer 255 bitand :> Z</langsyntaxhighlight>
with dataflow combinators:
<syntaxhighlight lang="text">[ floor >integer 255 bitand ] tri@</langsyntaxhighlight>
<langsyntaxhighlight lang="factor">USING: kernel math math.functions literals locals prettyprint
sequences ;
IN: rosetta-code.perlin-noise
Line 420 ⟶ 779:
: main ( -- ) 3.14 42 7 noise . ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
0.1369199587840001
</pre>
 
 
=={{header|Fortran}}==
{{trans|Java}}
 
<syntaxhighlight lang="fortran">
PROGRAM PERLIN
IMPLICIT NONE
INTEGER :: i
INTEGER, DIMENSION(0:511) :: p
INTEGER, DIMENSION(0:255), PARAMETER :: 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/)
 
DO i=0, 255
p(i) = permutation(i)
p(256+i) = permutation(i)
END DO
 
WRITE(*,"(F19.17)") NOISE(3.14d0, 42d0, 7d0)
 
CONTAINS
 
DOUBLE PRECISION FUNCTION NOISE(x_in, y_in, z_in)
DOUBLE PRECISION, INTENT(IN) :: x_in, y_in, z_in
DOUBLE PRECISION :: x, y, z
INTEGER :: xx, yy, zz, a, aa, ab, b, ba, bb
DOUBLE PRECISION :: u, v, w
 
x = x_in
y = y_in
z = z_in
 
xx = IAND(FLOOR(x), 255)
yy = IAND(FLOOR(y), 255)
zz = IAND(FLOOR(z), 255)
 
x = x - FLOOR(x)
y = y - FLOOR(y)
z = z - FLOOR(z)
 
u = FADE(x)
v = FADE(y)
w = FADE(z)
 
a = p(xx) + yy
aa = p(a) + zz
ab = p(a+1) + zz
b = p(xx+1) + yy
ba = p(b) + zz
bb = p(b+1) + zz
 
NOISE = LERP(w, LERP(v, LERP(u, GRAD(p(aa), x, y, z), &
GRAD(p(ba), x-1, y, z)), &
LERP(u, GRAD(p(ab), x, y-1, z), &
GRAD(p(bb), x-1, y-1, z))), &
LERP(v, LERP(u, GRAD(p(aa+1), x, y, z-1), &
GRAD(p(ba+1), x-1, y, z-1)), &
LERP(u, GRAD(p(ab+1), x, y-1, z-1), &
GRAD(p(bb+1), x-1, y-1, z-1))))
END FUNCTION
 
 
DOUBLE PRECISION FUNCTION FADE(t)
DOUBLE PRECISION, INTENT(IN) :: t
 
FADE = t ** 3 * (t * ( t * 6 - 15) + 10)
END FUNCTION
 
 
DOUBLE PRECISION FUNCTION LERP(t, a, b)
DOUBLE PRECISION, INTENT(IN) :: t, a, b
 
LERP = a + t * (b - a)
END FUNCTION
 
 
DOUBLE PRECISION FUNCTION GRAD(hash, x, y, z)
INTEGER, INTENT(IN) :: hash
DOUBLE PRECISION, INTENT(IN) :: x, y, z
INTEGER :: h
DOUBLE PRECISION :: u, v
 
h = IAND(hash, 15)
 
u = MERGE(x, y, h < 8)
 
v = MERGE(y, MERGE(x, z, h == 12 .OR. h == 14), h < 4)
 
GRAD = MERGE(u, -u, IAND(h, 1) == 0) + MERGE(v, -v, IAND(h, 2) == 0)
END FUNCTION
 
 
END PROGRAM
</syntaxhighlight>
 
{{out}}
<pre>
0.13691995878400012
</pre>
 
Line 430 ⟶ 899:
{{trans|PureBasic}}
{{trans|Phix}}
<langsyntaxhighlight lang="freebasic">
#define floor(x) ((x*2.0-0.5) Shr 1)
Dim Shared As Byte p(256) => { _
Line 467 ⟶ 936:
Case 5 : Return z-x
Case 6 : Return x-z
Case 7 : Return -x-y ' maybe -x-z?
Case 8 : Return y+z
Case 9 : Return z-y
Line 498 ⟶ 967:
Print Using "El Ruido Perlin para (3.14, 42, 7) es #.################"; noise(3.14, 42, 7)
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
El Ruido Perlin para (3.14, 42, 7) es 0.1369199587840001
</pre>
 
 
=={{header|GLSL}}==
From https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83 :
<langsyntaxhighlight lang="glsl">
float rand(vec2 c){
return fract(sin(dot(c.xy ,vec2(12.9898,78.233))) * 43758.5453);
Line 545 ⟶ 1,013:
return nf*nf*nf*nf;
}
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 636 ⟶ 1,104:
222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
}
var p = append(permutation, permutation...)</langsyntaxhighlight>
{{out}}
<pre>
Line 646 ⟶ 1,114:
We can trivially copy the java implementation:
 
<langsyntaxhighlight Jlang="j">band=:17 b.
ImprovedNoise=:3 :0
'x y z'=. y
Line 712 ⟶ 1,180:
(lerp w,(lerp v,(lerp u,(grad(p{~AA),x, y, z), t7),t6),t4)
)
</syntaxhighlight>
</lang>
 
And this gives us our desired result:
 
<langsyntaxhighlight Jlang="j"> ImprovedNoise 3.14 42 7
0.13692</langsyntaxhighlight>
 
Or, asking to see 20 digits after the decimal point:
 
<syntaxhighlight lang="j">
<lang J>
22j20": ImprovedNoise 3.14 42 7
0.13691995878400012000</langsyntaxhighlight>
 
=== Simplified Expression ===
Line 729 ⟶ 1,197:
It's tempting, though, to express this more concisely. We are limited, there, by some of the arbitrary choices in the algorithm, but we can still exploit regularities in its structure:
 
<langsyntaxhighlight Jlang="j">p=:,~ 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
 
fade=: 0 0 0 10 _15 6&p.
Line 751 ⟶ 1,219:
v=. (1{uvw) lerp"1 u
w=. (2{uvw) lerp"1 v
)</langsyntaxhighlight>
 
And we can see that there's no difference in this result:
 
<langsyntaxhighlight Jlang="j"> 0.13691995878400012 - ImprovedNoise 3.14 42 7
0</langsyntaxhighlight>
 
It's probably possible to simplify this further.
 
=={{header|Java}}==
The original code from Perlin was originally published in java. Note that this does not have a main method so there will be no output. To test, add a main method, call noise() with 3.14,42,7, save the file as ImprovedNoise.java and compile.
<langsyntaxhighlight lang="java">// JAVA REFERENCE IMPLEMENTATION OF IMPROVED NOISE - COPYRIGHT 2002 KEN PERLIN.
 
public final class ImprovedNoise {
Line 810 ⟶ 1,278:
};
static { for (int i=0; i < 256 ; i++) p[256+i] = p[i] = permutation[i]; }
}</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const permutation = UInt8[
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,
Line 863 ⟶ 1,331:
 
println("Perlin noise applied to (3.14, 42.0, 7.0) gives ", perlinsnoise(3.14, 42.0, 7.0))
</langsyntaxhighlight>{{out}}
<pre>
Perlin noise applied to (3.14, 42.0, 7.0) gives 0.13691995878400012
Line 870 ⟶ 1,338:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
object Perlin {
Line 949 ⟶ 1,417:
fun main(args: Array<String>) {
println(Perlin.noise(3.14, 42.0, 7.0))
}</langsyntaxhighlight>
 
{{out}}
Line 955 ⟶ 1,423:
0.13691995878400012
</pre>
=={{header|M2000 Interpreter}}==
{{trans|Java}}
 
<syntaxhighlight lang="m2000 interpreter">
Group Perlin {
private:
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)
 
Public:
function noise(x, y, z) {
p=lambda a=.permutation (it)-> {
if it < 256 then =a#val(it) else =a#val(it - 256)
}
def fade(t)= t * t * t * (t * (t * 6 - 15) + 10)
def lerp(t, a, b) = a + t * (b - a)
function grad(hash, x, y, z) {
// Convert low 4 bits of hash code into 12 gradient directions
h = binary.and(hash, 15)
u = if(h < 8-> x, y)
v = if(h < 4-> y, if(h == 12 or h == 14->x, z))
=if(binary.and(h,1) = 0-> u, -u) + if(binary.and(h, 2) = 0-> v, -v)
}
// Find unit cube that contains point
xi = binary.and(floor(x), 255)
yi = binary.and(floor(y), 255)
zi = binary.and(floor(z), 255)
// Find relative x, y, z of point in cube
xx = x - floor(x)
yy = y - floor(y)
zz = z - floor(z)
// Compute fade curves for each of xx, yy, zz
u = fade(xx)
v = fade(yy)
w = fade(zz)
// Hash co-ordinates of the 8 cube corners
// and add blended results from 8 corners of cube
a = p(xi) + yi
aa = p(a) + zi
ab = p(a + 1) + zi
b = p(xi + 1) + yi
ba = p(b) + zi
bb = p(b + 1) + zi
=lerp(w, lerp(v, lerp(u, grad(p(aa), xx, yy, zz), grad(p(ba), xx - 1, yy, zz)), lerp(u, grad(p(ab), xx, yy - 1, zz), grad(p(bb), xx - 1, yy - 1, zz))), lerp(v, lerp(u, grad(p(aa + 1), xx, yy, zz - 1), grad(p(ba + 1), xx - 1, yy, zz - 1)), lerp(u, grad(p(ab + 1), xx, yy - 1, zz - 1), grad(p(bb + 1), xx - 1, yy - 1, zz - 1))))
}
}
print Perlin.noise(3.14, 42.0, 7.0)
</syntaxhighlight>
{{out}}
<pre>
0.136919958784
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">local p = {
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
}
 
-- extending for easy access
for i = 1, #p do
p[i+256]=p[i]
end
 
local function fade (t)
-- fade graph: https://www.desmos.com/calculator/d5cgqlrmem
return t*t*t*(t*(t*6-15)+10)
end
 
local function lerp (t, a, b)
return a+t*(b-a)
end
 
local function grad (hash, x, y, z)
local h = hash%16
local cases = {
x+y,
-x+y,
x-y,
-x-y,
x+z,
-x+z,
x-z,
-x-z,
 
y+z,
-y+z,
y-z,
-y-z,
y+x,
-y+z,
y-x,
-y-z,
}
return cases[h+1]
end
 
local function noise (x,y,z)
local a, b, c = math.floor(x)%256, math.floor(y)%256, math.floor(z)%256 -- values in range [0, 255]
local xx, yy, zz = x%1, y%1, z%1
local u, v, w = fade (xx), fade (yy), fade (zz)
local a0 = p[a+1]+b
local a1, a2 = p[a0+1]+c, p[a0+2]+c
local b0 = p[a+2]+b
local b1, b2 = p[b0+1]+c, p[b0+2]+c
local k1 = grad(p[a1+1], xx, yy, zz)
local k2 = grad(p[b1+1], xx-1, yy, zz)
local k3 = grad(p[a2+1], xx, yy-1, zz)
local k4 = grad(p[b2+1], xx-1, yy-1, zz)
local k5 = grad(p[a1+2], xx, yy, zz-1)
local k6 = grad(p[b1+2], xx-1, yy, zz-1)
local k7 = grad(p[a2+2], xx, yy-1, zz-1)
local k8 = grad(p[b2+2], xx-1, yy-1, zz-1)
return lerp(w,
lerp(v, lerp(u, k1, k2), lerp(u, k3, k4)),
lerp(v, lerp(u, k5, k6), lerp(u, k7, k8)))
end
 
print (noise(3.14, 42, 7)) -- 0.136919958784
print (noise(1.4142, 1.2589, 2.718)) -- -0.17245663814988</syntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import math
 
const Permutation = [
Line 1,039 ⟶ 1,646:
 
when isMainModule:
echo noise(3.14, 42, 7)</langsyntaxhighlight>
 
{{out}}
Line 1,046 ⟶ 1,653:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let 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;
Line 1,103 ⟶ 1,710:
 
let p = perlin_init permutation in
print_string ((Printf.sprintf "%0.17f" (perlin_noise p 3.14 42.0 7.0)) ^ "\n")</langsyntaxhighlight>
{{out}}
<pre>0.13691995878400012</pre>
Line 1,110 ⟶ 1,717:
{{works with|Free Pascal}}
dumb copy of [http://rosettacode.org/mw/index.php?title=Perlin_noise#Go Go] minimal adjustments.
<langsyntaxhighlight lang="pascal">program perlinNoise;
//Perlin Noise
//http://rosettacode.org/mw/index.php?title=Perlin_noise#Go
Line 1,218 ⟶ 1,825:
writeln(noise(3.14, 42, 7):20:17);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre> 0.13691995878400012</pre>
Line 1,224 ⟶ 1,831:
=={{header|Perl}}==
{{trans|Java}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use experimental 'signatures';
Line 1,274 ⟶ 1,881:
}
 
print noise 3.14, 42, 7;</langsyntaxhighlight>
{{out}}
<pre>0.136919958784</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant ph = x"97 A0 89 5B 5A 0F 83 0D C9 5F 60 35 C2 E9 07 E1"&
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
x"8C 24 67 1E 45 8E 08 63 25 F0 15 0A 17 BE 06 94"&
<span style="color: #008080;">constant</span> <span style="color: #000000;">ph</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">x"97 A0 89 5B 5A 0F 83 0D C9 5F 60 35 C2 E9 07 E1"</span><span style="color: #0000FF;">&</span>
x"F7 78 EA 4B 00 1A C5 3E 5E FC DB CB 75 23 0B 20"&
<span style="color: #008000;">x"398C B124 2167 581E ED45 958E 3808 5763 AE25 14F0 7D15 880A AB17 A8BE 4406 94"</span><span style="color: AF#0000FF;">&</span>
<span style="color: #008000;">x"4AF7 A578 47EA 864B 8B00 301A 1BC5 A63E 4D5E 92FC 9EDB E7CB 5375 6F23 E50B 20"</span><span style="color: 7A#0000FF;">&</span>
<span style="color: #008000;">x"3C39 D3B1 8521 E658 DCED 6995 5C38 2957 37AE 2E14 F57D 2888 F4AB 66A8 8F44 AF"</span><span style="color: 36#0000FF;">&</span>
<span style="color: #008000;">x"414A 19A5 3F47 A186 018B D830 501B 49A6 D14D 4C92 849E BBE7 D053 596F 12E5 7A"</span><span style="color: A9#0000FF;">&</span>
<span style="color: #008000;">x"C83C C4D3 8785 82E6 74DC BC69 9F5C 5629 A437 642E 6DF5 C628 ADF4 BA66 038F 36"</span><span style="color: 40#0000FF;">&</span>
<span style="color: #008000;">x"3441 D919 E23F FAA1 7C01 7BD8 0550 CA49 26D1 934C 7684 7EBB FFD0 5259 5512 A9"</span><span style="color: D4#0000FF;">&</span>
<span style="color: #008000;">x"CFC8 CEC4 3B87 E382 2F74 10BC 3A9F 1156 B6A4 BD64 1C6D 2AC6 DFAD B7BA AA03 40"</span><span style="color: D5#0000FF;">&</span>
<span style="color: #008000;">x"7734 F8D9 98E2 02FA 2C7C 9A7B A305 46CA DD26 9993 6576 9B7E A7FF 2B52 AC55 D4"</span><span style="color: 09#0000FF;">&</span>
<span style="color: #008000;">x"81CF 16CE 273B FDE3 132F 6210 6C3A 6E11 4FB6 71BD E01C E82A B2DF B9B7 70AA D5"</span><span style="color: 68#0000FF;">&</span>
<span style="color: #008000;">x"DA77 F6F8 6198 E402 FB2C 229A F2A3 C146 EEDD D299 9065 0C9B BFA7 B32B A2AC 09"</span><span style="color: F1#0000FF;">&</span>
<span style="color: #008000;">x"5181 3316 9127 EBFD F913 0E62 EF6C 6B6E 314F C071 D6E0 1FE8 B5B2 C7B9 6A70 68"</span><span style="color: 9D#0000FF;">&</span>
<span style="color: #008000;">x"B8DA 54F6 CC61 B0E4 73FB 7922 32F2 2DC1 7FEE 04D2 9690 FE0C 8ABF ECB3 CDA2 F1"</span><span style="color: 5D#0000FF;">&</span>
<span style="color: #008000;">x"DE51 7233 4391 1DEB 18F9 480E F3EF 8D6B 8031 C3C0 4ED6 421F D7B5 3DC7 9C6A 9D"</span><span style="color: B4#0000FF;",>&</span>
<span style="color: #008000;">x"B8 54 CC B0 73 79 32 2D 7F 04 96 FE 8A EC CD 5D"</span><span style="color: #0000FF;">&</span>
p = ph&ph
<span style="color: #008000;">x"DE 72 43 1D 18 48 F3 8D 80 C3 4E 42 D7 3D 9C B4"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ph</span><span style="color: #0000FF;">&</span><span style="color: #000000;">ph</span>
function fade(atom t) return t * t * t * (t * (t * 6 - 15) + 10) end function
function lerp(atom t, a, b) return a + t * (b - a) end function
<span style="color: #008080;">function</span> <span style="color: #000000;">fade</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">6</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function grad(int hash, atom x, y, z)
<span style="color: #008080;">function</span> <span style="color: #000000;">lerp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">b</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
int h = and_bits(hash,15)
<span style="color: #008080;">function</span> <span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #004080;">int</span> <span style="color: #000000;">hash</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
atom u = iff(h<8 ? x : y),
<span style="color: #004080;">int</span> <span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hash</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">)</span>
v = iff(h<4 ? y : iff(h==12 or h==14 ? x : z))
<span style="color: #004080;">atom</span> <span style="color: #000000;">u</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">8</span> <span style="color: #0000FF;">?</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">),</span>
return iff(and_bits(h,1) == 0 ? u : -u) +
<span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;"><</span><span style="color: #000000;">4</span> <span style="color: #0000FF;">?</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">:</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">==</span><span style="color: #000000;">12</span> <span style="color: #008080;">or</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">==</span><span style="color: #000000;">14</span> <span style="color: #0000FF;">?</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">:</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">))</span>
iff(and_bits(h,2) == 0 ? v : -v)
<span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">0</span> <span style="color: #0000FF;">?</span> <span style="color: #000000;">u</span> <span style="color: #0000FF;">:</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">u</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span>
end function
<span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">0</span> <span style="color: #0000FF;">?</span> <span style="color: #000000;">v</span> <span style="color: #0000FF;">:</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function noise(atom x, y, z)
integer X = and_bits(x,255),
<span style="color: #008080;">function</span> <span style="color: #000000;">noise</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
Y = and_bits(y,255),
<span style="color: #004080;">integer</span> <span style="color: #000000;">X</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">255</span><span style="color: #0000FF;">),</span>
Z = and_bits(z,255)
<span style="color: #000000;">Y</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">255</span><span style="color: #0000FF;">),</span>
x -= floor(x)
<span style="color: #000000;">Z</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">255</span><span style="color: #0000FF;">)</span>
y -= floor(y)
<span style="color: #000000;">x</span> <span style="color: #0000FF;">-=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
z -= floor(z)
<span style="color: #000000;">y</span> <span style="color: #0000FF;">-=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
atom u = fade(x),
<span style="color: #000000;">z</span> <span style="color: #0000FF;">-=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
v = fade(y),
<span style="color: #004080;">atom</span> <span style="color: #000000;">u</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fade</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">),</span>
w = fade(z)
<span style="color: #000000;">v</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fade</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">),</span>
integer A = p[X+1]+Y, AA = p[A+1]+Z, AB = p[A+2]+Z,
<span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fade</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
B = p[X+2]+Y, BA = p[B+1]+Z, BB = p[B+2]+Z
<span style="color: #004080;">integer</span> <span style="color: #000000;">A</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">X</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">AA</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">A</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">Z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">AB</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">A</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">Z</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">B</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">X</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">BA</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">B</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">Z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">BB</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">B</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">Z</span>
return lerp(w,lerp(v,lerp(u,grad(p[AA+1], x , y , z ),
grad(p[BA+1], x-1, y , z )),
<span style="color: #008080;">return</span> <span style="color: #000000;">lerp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lerp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lerp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u</span><span style="color: #0000FF;">,</span><span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">AA</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">),</span>
lerp(u,grad(p[AB+1], x , y-1, z ),
<span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">BA</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">)),</span>
grad(p[BB+1], x-1, y-1, z ))),
<span style="color: #000000;">lerp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u</span><span style="color: #0000FF;">,</span><span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">AB</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">),</span>
lerp(v,lerp(u,grad(p[AA+2], x , y , z-1 ),
<span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">BB</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">))),</span>
grad(p[BA+2], x-1, y , z-1 )),
<span style="color: #000000;">lerp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lerp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u</span><span style="color: #0000FF;">,</span><span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">AA</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">),</span>
lerp(u,grad(p[AB+2], x , y-1, z-1 ),
<span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">BA</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">)),</span>
grad(p[BB+2], x-1, y-1, z-1 ))))
<span style="color: #000000;">lerp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u</span><span style="color: #0000FF;">,</span><span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">AB</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">),</span>
end function
<span style="color: #000000;">grad</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">BB</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #0000FF;">))))</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
printf(1,"Perlin Noise for (3.14,42,7) is %.17f\n",{noise(3.14,42,7)})</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Perlin Noise for (3.14,42,7) is %.17f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">noise</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3.14</span><span style="color: #0000FF;">,</span><span style="color: #000000;">42</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,338 ⟶ 1,948:
=={{header|PureBasic}}==
{{trans|Pascal}}
<langsyntaxhighlight PureBasiclang="purebasic">; Perlin_noise
; http://www.rosettacode.org
 
Line 1,429 ⟶ 2,039:
Input()
EndIf
</syntaxhighlight>
</lang>
{{out}}
<pre>noise(3.14,42,7) => 0.13691995878400012</pre>
Line 1,437 ⟶ 2,047:
{{trans|Java}}
 
<syntaxhighlight lang ="python">def perlin_noise(x, y,import z):math
 
X = int(x) & 255 # FIND UNIT CUBE THAT
def perlin_noise(x, y, z):
Y = int(y) & 255 # CONTAINS POINT.
ZX = intmath.floor(zx) & 255 # FIND UNIT CUBE THAT
xY -= intmath.floor(xy) & 255 # FINDCONTAINS RELATIVE X,Y,ZPOINT.
Z = math.floor(z) & 255
y -= int(y) # OF POINT IN CUBE.
x -= math.floor(x) # FIND RELATIVE X,Y,Z
z -= int(z)
y -= math.floor(y) # OF POINT IN CUBE.
z -= math.floor(z)
u = fade(x) # COMPUTE FADE CURVES
v = fade(y) # FOR EACH OF X,Y,Z.
Line 1,489 ⟶ 2,101:
 
if __name__ == '__main__':
print("%1.17f" % perlin_noise(3.14, 42, 7))</langsyntaxhighlight>
 
{{out}}
Line 1,497 ⟶ 2,109:
{{trans|Java}} -- because we were asked to
 
<langsyntaxhighlight lang="racket">#lang racket
(define (floor-to-255 x)
(bitwise-and (exact-floor x) #xFF))
Line 1,569 ⟶ 2,181:
 
(module+ test
(noise 3.14 42 7))</langsyntaxhighlight>
{{out}}
<pre>0.13691995878400012</pre>
Line 1,576 ⟶ 2,188:
(formerly Perl 6)
{{trans|Java}}
<syntaxhighlight lang="raku" perl6line>constant @p = mapflat {:36($ @_.sort.antipairs.Hash{@_)}, flat }(<
😅😎📸👑👐✌ 💻✊😻💀💁🍃😲🤦☺🤙🔵🌕💎🌎🎶🖕⚠💆🌖🤭❣⚽➡😮☹😂
47 4G 3T 2J 2I F 3N D 5L 2N 2O 1H 5E 6H 7 69 3W 10 2V U 1X 3Y 8 2R 11 6O L A N
🥰💣🤧🐷▶🌈😵🎁👼🦋🤐🙂💟🌔✅🌑🍒😟🌒👎🤪😃🍑👍😜❓💩📣😙😖🎵😝
5A 6 44 6V 3C 6I 23 0 Q 5H 1Q 2M 70 63 5N 39 Z B W 1L 4X X 2G 6L 45 1K 2F 4U K
🐶😓🏃📌🔴🌺🌊😔🐾😀😌🤣👉💙🤠💦🍻🙋💿🤢🤑💓👶🌞🍎🌸🥀🌚🤷💍🖤🍊
3H 3S 4R 4O 1W 4V 22 4L 1Z 3Q 3V 1C R 4M 25 42 4E 6F 2B 33 6D 3E 1O 5V 3P 6E 64
🎉⭐🎂😏☀🚩👆🐉🙈🐸💾😫🙇👏❄😗😹😴📍💸💞😬😍👌😒💋💗😶😛😪☎🎈
2X 2K 15 1J 1A 6T 14 6S 2U 3Z 1I 1T P 1R 4H 1 60 28 21 5T 24 3O 57 5S 2H I 4P
🍀🚶🤝🥵💨💧☝ 🙁🌗😁💡💪🪐👈👋🙌🙆🙅🍺🤞🌹✔🍓✨😤😭🌌🌟🤗😥😘🙏
5K 5G 3R 3M 38 58 4F 2E 4K 2S 31 5I 4T 56 3 1S 1G 61 6A 6Y 3G 3F 5 5M 12 43 3A
💢🥳😆☄🌴😈😑🎼🤓😇💌😉😕🌱😚⚡💰❤🌘🧐❌💅💖💘👅💛🤘🤤😠😩💚💐
3I 73 2A 2D 5W 5R 5Q 1N 6B 1B G 1M H 52 59 S 16 67 53 4Q 5X 3B 6W 48 2 18 4A 4J
🛰 🥂💃🤟🥺🌓🤯😱🤫🙊🖥 ✈😯😡😐🤮👇🌿🗣 🤨🥴✋🤬💕🌻😰🚀🌏😣😷💔😋
1Y 65 49 2T 4B 4N 17 4S 9 3L M 13 71 J 2Q 30 32 27 35 68 6G 4Y 55 34 2W 62 6U
😨👊🙃😞💝💥🌼🌷💫☕😄🧡🔥🤩🙄👻🤔💜🎤🌍⬇🏆🤲🕺💯😳👀🎊🚨🎀😊😢
2P 6C 6Z Y 6Q 5D 6M 5U 40 C 5B 4Z 4I 6P 29 1F 41 6J 6X E 6N 2Z 1D 5C 5Y V 51 5J
>.join.comb) xx 2;
2Y 4D 54 2C 5O 4W 37 3D 1E 19 3J 4 46 72 3U 6K 5P 2L 66 36 1V T O 20 6R 3X 3K
5F 26 1U 5Z 1P 4C 50
> xx 2;
 
sub fade($_) { $_ * $_ * $_ * ($_ * ($_ * 6 - 15) + 10) }
Line 1,598 ⟶ 2,208:
}
 
sub noise($x is copy, $y is copy, $z is copy) is export {
my ($u, $v, $w) = map &fade, ($x, $y, $z) »-=«
my ($X, $Y, $Z) = ($x, $y, $z)».floor »+&» 255;
my ($u, $v, $w) = map &fade, $x -= $X, $y -= $Y, $z -= $Z;
my ($AA, $AB) = @p[$_] + $Z, @p[$_ + 1] + $Z given @p[$X] + $Y;
my ($BA, $BB) = @p[$_] + $Z, @p[$_ + 1] + $Z given @p[$X + 1] + $Y;
lerp($w, lerp($v, lerp($u, grad(@p[$AA ], $x , $y , $z ),
grad(@p[$BA ], $x - 1, $y , $z )),
lerp($u, grad(@p[$AB ], $x , $y - 1, $z ),
grad(@p[$BB ], $x - 1, $y - 1, $z ))),
lerp($v, lerp($u, grad(@p[$AA + 1], $x , $y , $z - 1 ),
grad(@p[$BA + 1], $x - 1, $y , $z - 1 )),
lerp($u, grad(@p[$AB + 1], $x , $y - 1, $z - 1 ),
grad(@p[$BB + 1], $x - 1, $y - 1, $z - 1 ))));
}
 
say noise 3.14, 42, 7;</langsyntaxhighlight>
{{out}}
<pre>0.13691995878</pre>
Line 1,619 ⟶ 2,229:
=={{header|REXX}}==
{{trans|Go}}
Programming note: &nbsp; the REXX operator &nbsp; <big><b> '''//''' </b></big> &nbsp; is the remainder for division &nbsp; (not modulus), &nbsp; so the absolute value of the
<br>remainder is used for this task.
 
The original decimal (data) table was compressed into a single hexadecimal value (without spaces).
<langsyntaxhighlight lang="rexx">/*REXX program implements a Perlin noise algorithm of a point in 3D─space. */
_= 97a0895b5a0f830dc95f6035c2e907e18c24671e458e086325f0150a17be0694f778ea4b001ac53e5efcdbcb75230b2039b12158ed953857ae147d88aba844af,
||4aa547868b301ba64d929ee7536fe57a3cd385e6dc695c29372ef528f4668f3641193fa101d85049d14c84bbd05912a9c8c4878274bc9f56a4646dc6adba0340,
Line 1,668 ⟶ 2,278:
grad( pick(ba+1), xm, y , zm )), ,
lerp(u, grad( pick(ab+1), x , ym, zm ), ,
grad( pick(bb+1), xm, ym, zm )))) /1</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
 
(Output note: &nbsp; this REXX program uses '''100''' decimal digit precision, but '''20''' decimal digits would've been adequate.)
 
(Note that REXX uses &nbsp; ''decimal'' &nbsp; floating point, &nbsp; not binary.)
<pre>
Perlin noise for the 3D point [3.14 42 7] ───► 0.136919958784
Line 1,680 ⟶ 2,290:
=={{header|Rust}}==
{{trans|Java}}
<langsyntaxhighlight lang="rust">fn main() {
println!("{}", noise(3.14, 42.0, 7.0));
}
Line 1,756 ⟶ 2,366:
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];</langsyntaxhighlight>
'''Output:'''
<pre>
Line 1,764 ⟶ 2,374:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">const p = (%w'47 4G 3T 2J 2I F 3N D 5L 2N 2O 1H 5E 6H 7 69 3W 10 2V U 1X 3Y 8
2R 11 6O L A N 5A 6 44 6V 3C 6I 23 0 Q 5H 1Q 2M 70 63 5N 39 Z B W 1L 4X X 2G
6L 45 1K 2F 4U K 3H 3S 4R 4O 1W 4V 22 4L 1Z 3Q 3V 1C R 4M 25 42 4E 6F 2B 33 6D
Line 1,800 ⟶ 2,410:
}
 
say noise(3.14, 42, 7)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,808 ⟶ 2,418:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
struct Perlin {
Line 1,881 ⟶ 2,491:
}
 
print(Perlin.noise(x: 3.14, y: 42, z: 7))</langsyntaxhighlight>
 
{{out}}
Line 1,889 ⟶ 2,499:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">namespace eval perlin {
proc noise {x y z} {
# Find unit cube that contains point.
Line 1,956 ⟶ 2,566:
}
 
puts [perlin::noise 3.14 42 7]</langsyntaxhighlight>
{{out}}
0.13691995878400012
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import math
 
// vlang doesn't have globals
const (
p = [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,
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 ]
)
 
fn main() {
println(noise(3.14, 42, 7))
}
fn noise(x1 f64, y1 f64, z1 f64) f64 {
bx := int(math.floor(x1)) & 255
by := int(math.floor(y1)) & 255
bz := int(math.floor(z1)) & 255
x := x1 - math.floor(x1)
y := y1 - math.floor(y1)
z := z1 - math.floor(z1)
u := fade(x)
v := fade(y)
w := fade(z)
ba := p[bx] + by
baa := p[ba] + bz
bab := p[ba+1] + bz
bb := p[bx+1] + by
bba := p[bb] + bz
bbb := p[bb+1] + bz
return lerp(w, lerp(v, lerp(u, grad(p[baa], x, y, z),
grad(p[bba], x-1, y, z)),
lerp(u, grad(p[bab], x, y-1, z),
grad(p[bbb], x-1, y-1, z))),
lerp(v, lerp(u, grad(p[baa+1], x, y, z-1),
grad(p[bba+1], x-1, y, z-1)),
lerp(u, grad(p[bab+1], x, y-1, z-1),
grad(p[bbb+1], x-1, y-1, z-1))))
}
fn fade(t f64) f64 { return t * t * t * (t*(t*6-15) + 10) }
fn lerp(t f64, a f64, b f64) f64 { return a + t*(b-a) }
fn grad(hash int, x f64, y f64, z f64) f64 {
// Vlang doesn't have a ternary. Ternaries can be translated directly
// with if statements, but chains of if statements are often better
// expressed with match statements.
match hash & 15 {
0 {
return x + y
}
1 {
return y - x
}
2 {
return x - y
}
3 {
return -x - y
}
4 {
return x + z
}
5 {
return z - x
}
6{
return x - z
}
7{
return -x - z
}
8 {
return y + z
}
9 {
return z - y
}
10 {
return y - z
}
12 {
return x + y
}
13 {
return z - y
}
14 {
return y - x
}
else {
// case 11, 16:
return -y - z
}
}
}</syntaxhighlight>
{{out}}
<pre>
0.13691995878400012
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">var 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,
Line 2,028 ⟶ 2,767:
}
 
System.print(noise.call(3.14, 42, 7))</langsyntaxhighlight>
 
{{out}}
<pre>
0.136919958784
</pre>
 
=={{header|XPL0}}==
{{trans|Java}}
<syntaxhighlight lang="xpl0">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.));
]</syntaxhighlight>
{{out}}
<pre>
0.13691995878400000
</pre>
 
=={{header|Zig}}==
{{trans|Go}}
<syntaxhighlight lang="zig">// Made for Zig 0.10.0
// This implementation works with generic float types
 
const std = @import("std");
const expect = std.testing.expect;
 
pub fn main() void {
std.debug.print("{d}\n", .{noise3D(f64, 3.14, 42, 7)});
}
 
pub fn noise3D(comptime T: type, x: T, y: T, z: T) T {
// Disable runtime safety to allow the permutation table values to wrap
@setRuntimeSafety(false);
 
// Truncate float to u8 (256 possible values)
const x_i = @intCast(u8, @floatToInt(isize, @floor(x)) & 255);
const y_i = @intCast(u8, @floatToInt(isize, @floor(y)) & 255);
const z_i = @intCast(u8, @floatToInt(isize, @floor(z)) & 255);
 
// Float remainder of coords (eg: 5.34 -> 0.34)
const x_r = x - @floor(x);
const y_r = y - @floor(y);
const z_r = z - @floor(z);
 
const u = fade(x_r);
const v = fade(y_r);
const w = fade(z_r);
 
const a = permutation[ x_i ] +% y_i;
const aa = permutation[ a ] +% z_i;
const ab = permutation[ a + 1 ] +% z_i;
const b = permutation[x_i + 1] +% y_i;
const ba = permutation[ b ] +% z_i;
const bb = permutation[ b + 1 ] +% z_i;
 
// Add blended results from all 8 corners of the cube
// Use `1.0` instead of `1` to let Zig know it must always be a float value
return lerp(w, lerp(v, lerp(u, grad3D(T, permutation[ aa ], x_r, y_r, z_r),
grad3D(T, permutation[ ba ], x_r - 1.0, y_r, z_r)),
lerp(u, grad3D(T, permutation[ ab ], x_r, y_r - 1.0, z_r),
grad3D(T, permutation[ bb ], x_r - 1.0, y_r - 1.0, z_r))),
lerp(v, lerp(u, grad3D(T, permutation[aa + 1], x_r, y_r, z_r - 1.0),
grad3D(T, permutation[ba + 1], x_r - 1.0, y_r, z_r - 1.0)),
lerp(u, grad3D(T, permutation[ab + 1], x_r, y_r - 1.0, z_r - 1.0),
grad3D(T, permutation[bb + 1], x_r - 1.0, y_r - 1.0, z_r - 1.0))));
}
 
fn grad3D(comptime T: type, h: u8, x: T, y: T, z: T) T {
return switch (h & 15) {
0, 12 => x + y,
1, 14 => y - x,
2 => x - y,
3 => -x - y,
4 => x + z,
5 => z - x,
6 => x - z,
7 => -x - z,
8 => y + z,
9, 13 => z - y,
10 => y - z,
else => -y - z, // 11, 15
};
}
 
fn lerp(t: anytype, a: anytype, b: anytype) @TypeOf(t, a, b) {
return a + t * (b - a);
}
 
fn fade(t: anytype) @TypeOf(t) {
return t * t * t * (t * (6.0 * t - 15.0) + 10.0);
}
 
const permutation = [256]u8{
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,
};
 
test "3D noise" {
try expect(noise3D(f64, 3.14, 42, 7) == 0.13691995878400012);
try expect(noise3D(f64, -4.20, 10, 6) == 0.14208000000000043);
}</syntaxhighlight>
 
{{out}}
<pre>
0.13691995878400012
</pre>
 
=={{header|zkl}}==
{{trans|Java}}
<langsyntaxhighlight lang="zkl">class [static] ImprovedNoise{ // a container, not an object
fcn noise(xyz){ xyz=vm.arglist.apply("toFloat");
X,Y,Z:= // FIND UNIT CUBE THAT CONTAINS POINT.
Line 2,078 ⟶ 2,997:
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180),
p=Data(Void,permutation,permutation);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">ImprovedNoise.noise(3.14, 42, 7).println();</langsyntaxhighlight>
{{out}}
<pre>0.13692</pre>
9,476

edits