From a35769b7673bf845fa0be43fc622e56354782ce8 Mon Sep 17 00:00:00 2001 From: Andinus Date: Tue, 2 Nov 2021 22:57:56 +0530 Subject: Generate images for every iteration --- lib/Fornax/CLI.rakumod | 55 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/Fornax/CLI.rakumod b/lib/Fornax/CLI.rakumod index 78162e4..ffbe813 100644 --- a/lib/Fornax/CLI.rakumod +++ b/lib/Fornax/CLI.rakumod @@ -1,17 +1,66 @@ use Cairo; subset Directory of Str where *.IO.d; -proto MAIN(|) is export { unless so @*ARGS { say $*USAGE; exit }; {*} } +proto MAIN(|) is export { unless so @*ARGS { put $*USAGE; exit }; {*} } #| Collection of tools to visualize Path Finding Algorithms multi sub MAIN( Str $script, #= script to run (e.g. java/DFS) - Directory :$algorithms = 'algorithms/', #= algorithms directory + Directory :$algorithms = 'algorithms', #= algorithms directory + Directory :$output = 'output', #= output directory ) is export { my Str $interpreter = $script.split("/").first; - my IO() $program-path = $algorithms ~ $script ~ '.' ~ $interpreter; + my IO() $program-path = "$algorithms/$script.$interpreter"; die "Program path invalid" unless $program-path.IO.f; + + my $proc = run «$interpreter $program-path», :out; + my @out = $proc.out.slurp(:close).lines; + + my Int() $rows = @out[0].split(":")[0]; + my Int() $columns = @out[0].split(":")[1]; + + my Int $c-width = 512; + my Int $c-height = 512; + + for @out[1].split(" ", :skip-empty) -> $iter { + my $file = "$output/" ~ $++ ~ ".svg"; + given Cairo::Surface::SVG.create($file, $c-width, $c-height) { + given Cairo::Context.new($_) { + my $COMPLETE = so $iter.comb.first eq "|"; + my @iter-corrected = $COMPLETE ?? $iter.comb.skip !! $iter.comb; + my @grid = @iter-corrected.rotor: $columns; + + die "Invalid grid: $iter" unless @grid.elems == $rows; + + my $x-grid = $c-width / $rows; + my $y-grid = $c-height / $columns; + + for ^$rows -> $r { + for ^$columns -> $c { + .rectangle($c * $y-grid, $r * $x-grid, $y-grid, $x-grid); + + given @grid[$r][$c] -> $cell { + when $cell eq 'x' { + .rgba(192 / 255, 239 / 255, 255 / 255, 0.8); + .rgba(174 / 255, 207 / 255, 144 / 255, 1) if $COMPLETE; + } + when $cell eq '#' { .rgba(0, 0, 0, 0.5); } + when $cell eq '$' { .rgba(174 / 255, 207 / 255, 144 / 255, 1); } + when $cell eq '_' { .rgba(0, 0, 0, 0.1); } + default { .rgb(1, 0, 0); } + } + .fill :preserve; + + .rgba(0, 0, 0, 0.6); + .rectangle($c * $y-grid, $r * $x-grid, $y-grid, $x-grid); + .stroke; + } + } + } + .finish; + } + } } multi sub MAIN( -- cgit 1.4.1-2-gfad0