Draw a sphere: Difference between revisions
→Pure Perl 6: modernize a bit, add concurrency
Thundergnat (talk | contribs) (→Pure Perl 6: modernize a bit, add concurrency) |
|||
Line 2,846:
The C code is modified to output a .pgm file.
{{works with|Rakudo|
[[File:Sphere-perl6.png|thumb]]
<lang perl6>my $
my @light = normalize([ 3, 2, -5 ]);
Line 2,856 ⟶ 2,855:
my $depth = 255;
sub MAIN ($outfile = 'sphere-perl6.pgm') {
spurt $outfile, "P5\n$
my $out = open( $outfile, :a, :bin ) or die "$!\n";
$out.write( Blob.new(draw_sphere( ($
$out.close;
}
sub normalize (@vec) {
sub dot (@x, @y) {
sub draw_sphere ( $rad, $k, $ambient ) {
my @pixels[$height];
my $r2 = $rad * $rad;
my @range = -$rad .. $rad;
my @row[$width];
@range.map: -> $y {
if (my $x2 =
my
my $intensity = dot(@light, @vector) ** $k + $ambient;
my $pixel = (0 max ($intensity * $depth).Int) min $depth;
}▼
@row[$y+$rad] = 0;
▲ }
}
@pixels[$x+$rad] = @row;
}
}</lang>
|