Jump to content

Perlin noise: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Raku}}: did not need 'export')
m (syntax highlighting fixup automation)
Line 14:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V p = [-1] * 512
V permutation = [151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
Line 68:
grad(:p[BB + 1], x - 1, y - 1, z - 1))))
 
print(‘#.17’.format(perlin_noise(3.14, 42, 7)))</langsyntaxhighlight>
 
{{out}}
Line 77:
=={{header|C}}==
A sign of how close C and Java are is this implementation which differs very little from Perlin's Java implementation. I only removed the static, public and final keywords and the Math class name from the floor calls, and it compiled without errors. Interestingly, in the paper, Perlin says that the benchmark implementation which was used to gauge the algorithm's performance was in C. One important improvement here is that the permutation data is externalized and read from a file. This way different textures and effects can be generated without having to change the source code.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 139:
return 0;
}
</syntaxhighlight>
</lang>
Permutation file, it should have 256 integers in the range [0,255]:
<pre>
Line 151:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">;;;; Translation from: Java
 
(declaim (optimize (speed 3) (debug 0)))
Line 239:
 
(print (noise 3.14d0 42d0 7d0))
</syntaxhighlight>
</lang>
{{out}}
<pre>0.13691995878400012d0</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.math;
 
struct PerlinNoise {
Line 344:
im.savePGM("perlin_noise.pgm");
*/
}</langsyntaxhighlight>
{{out}}
<pre>0.13691995878400012</pre>
Line 354:
 
As this is a strict translation of applicative code that makes heavy use of local variables, it is highly non-idiomatic. In idiomatic code, we would never write a word as long as <code>noise</code> or name so many throwaway values. We would also abstract out repetitive patterns such as
<syntaxhighlight lang="text">x floor >integer 255 bitand :> X
y floor >integer 255 bitand :> Y
z floor >integer 255 bitand :> Z</langsyntaxhighlight>
with dataflow combinators:
<syntaxhighlight lang="text">[ floor >integer 255 bitand ] tri@</langsyntaxhighlight>
<langsyntaxhighlight lang="factor">USING: kernel math math.functions literals locals prettyprint
sequences ;
IN: rosetta-code.perlin-noise
Line 420:
: main ( -- ) 3.14 42 7 noise . ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 430:
{{trans|Java}}
 
<langsyntaxhighlight lang="fortran">
PROGRAM PERLIN
IMPLICIT NONE
Line 529:
 
END PROGRAM
</syntaxhighlight>
</lang>
 
{{out}}
Line 540:
{{trans|PureBasic}}
{{trans|Phix}}
<langsyntaxhighlight lang="freebasic">
#define floor(x) ((x*2.0-0.5) Shr 1)
Dim Shared As Byte p(256) => { _
Line 608:
Print Using "El Ruido Perlin para (3.14, 42, 7) es #.################"; noise(3.14, 42, 7)
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 616:
=={{header|GLSL}}==
From https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83 :
<langsyntaxhighlight lang="glsl">
float rand(vec2 c){
return fract(sin(dot(c.xy ,vec2(12.9898,78.233))) * 43758.5453);
Line 654:
return nf*nf*nf*nf;
}
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 745:
222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
}
var p = append(permutation, permutation...)</langsyntaxhighlight>
{{out}}
<pre>
Line 755:
We can trivially copy the java implementation:
 
<langsyntaxhighlight Jlang="j">band=:17 b.
ImprovedNoise=:3 :0
'x y z'=. y
Line 821:
(lerp w,(lerp v,(lerp u,(grad(p{~AA),x, y, z), t7),t6),t4)
)
</syntaxhighlight>
</lang>
 
And this gives us our desired result:
 
<langsyntaxhighlight Jlang="j"> ImprovedNoise 3.14 42 7
0.13692</langsyntaxhighlight>
 
Or, asking to see 20 digits after the decimal point:
 
<syntaxhighlight lang="j">
<lang J>
22j20": ImprovedNoise 3.14 42 7
0.13691995878400012000</langsyntaxhighlight>
 
=== Simplified Expression ===
Line 838:
It's tempting, though, to express this more concisely. We are limited, there, by some of the arbitrary choices in the algorithm, but we can still exploit regularities in its structure:
 
<langsyntaxhighlight Jlang="j">p=:,~ 151 160 137 91 90 15 131 13 201 95 96 53 194 233 7 225 140 36 103 30 69 142 8 99 37 240 21 10 23 190 6 148 247 120 234 75 0 26 197 62 94 252 219 203 117 35 11 32 57 177 33 88 237 149 56 87 174 20 125 136 171 168 68 175 74 165 71 134 139 48 27 166 77 146 158 231 83 111 229 122 60 211 133 230 220 105 92 41 55 46 245 40 244 102 143 54 65 25 63 161 1 216 80 73 209 76 132 187 208 89 18 169 200 196 135 130 116 188 159 86 164 100 109 198 173 186 3 64 52 217 226 250 124 123 5 202 38 147 118 126 255 82 85 212 207 206 59 227 47 16 58 17 182 189 28 42 223 183 170 213 119 248 152 2 44 154 163 70 221 153 101 155 167 43 172 9 129 22 39 253 19 98 108 110 79 113 224 232 178 185 112 104 218 246 97 228 251 34 242 193 238 210 144 12 191 179 162 241 81 51 145 235 249 14 239 107 49 192 214 31 181 199 106 157 184 84 204 176 115 121 50 45 127 4 150 254 138 236 205 93 222 114 67 29 24 72 243 141 128 195 78 66 215 61 156 180
 
fade=: 0 0 0 10 _15 6&p.
Line 860:
v=. (1{uvw) lerp"1 u
w=. (2{uvw) lerp"1 v
)</langsyntaxhighlight>
 
And we can see that there's no difference in this result:
 
<langsyntaxhighlight Jlang="j"> 0.13691995878400012 - ImprovedNoise 3.14 42 7
0</langsyntaxhighlight>
 
It's probably possible to simplify this further.
Line 871:
=={{header|Java}}==
The original code from Perlin was originally published in java. Note that this does not have a main method so there will be no output. To test, add a main method, call noise() with 3.14,42,7, save the file as ImprovedNoise.java and compile.
<langsyntaxhighlight lang="java">// JAVA REFERENCE IMPLEMENTATION OF IMPROVED NOISE - COPYRIGHT 2002 KEN PERLIN.
 
public final class ImprovedNoise {
Line 919:
};
static { for (int i=0; i < 256 ; i++) p[256+i] = p[i] = permutation[i]; }
}</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const permutation = UInt8[
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233,
7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
Line 972:
 
println("Perlin noise applied to (3.14, 42.0, 7.0) gives ", perlinsnoise(3.14, 42.0, 7.0))
</langsyntaxhighlight>{{out}}
<pre>
Perlin noise applied to (3.14, 42.0, 7.0) gives 0.13691995878400012
Line 979:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
object Perlin {
Line 1,058:
fun main(args: Array<String>) {
println(Perlin.noise(3.14, 42.0, 7.0))
}</langsyntaxhighlight>
 
{{out}}
Line 1,066:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local p = {
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,
Line 1,147:
 
print (noise(3.14, 42, 7)) -- 0.136919958784
print (noise(1.4142, 1.2589, 2.718)) -- -0.17245663814988</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import math
 
const Permutation = [
Line 1,232:
 
when isMainModule:
echo noise(3.14, 42, 7)</langsyntaxhighlight>
 
{{out}}
Line 1,239:
=={{header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let permutation = [151;160;137;91;90;15;
131;13;201;95;96;53;194;233;7;225;140;36;103;30;69;142;8;99;37;240;21;10;23;
190; 6;148;247;120;234;75;0;26;197;62;94;252;219;203;117;35;11;32;57;177;33;
Line 1,296:
 
let p = perlin_init permutation in
print_string ((Printf.sprintf "%0.17f" (perlin_noise p 3.14 42.0 7.0)) ^ "\n")</langsyntaxhighlight>
{{out}}
<pre>0.13691995878400012</pre>
Line 1,303:
{{works with|Free Pascal}}
dumb copy of [http://rosettacode.org/mw/index.php?title=Perlin_noise#Go Go] minimal adjustments.
<langsyntaxhighlight lang="pascal">program perlinNoise;
//Perlin Noise
//http://rosettacode.org/mw/index.php?title=Perlin_noise#Go
Line 1,411:
writeln(noise(3.14, 42, 7):20:17);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre> 0.13691995878400012</pre>
Line 1,417:
=={{header|Perl}}==
{{trans|Java}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use experimental 'signatures';
Line 1,467:
}
 
print noise 3.14, 42, 7;</langsyntaxhighlight>
{{out}}
<pre>0.136919958784</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">ph</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">x"97 A0 89 5B 5A 0F 83 0D C9 5F 60 35 C2 E9 07 E1"</span><span style="color: #0000FF;">&</span>
Line 1,526:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Perlin Noise for (3.14,42,7) is %.17f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">noise</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3.14</span><span style="color: #0000FF;">,</span><span style="color: #000000;">42</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,534:
=={{header|PureBasic}}==
{{trans|Pascal}}
<langsyntaxhighlight PureBasiclang="purebasic">; Perlin_noise
; http://www.rosettacode.org
 
Line 1,625:
Input()
EndIf
</syntaxhighlight>
</lang>
{{out}}
<pre>noise(3.14,42,7) => 0.13691995878400012</pre>
Line 1,633:
{{trans|Java}}
 
<langsyntaxhighlight lang="python">def perlin_noise(x, y, z):
X = int(x) & 255 # FIND UNIT CUBE THAT
Y = int(y) & 255 # CONTAINS POINT.
Line 1,685:
 
if __name__ == '__main__':
print("%1.17f" % perlin_noise(3.14, 42, 7))</langsyntaxhighlight>
 
{{out}}
Line 1,693:
{{trans|Java}} -- because we were asked to
 
<langsyntaxhighlight lang="racket">#lang racket
(define (floor-to-255 x)
(bitwise-and (exact-floor x) #xFF))
Line 1,765:
 
(module+ test
(noise 3.14 42 7))</langsyntaxhighlight>
{{out}}
<pre>0.13691995878400012</pre>
Line 1,772:
(formerly Perl 6)
{{trans|Java}}
<syntaxhighlight lang="raku" perl6line>constant @p = map {:36($_)}, flat <
47 4G 3T 2J 2I F 3N D 5L 2N 2O 1H 5E 6H 7 69 3W 10 2V U 1X 3Y 8 2R 11 6O L A N
5A 6 44 6V 3C 6I 23 0 Q 5H 1Q 2M 70 63 5N 39 Z B W 1L 4X X 2G 6L 45 1K 2F 4U K
Line 1,809:
}
 
say noise 3.14, 42, 7;</langsyntaxhighlight>
{{out}}
<pre>0.13691995878</pre>
Line 1,819:
 
The original decimal (data) table was compressed into a single hexadecimal value (without spaces).
<langsyntaxhighlight lang="rexx">/*REXX program implements a Perlin noise algorithm of a point in 3D─space. */
_= 97a0895b5a0f830dc95f6035c2e907e18c24671e458e086325f0150a17be0694f778ea4b001ac53e5efcdbcb75230b2039b12158ed953857ae147d88aba844af,
||4aa547868b301ba64d929ee7536fe57a3cd385e6dc695c29372ef528f4668f3641193fa101d85049d14c84bbd05912a9c8c4878274bc9f56a4646dc6adba0340,
Line 1,864:
grad( pick(ba+1), xm, y , zm )), ,
lerp(u, grad( pick(ab+1), x , ym, zm ), ,
grad( pick(bb+1), xm, ym, zm )))) /1</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
 
Line 1,876:
=={{header|Rust}}==
{{trans|Java}}
<langsyntaxhighlight lang="rust">fn main() {
println!("{}", noise(3.14, 42.0, 7.0));
}
Line 1,952:
210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, 49,192,214, 31,
181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,138,236,205,
93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180];</langsyntaxhighlight>
'''Output:'''
<pre>
Line 1,960:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">const p = (%w'47 4G 3T 2J 2I F 3N D 5L 2N 2O 1H 5E 6H 7 69 3W 10 2V U 1X 3Y 8
2R 11 6O L A N 5A 6 44 6V 3C 6I 23 0 Q 5H 1Q 2M 70 63 5N 39 Z B W 1L 4X X 2G
6L 45 1K 2F 4U K 3H 3S 4R 4O 1W 4V 22 4L 1Z 3Q 3V 1C R 4M 25 42 4E 6F 2B 33 6D
Line 1,996:
}
 
say noise(3.14, 42, 7)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,004:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
struct Perlin {
Line 2,077:
}
 
print(Perlin.noise(x: 3.14, y: 42, z: 7))</langsyntaxhighlight>
 
{{out}}
Line 2,085:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">namespace eval perlin {
proc noise {x y z} {
# Find unit cube that contains point.
Line 2,152:
}
 
puts [perlin::noise 3.14 42 7]</langsyntaxhighlight>
{{out}}
0.13691995878400012
Line 2,158:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">import math
 
// vlang doesn't have globals
Line 2,279:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,287:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">var permutation = [
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225,
140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148,
Line 2,353:
}
 
System.print(noise.call(3.14, 42, 7))</langsyntaxhighlight>
 
{{out}}
Line 2,362:
=={{header|XPL0}}==
{{trans|Java}}
<langsyntaxhighlight XPL0lang="xpl0">func real Noise; real X, Y, Z;
real U, V, W;
int IX, IY, IZ, P(512), A, AA, AB, B, BA, BB;
Line 2,431:
[Format(1, 17);
RlOut(0, Noise(3.14, 42., 7.));
]</langsyntaxhighlight>
 
{{out}}
Line 2,440:
=={{header|zkl}}==
{{trans|Java}}
<langsyntaxhighlight lang="zkl">class [static] ImprovedNoise{ // a container, not an object
fcn noise(xyz){ xyz=vm.arglist.apply("toFloat");
X,Y,Z:= // FIND UNIT CUBE THAT CONTAINS POINT.
Line 2,481:
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180),
p=Data(Void,permutation,permutation);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">ImprovedNoise.noise(3.14, 42, 7).println();</langsyntaxhighlight>
{{out}}
<pre>0.13692</pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.