I've looked at this a bit, with an eye to making it handle higher dimensions, and one of the easy changes is that the F and G constants can be simplified and extended:

# Skewing and unskewing factors for 2, 3, and 4 dimensions use constant { F2 => 0.5 * (sqrt(3.0) - 1.0), F3 => 1.0 / 3.0, F4 => (sqrt(5.0) - 1.0) / 4.0, G2 => (3.0 - sqrt(3.0)) / 6.0, G3 => 1.0 / 6.0, G4 => (5.0 - sqrt(5.0)) / 20.0, };
to
F2 => (sqrt(3.0) - 1.0) / 2.0, F3 => (sqrt(4.0) - 1.0) / 3.0, F4 => (sqrt(5.0) - 1.0) / 4.0, G2 => (3.0 - sqrt(3.0)) / 6.0, #/3*2 G3 => (4.0 - sqrt(4.0)) / 12.0, #/4*3 #1.0 / 6.0, G4 => (5.0 - sqrt(5.0)) / 20.0, #/5*4
and thus to:
sub FFactor { my $numDimensions = shift; return sqrt($numDimensions+1) -1 / $numDimensions; } sub GFactor { my $numDimensions = shift; return ($numDimensions+1 -sqrt($numDimensions+1))/($numDimensions* +$numDimensions+1); }

Playing around with 2D noise, stacking noise on multiple scales makes for really interesting pictures. I've tacked on a BMP output function to keep it all self-contained.

Example picture: http://imgur.com/aD6cQZ9

Image generation and BMP saving code below:

my $N = 256; my @pic = (); for my $i (1 .. $N) { my $x = $i / 1; my @row = (); for my $j (1 .. $N) { my $y = $j / 1; my $pixel; #$pixel = (((noise $x/$_, $y/$_)+ 1) / 2 * 256) for (512); $pixel = 1; #my $decay = .25; #$pixel = $pixel*(1-$decay) + (((noise $x/$_, $y/$_)+ 1) / 2 * + 256*$decay) for reverse (512, 128, 64, 16, );#4); $pixel *= (((noise $x/$_, $y/$_)+ 1) / 2) ** .5 for reverse (5 +12, 128, 64, 32, );#4); push @row, int($pixel*256); } push @pic, \@row; } BMP32bit('test.bmp', [0,$N-1,0,$N-1], \@pic, \@pic, \@pic); sub BMP32bit { my $filename = shift; my $coords = shift // [0,0,0,0]; my @channels = @_[0..3]; open my $outFH, '>', $filename or die "Can't open $filename for wr +ite: $!\n"; binmode $outFH; my ($top, $bottom, $left, $right) = (map {$_ // 0} @$coords); my ($width, $height) = (abs($left-$right)+1, abs($top-$bottom)+1); print "Image size ($width, $height)\n"; my $filesize = 0x36 + $height*4 * $width; #scalar $image->{channel +s}{red_top}; # BMP Header print $outFH pack('A2LSSL', 'BM', $filesize, 0,0, 0x36); # DIB header print $outFH pack('LLLSSLLLLLL', 40, $width, $height , 1, 32, 0, $ +filesize-0x36, 7874, 7874, 0, 0); #7874 pixels per meter == 200 dpi for (0..3) { $channels[$_] = [] unless ref($channels[$_]) eq 'ARRAY'; } for my $row ($top > $bottom ? ($bottom..$top) : (reverse $top..$bo +ttom)) { for my $col ( $left > $right ? ($right..$left) : (reverse $lef +t..$right)) { print $outFH pack('C4', $channels[0][$col][$row]//0, $channels[1][$col][$row]//0, $channels[2][$col][$row]//0, $channels[3][$col][$row]//0xff, ); } } }


In reply to Re: Translating simplex noise code from java to perl by SuicideJunkie
in thread Translating simplex noise code from java to perl by grondilu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.