Perlin noise: Difference between revisions
m
syntax highlighting fixup automation
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: did not need 'export') |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 14:
{{trans|Python}}
<
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)))</
{{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">
#include<stdlib.h>
#include<stdio.h>
Line 139:
return 0;
}
</syntaxhighlight>
Permutation file, it should have 256 integers in the range [0,255]:
<pre>
Line 151:
=={{header|Common Lisp}}==
<
(declaim (optimize (speed 3) (debug 0)))
Line 239:
(print (noise 3.14d0 42d0 7d0))
</syntaxhighlight>
{{out}}
<pre>0.13691995878400012d0</pre>
=={{header|D}}==
<
struct PerlinNoise {
Line 344:
im.savePGM("perlin_noise.pgm");
*/
}</
{{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</
with dataflow combinators:
<syntaxhighlight lang="text">[ floor >integer 255 bitand ] tri@</
<
sequences ;
IN: rosetta-code.perlin-noise
Line 420:
: main ( -- ) 3.14 42 7 noise . ;
MAIN: main</
{{out}}
<pre>
Line 430:
{{trans|Java}}
<
PROGRAM PERLIN
IMPLICIT NONE
Line 529:
END PROGRAM
</syntaxhighlight>
{{out}}
Line 540:
{{trans|PureBasic}}
{{trans|Phix}}
<
#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>
{{out}}
<pre>
Line 616:
=={{header|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);
Line 654:
return nf*nf*nf*nf;
}
</syntaxhighlight>
=={{header|Go}}==
<
import (
Line 745:
222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
}
var p = append(permutation, permutation...)</
{{out}}
<pre>
Line 755:
We can trivially copy the java implementation:
<
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>
And this gives us our desired result:
<
0.13692</
Or, asking to see 20 digits after the decimal point:
<syntaxhighlight lang="j">
22j20": ImprovedNoise 3.14 42 7
0.13691995878400012000</
=== 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:
<
fade=: 0 0 0 10 _15 6&p.
Line 860:
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</
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.
<
public final class ImprovedNoise {
Line 919:
};
static { for (int i=0; i < 256 ; i++) p[256+i] = p[i] = permutation[i]; }
}</
=={{header|Julia}}==
<
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))
</
<pre>
Perlin noise applied to (3.14, 42.0, 7.0) gives 0.13691995878400012
Line 979:
=={{header|Kotlin}}==
{{trans|Java}}
<
object Perlin {
Line 1,058:
fun main(args: Array<String>) {
println(Perlin.noise(3.14, 42.0, 7.0))
}</
{{out}}
Line 1,066:
=={{header|Lua}}==
<
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</
=={{header|Nim}}==
{{trans|Kotlin}}
<
const Permutation = [
Line 1,232:
when isMainModule:
echo noise(3.14, 42, 7)</
{{out}}
Line 1,239:
=={{header|OCaml}}==
<
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")</
{{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.
<
//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>
{{out}}
<pre> 0.13691995878400012</pre>
Line 1,417:
=={{header|Perl}}==
{{trans|Java}}
<
use warnings;
use experimental 'signatures';
Line 1,467:
}
print noise 3.14, 42, 7;</
{{out}}
<pre>0.136919958784</pre>
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,534:
=={{header|PureBasic}}==
{{trans|Pascal}}
<
; http://www.rosettacode.org
Line 1,625:
Input()
EndIf
</syntaxhighlight>
{{out}}
<pre>noise(3.14,42,7) => 0.13691995878400012</pre>
Line 1,633:
{{trans|Java}}
<
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))</
{{out}}
Line 1,693:
{{trans|Java}} -- because we were asked to
<
(define (floor-to-255 x)
(bitwise-and (exact-floor x) #xFF))
Line 1,765:
(module+ test
(noise 3.14 42 7))</
{{out}}
<pre>0.13691995878400012</pre>
Line 1,772:
(formerly Perl 6)
{{trans|Java}}
<syntaxhighlight lang="raku"
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;</
{{out}}
<pre>0.13691995878</pre>
Line 1,819:
The original decimal (data) table was compressed into a single hexadecimal value (without spaces).
<
_= 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</
{{out|output|text= when using the internal default inputs:}}
Line 1,876:
=={{header|Rust}}==
{{trans|Java}}
<
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];</
'''Output:'''
<pre>
Line 1,960:
=={{header|Sidef}}==
{{trans|Raku}}
<
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)</
{{out}}
<pre>
Line 2,004:
=={{header|Swift}}==
<
struct Perlin {
Line 2,077:
}
print(Perlin.noise(x: 3.14, y: 42, z: 7))</
{{out}}
Line 2,085:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
proc noise {x y z} {
# Find unit cube that contains point.
Line 2,152:
}
puts [perlin::noise 3.14 42 7]</
{{out}}
0.13691995878400012
Line 2,158:
=={{header|Vlang}}==
{{trans|go}}
<
// vlang doesn't have globals
Line 2,279:
}
}
}</
{{out}}
<pre>
Line 2,287:
=={{header|Wren}}==
{{trans|Kotlin}}
<
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))</
{{out}}
Line 2,362:
=={{header|XPL0}}==
{{trans|Java}}
<
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.));
]</
{{out}}
Line 2,440:
=={{header|zkl}}==
{{trans|Java}}
<
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);
}</
<
{{out}}
<pre>0.13692</pre>
|