Babylonian spiral: Difference between revisions

→‎{{header|Raku}}: Reduce unnecessary calculations, use more efficient abstractions, reduce execution time about another 50%, other minor style twiddles
m (→‎Independent implementation: slices rather than explicit enumerations)
(→‎{{header|Raku}}: Reduce unnecessary calculations, use more efficient abstractions, reduce execution time about another 50%, other minor style twiddles)
Line 473:
:width<100%>, :height<100%>,
:rect[:width<100%>, :height<100%>, :style<fill:white;>],
:polyline[ :points((flat babylonianSpiral(10000)).join: ','),
:style("stroke:red; stroke-width:6; fill:white;"),
:transform("scale (.05, -.05) translate (1000,-10000)")
Line 491:
===Independent implementation===
 
Exact same output; less thanabout one fifthtenth the execution time.
 
<lang perl6>my @next = { :x(1), :y(1), :radius(2.sqrt)2hyp },;
 
sub next-interval (Int $int) {
@next.append: (0..$int).map: { %( :x($int), :y($_), :radiushyp(sqrt( $int² + .²) ))) };
@next = |@next.sort: { *.<radiushyp> };
}
 
my @spiral = [\»+«] lazy gather {
my $radiusinterval = 1;
take [0,0];
take my @tail = 0,1;
Line 507:
my \Θ = atan2 |@tail[1,0];
my @this = @next.shift;
@this.push: @next.shift while @next and @next[0]<radiushyp> == @this[0]<radiushyp>;
my @candidates = |@this.map: -> $test {
next-intervalmy (++$radius\i, \j) if $radius == $test.<x y>;
|((1, 1), (1, next-1), interval(-1, 1++$interval), (-1, -1) X«*»if $test<xinterval y>,== $test<y x>)i;
|((i,j),(-i,j),(i,-j),(-i,-j),(j,i),(-j,i),(j,-i),(-j,-i))
}
take @tail = |@candidates.min: { ( Θ - atan2 |.[1,0] ) % τ };
Line 518 ⟶ 519:
# The task
say "The first $_ Babylonian spiral points are:\n",
(@spiral[^$_].map: ({ sprintf '(%3d,%4d)', @|$_ }).batch(10).join(: "\n") given 40;
 
# Stretch
Line 527 ⟶ 528:
:width<100%>, :height<100%>,
:rect[:width<100%>, :height<100%>, :style<fill:white;>],
:polyline[ :points((flat @spiral[^10000]).join: ','),
:style("stroke:red; stroke-width:6; fill:white;"),
:transform("scale (.05, -.05) translate (1000,-10000)")
10,327

edits