Perlin noise
You are encouraged to solve this task according to the task description, using any language you may know.
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 into with an integer 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.
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,
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]
L(i) 256
p[256 + i] = p[i] = permutation[i]
F fade(t)
R t ^ 3 * (t * (t * 6 - 15) + 10)
F linerp(t, a, b)
R a + t * (b - a)
F grad(hash, x, y, z)
V h = hash [&] 15
V u = I h < 8 {x} E y
V v = I h < 4 {y} E (I h C (12, 14) {x} E z)
R (I (h [&] 1) == 0 {u} E -u) + (I (h [&] 2) == 0 {v} E -v)
F perlin_noise(=x, =y, =z)
V _x_ = Int(x) [&] 255
V Y = Int(y) [&] 255
V Z = Int(z) [&] 255
x -= Int(x)
y -= Int(y)
z -= Int(z)
V u = fade(x)
V v = fade(y)
V w = fade(z)
V A = :p[_x_] + Y
V AA = :p[A] + Z
V AB = :p[A + 1] + Z
V B = :p[_x_ + 1] + Y
V BA = :p[B] + Z
V BB = :p[B + 1] + Z
R linerp(w, linerp(v, linerp(u, grad(:p[AA], x, y, z),
grad(:p[BA], x - 1, y, z)),
linerp(u, grad(:p[AB], x, y - 1, z),
grad(:p[BB], x - 1, y - 1, z))),
linerp(v, linerp(u, grad(:p[AA + 1], x, y, z - 1),
grad(:p[BA + 1], x - 1, y, z - 1)),
linerp(u, grad(:p[AB + 1], x, y - 1, z - 1),
grad(:p[BB + 1], x - 1, y - 1, z - 1))))
print(‘#.17’.format(perlin_noise(3.14, 42, 7)))
- Output:
0.13691995878400011
ALGOL 68
BEGIN # perlin noise - translated from the 11l sample #
[ 0 : 511 ]INT p;
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
);
p[ 0 : 255 ] := p[ 256 : ];
OP AND = ( INT a, b )INT:
IF a >= 0
THEN ABS ( BIN a AND BIN b )
ELSE ABS ( NOT BIN ( - ( a + 1 ) ) AND BIN b )
FI # AND # ;
PROC fade = ( REAL t )REAL: ( t * t * t ) * ( t * ( t * 6 - 15 ) + 10 );
PROC linerp = ( REAL t, a, b )REAL: a + ( t * ( b - a ) );
PROC grad = ( INT hash, REAL x, y, z )REAL:
BEGIN
INT h = hash AND 15;
REAL u = IF h < 8 THEN x ELSE y FI;
REAL v = IF h < 4 THEN y ELIF h = 12 OR h = 14 THEN x ELSE z FI;
IF ( h AND 1 ) = 0 THEN u ELSE -u FI + IF ( h AND 2 ) = 0 THEN v ELSE -v FI
END # grad # ;
PROC perlin noise = ( REAL x in, y in, z in )REAL:
BEGIN
REAL x := x in, y := y in, z := z in;
INT xx = ENTIER x AND 255;
INT yy = ENTIER y AND 255;
INT zz = ENTIER z AND 255;
x -:= ENTIER x;
y -:= ENTIER y;
z -:= ENTIER z;
REAL u = fade( x );
REAL v = fade( y );
REAL w = fade( z );
INT a = p[ xx ] + yy;
INT aa = p[ a ] + zz;
INT ab = p[ a + 1 ] + zz;
INT b = p[ xx + 1 ] + yy;
INT ba = p[ b ] + zz;
INT bb = p[ b + 1 ] + zz;
linerp( w
, linerp( v
, linerp( u
, grad( p[ aa ], x, y, z )
, grad( p[ ba ], x - 1, y, z )
)
, linerp( u
, grad( p[ ab ], x, y - 1, z )
, grad( p[ bb ], x - 1, y - 1, z )
)
)
, linerp( v
, linerp( u
, grad( p[ aa + 1 ], x, y, z - 1 )
, grad( p[ ba + 1 ], x - 1, y, z - 1 )
)
, linerp( u
, grad( p[ ab + 1 ], x, y - 1, z - 1 )
, grad( p[ bb + 1 ], x - 1, y - 1, z - 1 )
)
)
)
END # perlin noise # ;
print( ( fixed( perlin noise( 3.14, 42, 7 ), -18, 15 ) ) )
END
- Output:
0.136919958784000
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
- Output:
0.1369199587840001
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.
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int p[512];
double fade(double t) { return t * t * t * (t * (t * 6 - 15) + 10); }
double lerp(double t, double a, double b) { return a + t * (b - a); }
double grad(int hash, double x, double y, double z) {
int h = hash & 15;
double u = h<8 ? x : y,
v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
double noise(double x, double y, double z) {
int X = (int)floor(x) & 255,
Y = (int)floor(y) & 255,
Z = (int)floor(z) & 255;
x -= floor(x);
y -= floor(y);
z -= floor(z);
double u = fade(x),
v = fade(y),
w = fade(z);
int 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;
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 ))));
}
void loadPermutation(char* fileName){
FILE* fp = fopen(fileName,"r");
int permutation[256],i;
for(i=0;i<256;i++)
fscanf(fp,"%d",&permutation[i]);
fclose(fp);
for (int i=0; i < 256 ; i++) p[256+i] = p[i] = permutation[i];
}
int main(int argC,char* argV[])
{
if(argC!=5)
printf("Usage : %s <permutation data file> <x,y,z co-ordinates separated by space>");
else{
loadPermutation(argV[1]);
printf("Perlin Noise for (%s,%s,%s) is %.17lf",argV[2],argV[3],argV[4],noise(strtod(argV[2],NULL),strtod(argV[3],NULL),strtod(argV[4],NULL)));
}
return 0;
}
Permutation file, it should have 256 integers in the range [0,255]:
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
Invocation and output :
C:\rosettaCode>perlin.exe perlinData.txt 3.14 42 7 Perlin Noise for (3.14,42,7) is 0.13691995878400012
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;
}
- Output:
Perlin noise applied to (3.14, 42.0, 7.0) gives 0.1369199587840001
Common Lisp
;;;; Translation from: Java
(declaim (optimize (speed 3) (debug 0)))
(defconstant +p+
(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
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))
(aux (make-array 512 :element-type 'fixnum)))
(dotimes (i 256 aux)
(setf (aref aux i) (aref permutation i))
(setf (aref aux (+ 256 i)) (aref permutation i)))))
(defun fade (te)
(declare (type double-float te))
(the double-float (* te te te (+ (* te (- (* te 6) 15)) 10))))
(defun lerp (te a b)
(declare (type double-float te a b))
(the double-float (+ a (* te (- b a)))))
(defun grad (hash x y z)
(declare (type fixnum hash)
(type double-float x y z))
(let* ((h (logand hash 15)) ;; convert lo 4 bits of hash code into 12 gradient directions
(u (if (< h 8) x y))
(v (cond ((< h 4)
y)
((or (= h 12) (= h 14))
x)
(t z))))
(the
double-float
(+
(if (zerop (logand h 1)) u (- u))
(if (zerop (logand h 2)) v (- v))))))
(defun noise (x y z)
(declare (type double-float x y z))
;; find unit cube that contains point.
(let ((cx (logand (floor x) 255))
(cy (logand (floor y) 255))
(cz (logand (floor z) 255)))
;; find relative x, y, z of point in cube.
(let ((x (- x (floor x)))
(y (- y (floor y)))
(z (- z (floor z))))
;; compute fade curves for each of x, y, z.
(let ((u (fade x))
(v (fade y))
(w (fade z)))
;; hash coordinates of the 8 cube corners,
(let* ((ca (+ (aref +p+ cx) cy))
(caa (+ (aref +p+ ca) cz))
(cab (+ (aref +p+ (1+ ca)) cz))
(cb (+ (aref +p+ (1+ cx)) cy))
(cba (+ (aref +p+ cb) cz))
(cbb (+ (aref +p+ (1+ cb)) cz)))
;; ... and add blended results from 8 corners of cube
(the double-float
(lerp w
(lerp v
(lerp u
(grad (aref +p+ caa) x y z)
(grad (aref +p+ cba) (1- x) y z))
(lerp u
(grad (aref +p+ cab) x (1- y) z)
(grad (aref +p+ cbb) (1- x) (1- y) z)))
(lerp v
(lerp u
(grad (aref +p+ (1+ caa)) x y (1- z))
(grad (aref +p+ (1+ cba)) (1- x) y (1- z)))
(lerp u
(grad (aref +p+ (1+ cab)) x (1- y) (1- z))
(grad (aref +p+ (1+ cbb)) (1- x) (1- y) (1- z)))))))))))
(print (noise 3.14d0 42d0 7d0))
- Output:
0.13691995878400012d0
D
import std.stdio, std.math;
struct PerlinNoise {
private static double fade(in double t) pure nothrow @safe @nogc {
return t ^^ 3 * (t * (t * 6 - 15) + 10);
}
private static double lerp(in double t, in double a, in double b)
pure nothrow @safe @nogc {
return a + t * (b - a);
}
private static double grad(in ubyte hash,
in double x, in double y, in double z)
pure nothrow @safe @nogc {
// Convert lo 4 bits of hash code into 12 gradient directions.
immutable h = hash & 0xF;
immutable double u = (h < 8) ? x : y,
v = (h < 4) ? y : (h == 12 || h == 14 ? x : z);
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
}
static immutable ubyte[512] p;
static this() pure nothrow @safe @nogc {
static immutable permutation = cast(ubyte[256])x"97 A0 89 5B 5A
0F 83 0D C9 5F 60 35 C2 E9 07 E1 8C 24 67 1E 45 8E 08 63 25
F0 15 0A 17 BE 06 94 F7 78 EA 4B 00 1A C5 3E 5E FC DB CB 75
23 0B 20 39 B1 21 58 ED 95 38 57 AE 14 7D 88 AB A8 44 AF 4A
A5 47 86 8B 30 1B A6 4D 92 9E E7 53 6F E5 7A 3C D3 85 E6 DC
69 5C 29 37 2E F5 28 F4 66 8F 36 41 19 3F A1 01 D8 50 49 D1
4C 84 BB D0 59 12 A9 C8 C4 87 82 74 BC 9F 56 A4 64 6D C6 AD
BA 03 40 34 D9 E2 FA 7C 7B 05 CA 26 93 76 7E FF 52 55 D4 CF
CE 3B E3 2F 10 3A 11 B6 BD 1C 2A DF B7 AA D5 77 F8 98 02 2C
9A A3 46 DD 99 65 9B A7 2B AC 09 81 16 27 FD 13 62 6C 6E 4F
71 E0 E8 B2 B9 70 68 DA F6 61 E4 FB 22 F2 C1 EE D2 90 0C BF
B3 A2 F1 51 33 91 EB F9 0E EF 6B 31 C0 D6 1F B5 C7 6A 9D B8
54 CC B0 73 79 32 2D 7F 04 96 FE 8A EC CD 5D DE 72 43 1D 18
48 F3 8D 80 C3 4E 42 D7 3D 9C B4";
// Two copies of permutation.
p[0 .. permutation.length] = permutation[];
p[permutation.length .. $] = permutation[];
}
/// x0, y0 and z0 can be any real numbers, but the result is
/// zero if they are all integers.
/// The result is probably in [-1.0, 1.0].
static double opCall(in double x0, in double y0, in double z0)
pure nothrow @safe @nogc {
// Find unit cube that contains point.
immutable ubyte X = cast(int)x0.floor & 0xFF,
Y = cast(int)y0.floor & 0xFF,
Z = cast(int)z0.floor & 0xFF;
// Find relative x,y,z of point in cube.
immutable x = x0 - x0.floor,
y = y0 - y0.floor,
z = z0 - z0.floor;
// Compute fade curves for each of x,y,z.
immutable u = fade(x),
v = fade(y),
w = fade(z);
// Hash coordinates of the 8 cube corners.
immutable 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;
// And add blended results from 8 corners of 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))));
}
}
void main() {
writefln("%1.17f", PerlinNoise(3.14, 42, 7));
/*
// Generate a demo image using the Gray Scale task module.
import grayscale_image;
enum N = 200;
auto im = new Image!Gray(N, N);
foreach (immutable y; 0 .. N)
foreach (immutable x; 0 .. N) {
immutable p = PerlinNoise(x / 30.0, y / 30.0, 0.1);
im[x, y] = Gray(cast(ubyte)((p + 1) / 2 * 256));
}
im.savePGM("perlin_noise.pgm");
*/
}
- Output:
0.13691995878400012
Delphi
See Pascal.
EasyLang
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
.
.
Evaldraw
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.
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 ))));
}
Factor
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 noise
or name so many throwaway values. We would also abstract out repetitive patterns such as
x floor >integer 255 bitand :> X
y floor >integer 255 bitand :> Y
z floor >integer 255 bitand :> Z
with dataflow combinators:
[ floor >integer 255 bitand ] tri@
USING: kernel math math.functions literals locals prettyprint
sequences ;
IN: rosetta-code.perlin-noise
CONSTANT: 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 } dup append
]
:: fade ( t -- x ) t 6 * 15 - t * 10 + t * t * t * ;
:: lerp ( t a b -- x ) b a - t * a + ;
:: grad ( hash x y z -- w )
hash 15 bitand :> h
h 8 < x y ? :> u
h 4 < y h 12 = h 14 = or x z ? ? :> v
h 1 bitand 0 = u u neg ? h 2 bitand 0 = v v neg ? + ;
:: noise ( x! y! z! -- noise )
x floor >integer 255 bitand :> X
y floor >integer 255 bitand :> Y
z floor >integer 255 bitand :> Z
x x floor - x!
y y floor - y!
z z floor - z!
x fade :> u
y fade :> v
z fade :> w
X p nth Y + :> A
A p nth Z + :> AA
A 1 + p nth Z + :> AB
X 1 + p nth Y + :> B
B p nth Z + :> BA
B 1 + p nth Z + :> BB
w v u AA p nth x y z grad
BA p nth x 1 - y z grad lerp
u AB p nth x y 1 - z grad
BB p nth x 1 - y 1 - z grad lerp lerp
v u AA 1 + p nth x y z 1 - grad
BA 1 + p nth x 1 - y z 1 - grad lerp
u AB 1 + p nth x y 1 - z 1 - grad
BB 1 + p nth x 1 - y 1 - z 1 - grad lerp lerp lerp ;
: main ( -- ) 3.14 42 7 noise . ;
MAIN: main
- Output:
0.1369199587840001
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
- Output:
0.13691995878400012
FreeBASIC
#define floor(x) ((x*2.0-0.5) Shr 1)
Dim Shared As Byte 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}
Function fade(t As Double) As Double
Return t * t * t * (t * (t * 6 - 15) + 10)
End Function
Function lerp(t As Double, a As Double, b As Double) As Double
Return a + t * (b - a)
End Function
Function grad(hash As Integer, x As Double, y As Double, z As Double) As Double
Select Case (hash And 15)
Case 0 : Return x+y
Case 1 : Return y-x
Case 2 : Return x-y
Case 3 : Return -x-y
Case 4 : Return x+z
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
Case 10 : Return y-z
Case 11 : Return -y-z
Case 12 : Return x+y
Case 13 : Return z-y
Case 14 : Return y-x
Case 15 : Return -y-z
End Select
End Function
Function noise(x As Double, y As Double, z As Double) As Double
Dim As Integer a = Int(x) And 255, b = Int(y) And 255, c = Int(z) And 255
x -= floor(x) : y -= floor(y) : z -= floor(z)
Dim As Double u = fade(x), v = fade(y), w = fade(z)
Dim As Integer A0 = p(a)+b, A1 = p(A0)+c, A2 = p(A0+1)+c
Dim As Integer B0 = p(a+1)+b, B1 = p(B0)+c, B2 = p(B0+1)+c
Return lerp(w, lerp(v, lerp(u, grad(p(A1), x, y, z), _
grad(p(B1), x-1, y, z)), _
lerp(u, grad(p(A2), x, y-1, z), _
grad(p(B2), x-1, y-1, z))), _
lerp(v, lerp(u, grad(p(A1+1), x, y, z-1), _
grad(p(B1+1), x-1, y, z-1)), _
lerp(u, grad(p(A2+1), x, y-1, z-1), _
grad(p(B2+1), x-1, y-1, z-1))))
End Function
Print Using "El Ruido Perlin para (3.14, 42, 7) es #.################"; noise(3.14, 42, 7)
Sleep
- Output:
El Ruido Perlin para (3.14, 42, 7) es 0.1369199587840001
GLSL
From https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83 :
float rand(vec2 c){
return fract(sin(dot(c.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
float noise(vec2 p, float freq ){
float unit = screenWidth/freq;
vec2 ij = floor(p/unit);
vec2 xy = mod(p,unit)/unit;
//xy = 3.*xy*xy-2.*xy*xy*xy;
xy = .5*(1.-cos(PI*xy));
float a = rand((ij+vec2(0.,0.)));
float b = rand((ij+vec2(1.,0.)));
float c = rand((ij+vec2(0.,1.)));
float d = rand((ij+vec2(1.,1.)));
float x1 = mix(a, b, xy.x);
float x2 = mix(c, d, xy.x);
return mix(x1, x2, xy.y);
}
float pNoise(vec2 p, int res){
float persistance = .5;
float n = 0.;
float normK = 0.;
float f = 4.;
float amp = 1.;
int iCount = 0;
for (int i = 0; i<50; i++){
n+=amp*noise(p, f);
f*=2.;
normK+=amp;
amp*=persistance;
if (iCount == res) break;
iCount++;
}
float nf = n/normK;
return nf*nf*nf*nf;
}
Go
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(noise(3.14, 42, 7))
}
func noise(x, y, z float64) float64 {
X := int(math.Floor(x)) & 255
Y := int(math.Floor(y)) & 255
Z := int(math.Floor(z)) & 255
x -= math.Floor(x)
y -= math.Floor(y)
z -= math.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
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))))
}
func fade(t float64) float64 { return t * t * t * (t*(t*6-15) + 10) }
func lerp(t, a, b float64) float64 { return a + t*(b-a) }
func grad(hash int, x, y, z float64) float64 {
// Go doesn't have a ternary. Ternaries can be translated directly
// with if statements, but chains of if statements are often better
// expressed with switch statements.
switch hash & 15 {
case 0, 12:
return x + y
case 1, 14:
return y - x
case 2:
return x - y
case 3:
return -x - y
case 4:
return x + z
case 5:
return z - x
case 6:
return x - z
case 7:
return -x - z
case 8:
return y + z
case 9, 13:
return z - y
case 10:
return y - z
}
// case 11, 16:
return -y - z
}
var permutation = []int{
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,
}
var p = append(permutation, permutation...)
- Output:
0.13691995878400012
J
We can trivially copy the java implementation:
band=:17 b.
ImprovedNoise=:3 :0
'x y z'=. y
X=. (<. x) band 255
Y=. (<. y) band 255
Z=. (<. z) band 255
x=. x-<.!.0 x
y=. y-<.!.0 y
z=. z-<.!.0 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
t1=. (grad(p{~BB+1),(x-1),(y-1),z-1)
t2=. (lerp u,(grad(p{~AB+1),x,(y-1),z-1),t1)
t3=. (grad(p{~BA+1),(x-1),y,z-1)
t4=. (lerp v,(lerp u,(grad(p{~AA+1),x,y,z-1),t3),t2)
t5=. (grad(p{~BB),(x-1),(y-1),z)
t6=. (lerp u,(grad(p{~AB),x,(y-1),z),t5)
t7=. (grad(p{~BA),(x-1),y,z)
(lerp w,(lerp v,(lerp u,(grad(p{~AA),x,y,z),t7),t6),t4)
)
fade=:3 :0
t=.y
t * t * t * ((t * ((t * 6) - 15)) + 10)
)
lerp=:3 :0
't a b'=. y
a + t * (b - a)
)
grad=:3 :0
'hash x y z'=. y
h =. hash band 15 NB. CONVERT LO 4 BITS OF HASH CODE
u =. x [^:(h<8) y NB. INTO 12 GRADIENT DIRECTIONS.
v =. y [^:(h<4) x [^:((h=12)+.(h=14)) z
(u [^:((h band 1) = 0) -u) + v [^:((h band 2) = 0) -v
)
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.
ImprovedNoise=:3 :0
'XYZ xyz'=. |:256 1 #:y
uvw=. fade xyz
hash=. 0 1+/(+ p{~0 1+/])/|. XYZ
t1=. (grad(p{~BB+1),(x-1),(y-1),z-1)
t2=. (lerp u,(grad(p{~AB+1), x, (y-1),z-1),t1)
t3=. (grad(p{~BA+1),(x-1), y, z-1)
t4=. (lerp v,(lerp u,(grad(p{~AA+1), x, y, z-1),t3),t2)
t5=. (grad(p{~BB), (x-1),(y-1),z)
t6=. (lerp u,(grad(p{~AB), x, (y-1),z), t5)
t7=. (grad(p{~BA), (x-1), y, z)
(lerp w,(lerp v,(lerp u,(grad(p{~AA),x, y, z), t7),t6),t4)
)
And this gives us our desired result:
ImprovedNoise 3.14 42 7
0.13692
Or, asking to see 20 digits after the decimal point:
22j20": ImprovedNoise 3.14 42 7
0.13691995878400012000
Simplified Expression
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:
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.
grad=:4 :0
dir=. (16|x){_1+3 3 3#:25 7 19 1 23 5 21 3 17 11 15 9 25 11 7 9
dir+/ .*"1 y
)
lerp=:4 :0
'a b'=. y
a + x * (b - a)
)
ImprovedNoise=:3 :0
'XYZ xyz'=. |:256 1 #:y
uvw=. fade xyz
hash=. p{~0 1+/(+ p{~0 1+/])/|. XYZ
g=. hash grad xyz-"1|."1#:i.$hash
u=. (0{uvw) lerp"1 g
v=. (1{uvw) lerp"1 u
w=. (2{uvw) lerp"1 v
)
And we can see that there's no difference in this result:
0.13691995878400012 - ImprovedNoise 3.14 42 7
0
It's probably possible to simplify this further.
Java
The original code from Perlin was 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.
// JAVA REFERENCE IMPLEMENTATION OF IMPROVED NOISE - COPYRIGHT 2002 KEN PERLIN.
public final class ImprovedNoise {
static public double noise(double x, double y, double z) {
int X = (int)Math.floor(x) & 255, // FIND UNIT CUBE THAT
Y = (int)Math.floor(y) & 255, // CONTAINS POINT.
Z = (int)Math.floor(z) & 255;
x -= Math.floor(x); // FIND RELATIVE X,Y,Z
y -= Math.floor(y); // OF POINT IN CUBE.
z -= Math.floor(z);
double u = fade(x), // COMPUTE FADE CURVES
v = fade(y), // FOR EACH OF X,Y,Z.
w = fade(z);
int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF
B = p[X+1]+Y, BA = p[B]+Z, 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 ))));
}
static double fade(double t) { return t * t * t * (t * (t * 6 - 15) + 10); }
static double lerp(double t, double a, double b) { return a + t * (b - a); }
static double grad(int hash, double x, double y, double z) {
int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE
double u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS.
v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
static final int p[] = new int[512], 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
};
static { for (int i=0; i < 256 ; i++) p[256+i] = p[i] = permutation[i]; }
}
jq
Adapted from Wren
Works with jq, the C implementation of jq
Works with gojq, the Go implementation of jq
### Generic functions
# (-3.2|fraction) #=> -0.2
def fraction:
if . >= 0 then . - trunc
else - (-. | fraction)
end;
# For gojq
def div($y):
(. - (. % y)) / $y;
# Convert the input integer to a stream of 0s and 1s, least significant bit first
def bitwise:
recurse( if . >= 2 then div(2) else empty end) | . % 2;
# Inverse of bitwise:
def stream_to_integer(stream):
reduce stream as $c ( {power:1 , ans: 0};
.ans += ($c * .power) | .power *= 2 )
| .ans;
# . & 2^($n-1)
def bits($n):
stream_to_integer(limit($n; bitwise));
### Perlin Noise
def 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
];
def grad($hash; $x; $y; $z):
def bit2: nth(1; bitwise);
# Convert low 4 bits of hash code into 12 gradient directions:
( $hash|bits(4) ) as $h
| (if $h < 8 then $x else $y end) as $u
| (if $h < 4 then $y elif ($h == 12 or $h == 14) then $x else $z end) as $v
| (if ($h % 2 == 0) then $u else -$u end) +
(if (($h | bit2) == 0) then $v else -$v end) ;
def noise($x; $y; $z):
def p:
permutation as $p
| $p[:256] + [range(256;512) | $p[.-256]];
def fade:
. * . * . * (. * (. * 6 - 15) + 10) ;
def lerp($t; $a; $b): $a + $t * ($b - $a);
# Find unit cube that contains point
([$x,$y,$z] | map(floor | bits(8))) as [$xi, $yi, $zi]
# Find relative x, y, z of point in cube
| ([$x,$y,$z] | map(fraction)) as [$xx, $yy, $zz]
# Compute fade curves for each of xx, yy, zz
| ([$xx,$yy,$zz] | map(fade)) as [$u, $v, $w]
| p as $p
# Hash co-ordinates of the 8 cube corners
# and add blended results from 8 corners of cube
| ($p[$xi] + $yi) as $a
| ($p[$a] + $zi) as $aa
| ($p[$a + 1] + $zi) as $ab
| ($p[$xi + 1] + $yi) as $b
| ($p[$b] + $zi) as $ba
| ($p[$b + 1] + $zi) as $bb
| 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))) );
noise(3.14; 42; 7)
- Output:
0.13691995878400012
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,
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]
function grad(h::Integer, x, y, z)
h &= 15 # CONVERT LO 4 BITS OF HASH CODE
u = h < 8 ? x : y # INTO 12 GRADIENT DIRECTIONS.
v = h < 4 ? y : h == 12 || h == 14 ? x : z
(h & 1 == 0 ? u : -u) + (h & 2 == 0 ? v : -v)
end
function perlinsnoise(x, y, z)
p = vcat(permutation, permutation)
fade(t) = t * t * t * (t * (t * 6 - 15) + 10)
lerp(t, a, b) = a + t * (b - a)
floorb(x) = Int(floor(x)) & 0xff
X, Y, Z = floorb(x), floorb(y), floorb(z) # FIND UNIT CUBE THAT CONTAINS POINT.
x, y, z = x - floor(x), y - floor(y), z - floor(z) # FIND RELATIVE X,Y,Z OF POINT IN CUBE.
u, v, w = fade(x), fade(y), fade(z) # COMPUTE FADE CURVES FOR EACH OF X,Y,Z.
A = p[X + 1] + Y; AA = p[A + 1] + Z; AB = p[A + 2] + Z
B = p[X + 2] + Y; BA = p[B + 1] + Z; BB = p[B + 2] + Z # HASH COORDINATES OF THE 8 CUBE CORNERS
return lerp(w, lerp(v, lerp(u, grad(p[AA + 1], x , y , z ), # AND ADD
grad(p[BA + 1], x - 1, y , z )), # BLENDED
lerp(u, grad(p[AB + 1], x , y - 1, z ), # RESULTS
grad(p[BB + 1], x - 1, y - 1, z ))), # FROM 8
lerp(v, lerp(u, grad(p[AA + 2], x , y , z - 1 ), # CORNERS
grad(p[BA + 2], x - 1, y , z - 1)), # OF CUBE.
lerp(u, grad(p[AB + 2], x , y - 1, z - 1 ),
grad(p[BB + 2], x - 1, y - 1, z - 1))))
end
println("Perlin noise applied to (3.14, 42.0, 7.0) gives ", perlinsnoise(3.14, 42.0, 7.0))
- Output:
Perlin noise applied to (3.14, 42.0, 7.0) gives 0.13691995878400012
Kotlin
// version 1.1.3
object Perlin {
private val permutation = intArrayOf(
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
)
private val p = IntArray(512) {
if (it < 256) permutation[it] else permutation[it - 256]
}
fun noise(x: Double, y: Double, z: Double): Double {
// Find unit cube that contains point
val xi = Math.floor(x).toInt() and 255
val yi = Math.floor(y).toInt() and 255
val zi = Math.floor(z).toInt() and 255
// Find relative x, y, z of point in cube
val xx = x - Math.floor(x)
val yy = y - Math.floor(y)
val zz = z - Math.floor(z)
// Compute fade curves for each of xx, yy, zz
val u = fade(xx)
val v = fade(yy)
val w = fade(zz)
// Hash co-ordinates of the 8 cube corners
// and add blended results from 8 corners of cube
val a = p[xi] + yi
val aa = p[a] + zi
val ab = p[a + 1] + zi
val b = p[xi + 1] + yi
val ba = p[b] + zi
val bb = p[b + 1] + zi
return 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))))
}
private fun fade(t: Double) = t * t * t * (t * (t * 6 - 15) + 10)
private fun lerp(t: Double, a: Double, b: Double) = a + t * (b - a)
private fun grad(hash: Int, x: Double, y: Double, z: Double): Double {
// Convert low 4 bits of hash code into 12 gradient directions
val h = hash and 15
val u = if (h < 8) x else y
val v = if (h < 4) y else if (h == 12 || h == 14) x else z
return (if ((h and 1) == 0) u else -u) +
(if ((h and 2) == 0) v else -v)
}
}
fun main(args: Array<String>) {
println(Perlin.noise(3.14, 42.0, 7.0))
}
- Output:
0.13691995878400012
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)
- Output:
0.136919958784
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
Nim
import math
const 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
]
const P = static:
var a: array[512, int]
for i, val in Permutation:
a[i] = val
a[i + 256] = val
a
func fade(t: float): float = t * t * t * (t * (t * 6 - 15) + 10)
func lerp(t, a, b: float): float = a + t * (b - a)
func grad(hash: int; x, y, z: float): float =
## Convert low 4 bits of hash code into 12 gradient directions.
let h = hash and 15
let u = if h < 8: x else: y
let v = if h < 4: y elif h == 12 or h == 14: x else: z
result = (if (h and 1) == 0: u else: -u) + (if (h and 2) == 0: v else: -v)
func noise(x, y, z: float): float =
# Find unit cube that contains point.
let
xi = int(x) and 255
yi = int(y) and 255
zi = int(z) and 255
# Find relative x, y, z of point in cube.
let
x = x - floor(x)
y = y - floor(y)
z = z - floor(z)
# Compute fade curves for each of x, y, z.
let
u = fade(x)
v = fade(y)
w = fade(z)
# Hash coordinates of the 8 cube corners and
# add blended results from 8 corners of cube.
let
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
result = 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))))
when isMainModule:
echo noise(3.14, 42, 7)
- Output:
0.1369199587840001
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;
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]
let lerp (t, a, b) = a +. t *. (b -. a)
let fade t =
(t *. t *. t) *. (t *. (t *. 6. -. 15.) +. 10.)
let grad (hash, x, y, z) =
let h = hash land 15 in
let u = if (h < 8) then x else y in
let v = if (h < 4) then y else (if (h = 12 || h = 14) then x else z) in
(if (h land 1 = 0) then u else (0. -. u)) +.
(if (h land 2 = 0) then v else (0. -. v))
let perlin_init p =
List.rev (List.fold_left (fun i x -> x :: i) (List.rev p) p);;
let perlin_noise p x y z =
let x1 = (int_of_float x) land 255 and
y1 = (int_of_float y) land 255 and
z1 = (int_of_float z) land 255 and
xi = x -. (float (int_of_float x)) and
yi = y -. (float (int_of_float y)) and
zi = z -. (float (int_of_float z)) in
let u = fade xi and
v = fade yi and
w = fade zi and
a = (List.nth p x1) + y1 in
let aa = (List.nth p a) + z1 and
ab = (List.nth p (a + 1)) + z1 and
b = (List.nth p (x1 + 1)) + y1 in
let ba = (List.nth p b) + z1 and
bb = (List.nth p (b + 1)) + z1 in
lerp(w, lerp(v, lerp(u, (grad ((List.nth p aa), xi, yi, zi)),
(grad ((List.nth p ba), xi -. 1., yi , zi))),
lerp(u, (grad ((List.nth p ab), xi , yi -. 1., zi)),
(grad ((List.nth p bb), xi -. 1., yi -. 1., zi)))),
lerp(v, lerp(u, (grad ((List.nth p (aa + 1)), xi, yi, zi -. 1.)),
(grad ((List.nth p (ba + 1)), xi -. 1., yi , zi -. 1.))),
lerp(u, (grad ((List.nth p (ab + 1)), xi , yi -. 1., zi -. 1.)),
(grad ((List.nth p (bb + 1)), xi -. 1., yi -. 1., zi -. 1.)))))
;;
let p = perlin_init permutation in
print_string ((Printf.sprintf "%0.17f" (perlin_noise p 3.14 42.0 7.0)) ^ "\n")
- Output:
0.13691995878400012
Pascal
dumb copy of Go minimal adjustments.
program perlinNoise;
//Perlin Noise
//http://rosettacode.org/mw/index.php?title=Perlin_noise#Go
uses
sysutils;
type
float64 = double;
const
p{ermutation} : array[0..255] of byte = (
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);
function fade(t:float64):float64;inline;
begin
fade := ((t*6-15)*t + 10) * t*t*t;
end;
function lerp(t, a, b:float64):float64;inline;
Begin
lerp := t*(b-a)+a;
end;
function grad(hash:integer; x, y, z: float64):float64;
Begin
case (hash AND 15) of
0:
grad := x + y;
1:
grad := y - x;
2:
grad := x - y;
3:
grad := -x - y;
4:
grad := x + z;
5:
grad := z - x;
6:
grad := x - z;
7:
grad := -x - z;
8:
grad := y + z;
9:
grad := z - y;
10:
grad := y - z;
11:
grad := -y - z;
12:
grad := x + y;
13:
grad := z - y;
14:
grad := y - x;
15:
grad := -y - z;
end;
end;
function noise(x, y, z: float64):float64;
var
u,v,w : float64;
a,b,c,A0,A1,A2,B0,B1,B2 : Integer;
Begin
a := trunc(x) AND 255;
b := trunc(y) AND 255;
c := trunc(z) AND 255;
x := frac(x);
y := frac(y);
z := frac(z);
u := fade(x);
v := fade(y);
w := fade(z);
A0 := p[ a] + b;
A1 := p[A0] + c;
A2 := p[A0+1] + c;
B0 := p[ a+1] + b;
B1 := p[B0] + c;
B2 := p[B0+1] + c;
noise:= lerp(w, lerp(v, lerp(u, grad(p[A1], x, y, z),
grad(p[B1], x-1, y, z)),
lerp(u, grad(p[A2], x, y-1, z),
grad(p[B2], x-1, y-1, z))),
lerp(v, lerp(u, grad(p[A1+1], x, y, z-1),
grad(p[B1+1], x-1, y, z-1)),
lerp(u, grad(p[A2+1], x, y-1, z-1),
grad(p[B2+1], x-1, y-1, z-1))))
end;
Begin
writeln(noise(3.14, 42, 7):20:17);
end.
- Output:
0.13691995878400012
Perl
use strict;
use warnings;
use experimental 'signatures';
use constant permutation => qw{
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};
use constant p => permutation, permutation;
sub floor ($x) { my $xi = int($x); return $x < $xi ? $xi - 1 : $xi }
sub fade ($t) { $t**3 * ($t * ($t * 6 - 15) + 10) }
sub lerp ($t, $a, $b) { $a + $t * ($b - $a) }
sub grad ($h, $x, $y, $z) {
$h &= 15;
my $u = $h < 8 ? $x : $y;
my $v = $h < 4 ? $y : ($h == 12 or $h == 14) ? $x : $z;
(($h & 1) == 0 ? $u : -$u) + (($h & 2) == 0 ? $v : -$v);
}
sub noise ($x, $y, $z) {
my ($X, $Y, $Z) = map { 255 & floor $_ } $x, $y, $z;
my ($u, $v, $w) = map { fade $_ } $x -= $X, $y -= $Y, $z -= $Z;
my $A = (p)[$X] + $Y;
my ($AA, $AB) = ( (p)[$A] + $Z, (p)[$A + 1] + $Z );
my $B = (p)[$X + 1] + $Y;
my ($BA, $BB) = ( (p)[$B] + $Z, (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;
- Output:
0.136919958784
Phix
with javascript_semantics constant ph = x"97 A0 89 5B 5A 0F 83 0D C9 5F 60 35 C2 E9 07 E1"& x"8C 24 67 1E 45 8E 08 63 25 F0 15 0A 17 BE 06 94"& x"F7 78 EA 4B 00 1A C5 3E 5E FC DB CB 75 23 0B 20"& x"39 B1 21 58 ED 95 38 57 AE 14 7D 88 AB A8 44 AF"& x"4A A5 47 86 8B 30 1B A6 4D 92 9E E7 53 6F E5 7A"& x"3C D3 85 E6 DC 69 5C 29 37 2E F5 28 F4 66 8F 36"& x"41 19 3F A1 01 D8 50 49 D1 4C 84 BB D0 59 12 A9"& x"C8 C4 87 82 74 BC 9F 56 A4 64 6D C6 AD BA 03 40"& x"34 D9 E2 FA 7C 7B 05 CA 26 93 76 7E FF 52 55 D4"& x"CF CE 3B E3 2F 10 3A 11 B6 BD 1C 2A DF B7 AA D5"& x"77 F8 98 02 2C 9A A3 46 DD 99 65 9B A7 2B AC 09"& x"81 16 27 FD 13 62 6C 6E 4F 71 E0 E8 B2 B9 70 68"& x"DA F6 61 E4 FB 22 F2 C1 EE D2 90 0C BF B3 A2 F1"& x"51 33 91 EB F9 0E EF 6B 31 C0 D6 1F B5 C7 6A 9D"& x"B8 54 CC B0 73 79 32 2D 7F 04 96 FE 8A EC CD 5D"& x"DE 72 43 1D 18 48 F3 8D 80 C3 4E 42 D7 3D 9C B4", p = ph&ph 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 function grad(int hash, atom x, y, z) int h = and_bits(hash,15) atom u = iff(h<8 ? x : y), v = iff(h<4 ? y : iff(h==12 or h==14 ? x : z)) return iff(and_bits(h,1) == 0 ? u : -u) + iff(and_bits(h,2) == 0 ? v : -v) end function function noise(atom x, y, z) integer X = and_bits(x,255), Y = and_bits(y,255), Z = and_bits(z,255) x -= floor(x) y -= floor(y) z -= floor(z) atom u = fade(x), v = fade(y), w = fade(z) integer A = p[X+1]+Y, AA = p[A+1]+Z, AB = p[A+2]+Z, B = p[X+2]+Y, BA = p[B+1]+Z, BB = p[B+2]+Z return lerp(w,lerp(v,lerp(u,grad(p[AA+1], x , y , z ), grad(p[BA+1], x-1, y , z )), lerp(u,grad(p[AB+1], x , y-1, z ), grad(p[BB+1], x-1, y-1, z ))), lerp(v,lerp(u,grad(p[AA+2], x , y , z-1 ), grad(p[BA+2], x-1, y , z-1 )), lerp(u,grad(p[AB+2], x , y-1, z-1 ), grad(p[BB+2], x-1, y-1, z-1 )))) end function printf(1,"Perlin Noise for (3.14,42,7) is %.17f\n",{noise(3.14,42,7)})
- Output:
Perlin Noise for (3.14,42,7) is 0.1369199587840001
PureBasic
; Perlin_noise
; http://www.rosettacode.org
DataSection
STARTDATA:
Data.i 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
ENDDATA:
EndDataSection
Procedure.i p(index.i)
ProcedureReturn PeekI(?STARTDATA+SizeOf(Integer)*index)
EndProcedure
Procedure.d fade(t.d)
ProcedureReturn ((t*6-15)*t+10)*t*t*t
EndProcedure
Procedure.d lerp(t.d,a.d,b.d)
ProcedureReturn t*(b-a)+a
EndProcedure
Procedure.d grad(hash.i, x.d,y.d,z.d)
Select hash & 15
Case 0 : ProcedureReturn x+y
Case 1 : ProcedureReturn y-x
Case 2 : ProcedureReturn x-y
Case 3 : ProcedureReturn -x-y
Case 4 : ProcedureReturn x+z
Case 5 : ProcedureReturn z-x
Case 6 : ProcedureReturn x-z
Case 7 : ProcedureReturn -x-y
Case 8 : ProcedureReturn y+z
Case 9 : ProcedureReturn z-y
Case 10 : ProcedureReturn y-z
Case 11 : ProcedureReturn -y-z
Case 12 : ProcedureReturn x+y
Case 13 : ProcedureReturn z-y
Case 14 : ProcedureReturn y-x
Case 15 : ProcedureReturn -y-z
EndSelect
EndProcedure
Procedure.d noise(x.d,y.d,z.d)
Define.d u,v,w
Define.i a,b,c,A0,A1,A2,B0,B1,B2
a=Int(x)&255
b=Int(y)&255
c=Int(z)&255
x=x-IntQ(x)
y=y-IntQ(y)
z=z-IntQ(z)
u=fade(x)
v=fade(y)
w=fade(z)
A0=p(a)+b
A1=p(A0)+c
A2=p(A0+1)+c
B0=p(a+1)+b
B1=p(B0)+c
B2=p(B0+1)+c
ProcedureReturn lerp(w,lerp(v,lerp(u,grad(p(A1),x,y,z),
grad(p(B1),x-1,y,z)),
lerp(u,grad(p(A2),x,y-1,z),
grad(p(B2),x-1,y-1,z))),
lerp(v,lerp(u,grad(p(A1+1),x,y,z-1),
grad(p(B1+1),x-1,y,z-1)),
lerp(u,grad(p(A2+1),x,y-1,z-1),
grad(p(B2+1),x-1,y-1,z-1))))
EndProcedure
If OpenConsole()
PrintN("noise(3.14,42,7) => "+StrD(noise(3.14,42,7),17))
Input()
EndIf
- Output:
noise(3.14,42,7) => 0.13691995878400012
Python
import math
def perlin_noise(x, y, z):
X = math.floor(x) & 255 # FIND UNIT CUBE THAT
Y = math.floor(y) & 255 # CONTAINS POINT.
Z = math.floor(z) & 255
x -= math.floor(x) # FIND RELATIVE X,Y,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.
w = fade(z)
A = p[X ]+Y; AA = p[A]+Z; AB = p[A+1]+Z # HASH COORDINATES OF
B = p[X+1]+Y; BA = p[B]+Z; 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 ))))
def fade(t):
return t ** 3 * (t * (t * 6 - 15) + 10)
def lerp(t, a, b):
return a + t * (b - a)
def grad(hash, x, y, z):
h = hash & 15 # CONVERT LO 4 BITS OF HASH CODE
u = x if h<8 else y # INTO 12 GRADIENT DIRECTIONS.
v = y if h<4 else (x if h in (12, 14) else z)
return (u if (h&1) == 0 else -u) + (v if (h&2) == 0 else -v)
p = [None] * 512
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 in range(256):
p[256+i] = p[i] = permutation[i]
if __name__ == '__main__':
print("%1.17f" % perlin_noise(3.14, 42, 7))
- Output:
0.13691995878400012
Racket
-- because we were asked to
#lang racket
(define (floor-to-255 x)
(bitwise-and (exact-floor x) #xFF))
(define (fade t)
(* t t t (+ 10 (* t (- (* t 6) 15)))))
(define (lerp t a b)
(+ a (* t (- b a))))
;; CONVERT LO 4 BITS OF HASH CODE INTO 12 GRADIENT DIRECTIONS.
(define (grad hsh x y z)
(define h (bitwise-and hsh #x0F))
(define u (if (< h 8) x y))
(define v (cond [(< h 4) y] [(or (= h 12) (= h 14)) x] [else z]))
(+ (if (bitwise-bit-set? h 0) (- u) u) (if (bitwise-bit-set? h 1) (- v) v)))
(define permutation
(vector
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))
(define p (make-vector 512))
(for ((offset (in-list '(0 256))))
(vector-copy! p offset permutation))
(define-syntax-rule (p-ref n)
(vector-ref p n))
(define (noise x y z)
(let*
(
;; FIND UNIT CUBE THAT CONTAINS POINT.
(X (floor-to-255 x))
(Y (floor-to-255 y))
(Z (floor-to-255 z))
; FIND RELATIVE X,Y,Z OF POINT IN CUBE.
(x (- x (floor x)))
(y (- y (floor y)))
(z (- z (floor z)))
;; COMPUTE FADE CURVES FOR EACH OF X,Y,Z.
(u (fade x))
(v (fade y))
(w (fade z))
;; HASH COORDINATES OF THE 8 CUBE CORNERS...
(A (+ (p-ref X) Y))
(AA (+ (p-ref A) Z))
(AB (+ (p-ref (add1 A)) Z))
(B (+ (p-ref (add1 X)) Y))
(BA (+ (p-ref B) Z))
(BB (+ (p-ref (add1 B)) Z)))
;; .. AND ADD BLENDED RESULTS FROM 8 CORNERS OF CUBE
(lerp
w
(lerp
v (lerp u (grad (p-ref AA) x y z) (grad (p-ref BA) (sub1 x) y z))
(lerp u (grad (p-ref AB) x (sub1 y) z) (grad (p-ref BB) (sub1 x) (sub1 y) z)))
(lerp
v
(lerp u (grad (p-ref (add1 AA)) x y (sub1 z)) (grad (p-ref (add1 BA)) (sub1 x) y (sub1 z)))
(lerp u (grad (vector-ref p (add1 AB)) x (sub1 y) (sub1 z))
(grad (vector-ref p (add1 BB)) (sub1 x) (sub1 y) (sub1 z)))))))
(module+ test
(noise 3.14 42 7))
- Output:
0.13691995878400012
Raku
(formerly Perl 6)
constant @p = flat { @_.sort.antipairs.Hash{@_} }(<
😅😎📸👑👐✌ 💻✊😻💀💁🍃😲🤦☺🤙🔵🌕💎🌎🎶🖕⚠💆🌖🤭❣⚽➡😮☹😂
🥰💣🤧🐷▶🌈😵🎁👼🦋🤐🙂💟🌔✅🌑🍒😟🌒👎🤪😃🍑👍😜❓💩📣😙😖🎵😝
🐶😓🏃📌🔴🌺🌊😔🐾😀😌🤣👉💙🤠💦🍻🙋💿🤢🤑💓👶🌞🍎🌸🥀🌚🤷💍🖤🍊
🎉⭐🎂😏☀🚩👆🐉🙈🐸💾😫🙇👏❄😗😹😴📍💸💞😬😍👌😒💋💗😶😛😪☎🎈
🍀🚶🤝🥵💨💧☝ 🙁🌗😁💡💪🪐👈👋🙌🙆🙅🍺🤞🌹✔🍓✨😤😭🌌🌟🤗😥😘🙏
💢🥳😆☄🌴😈😑🎼🤓😇💌😉😕🌱😚⚡💰❤🌘🧐❌💅💖💘👅💛🤘🤤😠😩💚💐
🛰 🥂💃🤟🥺🌓🤯😱🤫🙊🖥 ✈😯😡😐🤮👇🌿🗣 🤨🥴✋🤬💕🌻😰🚀🌏😣😷💔😋
😨👊🙃😞💝💥🌼🌷💫☕😄🧡🔥🤩🙄👻🤔💜🎤🌍⬇🏆🤲🕺💯😳👀🎊🚨🎀😊😢
>.join.comb) xx 2;
sub fade($_) { $_ * $_ * $_ * ($_ * ($_ * 6 - 15) + 10) }
sub lerp($t, $a, $b) { $a + $t * ($b - $a) }
sub grad($h is copy, $x, $y, $z) {
$h +&= 15;
my $u = $h < 8 ?? $x !! $y;
my $v = $h < 4 ?? $y !! $h == 12|14 ?? $x !! $z;
($h +& 1 ?? -$u !! $u) + ($h +& 2 ?? -$v !! $v);
}
sub noise($x is copy, $y is copy, $z is copy) {
my ($u, $v, $w) = map &fade, ($x, $y, $z) »-=«
my ($X, $Y, $Z) = ($x, $y, $z)».floor »+&» 255;
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;
- Output:
0.13691995878
REXX
Programming note: the REXX operator // is the remainder for division (not modulus), so the absolute value of the
remainder is used for this task.
The original decimal (data) table was compressed into a single hexadecimal value (without spaces).
/*REXX program implements a Perlin noise algorithm of a point in 3D─space. */
_= 97a0895b5a0f830dc95f6035c2e907e18c24671e458e086325f0150a17be0694f778ea4b001ac53e5efcdbcb75230b2039b12158ed953857ae147d88aba844af,
||4aa547868b301ba64d929ee7536fe57a3cd385e6dc695c29372ef528f4668f3641193fa101d85049d14c84bbd05912a9c8c4878274bc9f56a4646dc6adba0340,
||34d9e2fa7c7b05ca2693767eff5255d4cfce3be32f103a11b6bd1c2adfb7aad577f898022c9aa346dd99659ba72bac09811627fd13626c6e4f71e0e8b2b97068,
||daf661e4fb22f2c1eed2900cbfb3a2f1513391ebf90eef6b31c0d61fb5c76a9db854ccb07379322d7f0496fe8aeccd5dde72431d1848f38d80c34e42d73d9cb4
do j=0 for length(_)%2; @.j= x2d( substr(_, 2 * j + 1, 2) )
end /*j*/ /* [↑] assign an indexed array. */
parse arg x y z d . /*obtain optional arguments from the CL*/
if x=='' | x=="," then x= 3.14 /*Not specified? Then use the default.*/
if y=='' | y=="," then y= 42 /* " " " " " " */
if z=='' | z=="," then z= 7 /* " " " " " " */
if d=='' | d=="," then d= 100 /* " " " " " " */
numeric digits d /*use D decimal digits for precision.*/
say 'Perlin noise for the 3D point ['space(x y z, 3)"] ───► " PerlinNoise(x, y, z)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
fade: procedure; parse arg t; return t**3 * (t * (t * 6 - 15) + 10)
floor: procedure; parse arg x; _= x % 1; return _ - (x < 0) * (x \= _)
lerp: procedure; parse arg t,a,b; return a + t * (b - a)
pick: _= abs( arg(1) ) // 256; return @._
/*──────────────────────────────────────────────────────────────────────────────────────*/
grad: procedure; parse arg hash,x,y,z; _= abs(hash) // 16 /*force positive remainder*/
select
when _== 0 | _==12 then return x+y; when _== 1 | _==14 then return y-x
when _== 2 then return x-y; when _== 3 then return -x-y
when _== 4 then return x+z; when _== 5 then return z-x
when _== 6 then return x-z; when _== 7 then return -x-z
when _== 8 then return y+z; when _== 9 | _==13 then return z-y
when _==10 then return y-z
otherwise return -y-z /*for cases 11 or 15. */
end /*select*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
PerlinNoise: procedure expose @.; parse arg x,y,z
x$= floor(x) // 256; x = x - floor(x); xm= x-1; u= fade(x)
y$= floor(y) // 256; y = y - floor(y); ym= y-1; v= fade(y)
z$= floor(z) // 256; z = z - floor(z); zm= z-1; w= fade(z)
a = pick(x$ ) + y$; aa= pick(a) + z$; ab= pick(a +1) + z$
b = pick(x$ +1) + y$; ba= pick(b) + z$; bb= pick(b +1) + z$
return lerp(w, lerp(v, lerp(u, grad( pick(aa ), x , y , z ), ,
grad( pick(ba ), xm, y , z )), ,
lerp(u, grad( pick(ab ), x , ym, z ), ,
grad( pick(bb ), xm, ym, z ))), ,
lerp(v, lerp(u, grad( pick(aa+1), x , y , zm ), ,
grad( pick(ba+1), xm, y , zm )), ,
lerp(u, grad( pick(ab+1), x , ym, zm ), ,
grad( pick(bb+1), xm, ym, zm )))) /1
- output when using the internal default inputs:
(Output note: this REXX program uses 100 decimal digit precision, but 20 decimal digits would've been adequate.)
(Note that REXX uses decimal floating point, not binary.)
Perlin noise for the 3D point [3.14 42 7] ───► 0.136919958784
Rust
fn main() {
println!("{}", noise(3.14, 42.0, 7.0));
}
fn noise(x: f64, y: f64, z: f64) -> f64 {
let x0 = x.floor() as usize & 255;
let y0 = y.floor() as usize & 255;
let z0 = z.floor() as usize & 255;
let x = x - x.floor();
let y = y - y.floor();
let z = z - z.floor();
let u = fade(x);
let v = fade(y);
let w = fade(z);
let a = P[x0] + y0;
let aa = P[a] + z0;
let ab = P[a + 1] + z0;
let b = P[x0 + 1] + y0;
let ba = P[b] + z0;
let bb = P[b + 1] + z0;
return lerp(w,
lerp(v, lerp(u, grad(P[aa], x , y , z),
grad(P[ba], x-1.0, y , z)),
lerp(u, grad(P[ab], x , y-1.0, z),
grad(P[bb], x-1.0, y-1.0, z))),
lerp(v, lerp(u, grad(P[aa+1], x , y , z-1.0),
grad(P[ba+1], x-1.0, y , z-1.0)),
lerp(u, grad(P[ab+1], x , y-1.0, z-1.0),
grad(P[bb+1], x-1.0, y-1.0, z-1.0))));
}
fn fade(t: f64) -> f64 {
t * t * t * ( t * (t * 6.0 - 15.0) + 10.0)
}
fn lerp(t: f64, a: f64, b: f64) -> f64 {
a + t * (b - a)
}
fn grad(hash: usize, x: f64, y: f64, z: f64) -> f64 {
let h = hash & 15;
let u = if h < 8 { x } else { y };
let v = if h < 4 { y } else { if h == 12 || h == 14 { x } else { z } };
return if h&1 == 0 { u } else { -u } + if h&2 == 0 { v } else { -v };
}
static P: [usize; 512] = [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];
Output:
0.13691995878400012
Sidef
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
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 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' * 2 -> map {|n| Num(n, 36) })
func fade(n) { n * n * n * (n * (n * 6 - 15) + 10) }
func lerp(t, a, b) { a + t*(b-a) }
func grad(h, x, y, z) {
h &= 15
var u = (h < 8 ? x : y)
var v = (h < 4 ? y : (h ~~ [12,14] ? x : z))
(h&1 ? -u : u) + (h&2 ? -v : v)
}
func noise(x, y, z) {
var(X, Y, Z) = [x, y, z].map { .floor & 255 }...
var (u, v, w) = [x-=X, y-=Y, z-=Z].map { fade(_) }...
var (AA, AB) = with(p[X] + Y) {|i| (p[i] + Z, p[i+1] + Z) }
var (BA, BB) = with(p[X+1] + Y) {|i| (p[i] + Z, p[i+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))))
}
say noise(3.14, 42, 7)
- Output:
0.136919958784
Swift
import Foundation
struct Perlin {
private static 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, 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
]
private static let p = (0..<512).map({i -> Int in
if i < 256 {
return permutation[i]
} else {
return permutation[i - 256]
}
})
private static func fade(_ t: Double) -> Double { t * t * t * (t * (t * 6 - 15) + 10) }
private static func lerp(_ t: Double, _ a: Double, _ b: Double) -> Double { a + t * (b - a) }
private static func grad(_ hash: Int, _ x: Double, _ y: Double, _ z: Double) -> Double {
let h = hash & 15
let u = h < 8 ? x : y
let v = h < 4 ? y : h == 12 || h == 14 ? x : z
return (h & 1 == 0 ? u : -u) + (h & 2 == 0 ? v : -v)
}
static func noise(x: Double, y: Double, z: Double) -> Double {
let xi = Int(x) & 255
let yi = Int(y) & 255
let zi = Int(z) & 255
let xx = x - floor(x)
let yy = y - floor(y)
let zz = z - floor(z)
let u = fade(xx)
let v = fade(yy)
let w = fade(zz)
let a = p[xi] + yi
let aa = p[a] + zi
let b = p[xi + 1] + yi
let ba = p[b] + zi
let ab = p[a + 1] + zi
let bb = p[b + 1] + zi
return 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(x: 3.14, y: 42, z: 7))
- Output:
0.13691995878400012
Tcl
namespace eval perlin {
proc noise {x y z} {
# Find unit cube that contains point.
set X [expr {int(floor($x)) & 255}]
set Y [expr {int(floor($y)) & 255}]
set Z [expr {int(floor($z)) & 255}]
# Find relative x,y,z of point in cube.
set x [expr {$x - floor($x)}]
set y [expr {$y - floor($y)}]
set z [expr {$z - floor($z)}]
# Compute fade curves for each of x,y,z.
set u [expr {fade($x)}]
set v [expr {fade($y)}]
set w [expr {fade($z)}]
# Hash coordinates of the 8 cube corners...
variable p
set A [expr {p($X) + $Y}]
set AA [expr {p($A) + $Z}]
set AB [expr {p($A+1) + $Z}]
set B [expr {p($X+1) + $Y}]
set BA [expr {p($B) + $Z}]
set BB [expr {p($B+1) + $Z}]
# And add blended results from 8 corners of cube
return [expr {
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 ))))
}]
}
namespace eval tcl::mathfunc {
proc p {idx} {lindex $::perlin::permutation $idx}
proc fade {t} {expr { $t**3 * ($t * ($t * 6 - 15) + 10) }}
proc lerp {t a b} {expr { $a + $t * ($b - $a) }}
proc grad {hash x y z} {
# Convert low 4 bits of hash code into 12 gradient directions
set h [expr { $hash & 15 }]
set u [expr { $h<8 ? $x : $y }]
set v [expr { $h<4 ? $y : ($h==12 || $h==14) ? $x : $z }]
expr { (($h&1)==0 ? $u : -$u) + (($h&2)==0 ? $v : -$v) }
}
}
apply {{} {
binary scan [binary format H* [join {
97A0895B5A0F830DC95F6035C2E907E18C24671E458E086325F0150A17BE0694F7
78EA4B001AC53E5EFCDBCB75230B2039B12158ED953857AE147D88ABA844AF4AA5
47868B301BA64D929EE7536FE57A3CD385E6DC695C29372EF528F4668F3641193F
A101D85049D14C84BBD05912A9C8C4878274BC9F56A4646DC6ADBA034034D9E2FA
7C7B05CA2693767EFF5255D4CFCE3BE32F103A11B6BD1C2ADFB7AAD577F898022C
9AA346DD99659BA72BAC09811627FD13626C6E4F71E0E8B2B97068DAF661E4FB22
F2C1EED2900CBFB3A2F1513391EBF90EEF6B31C0D61FB5C76A9DB854CCB0737932
2D7F0496FE8AECCD5DDE72431D1848F38D80C34E42D73D9CB4
} ""]] cu* p
variable ::perlin::permutation [concat $p $p]
}}
}
puts [perlin::noise 3.14 42 7]
- Output:
0.13691995878400012
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
}
}
}
- Output:
0.13691995878400012
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,
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
]
var p = (0...512).map { |i| (i < 256) ? permutation[i] : permutation[i-256] }.toList
var fade = Fn.new { |t| t * t * t * (t * (t * 6 - 15) + 10) }
var lerp = Fn.new { |t, a, b| a + t * (b - a) }
var grad = Fn.new { |hash, x, y, z|
// Convert low 4 bits of hash code into 12 gradient directions
var h = hash & 15
var u = (h < 8) ? x : y
var v = (h < 4) ? y : (h == 12 || h == 14) ? x : z
return (((h & 1) == 0) ? u : -u) + (((h & 2) == 0) ? v : -v)
}
var noise = Fn.new { |x, y, z|
// Find unit cube that contains point
var xi = x.floor & 255
var yi = y.floor & 255
var zi = z.floor & 255
// Find relative x, y, z of point in cube
var xx = x.fraction
var yy = y.fraction
var zz = z.fraction
// Compute fade curves for each of xx, yy, zz
var u = fade.call(xx)
var v = fade.call(yy)
var w = fade.call(zz)
// Hash co-ordinates of the 8 cube corners
// and add blended results from 8 corners of cube
var a = p[xi] + yi
var aa = p[a] + zi
var ab = p[a + 1] + zi
var b = p[xi + 1] + yi
var ba = p[b] + zi
var bb = p[b + 1] + zi
return lerp.call(w,
lerp.call(v, lerp.call(u, grad.call(p[aa], xx, yy, zz), grad.call(p[ba], xx - 1, yy, zz)),
lerp.call(u, grad.call(p[ab], xx, yy - 1, zz), grad.call(p[bb], xx - 1, yy - 1, zz))),
lerp.call(v, lerp.call(u, grad.call(p[aa + 1], xx, yy, zz - 1), grad.call(p[ba + 1], xx - 1, yy, zz - 1)),
lerp.call(u, grad.call(p[ab + 1], xx, yy - 1, zz - 1), grad.call(p[bb + 1], xx - 1, yy - 1, zz - 1)))
)
}
System.print(noise.call(3.14, 42, 7))
- Output:
0.136919958784
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.));
]
- Output:
0.13691995878400000
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