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:
to# 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, };
and thus 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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |