From d300d2d851a72fce9470cd8386eec94e38d96a3b Mon Sep 17 00:00:00 2001 From: kbrannen Date: Fri, 21 Apr 2023 00:25:47 -0500 Subject: [PATCH 1/3] added 13-bounce for Perl --- 13_Bounce/perl/README.md | 3 ++ 13_Bounce/perl/bounce.pl | 91 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100755 13_Bounce/perl/bounce.pl diff --git a/13_Bounce/perl/README.md b/13_Bounce/perl/README.md index e69c8b81..6d213dba 100644 --- a/13_Bounce/perl/README.md +++ b/13_Bounce/perl/README.md @@ -1,3 +1,6 @@ Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) Conversion to [Perl](https://www.perl.org/) + +Added feature so that if "TIME" value is "0" then it will quit, +so you don't have to hit Control-C. Also added a little error checking of the input. diff --git a/13_Bounce/perl/bounce.pl b/13_Bounce/perl/bounce.pl new file mode 100755 index 00000000..023f6f04 --- /dev/null +++ b/13_Bounce/perl/bounce.pl @@ -0,0 +1,91 @@ +#!/usr/bin/perl + +# Bounce program in Perl +# Translated by Kevin Brannen (kbrannen) + +use strict; +use warnings; + +print "\n"; +print " " x 31,"BOUNCE\n"; +print " " x 15, "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n"; +print "\n\n\n"; + +print "THIS SIMULATION LETS YOU SPECIFY THE INITIAL VELOCITY\n"; +print "OF A BALL THROWN STRAIGHT UP, AND THE COEFFICIENT OF\n"; +print "ELASTICITY OF THE BALL. PLEASE USE A DECIMAL FRACTION\n"; +print "COEFFICIENCY (LESS THAN 1).\n\n"; +print "YOU ALSO SPECIFY THE TIME INCREMENT TO BE USED IN\n"; +print "'STROBING' THE BALL'S FLIGHT (TRY .1 INITIALLY).\n\n"; + +# deal with basic's tab() for line positioning +# line = line string we're starting with +# pos = position to start writing +# s = string to write +# returns the resultant string, which might not have been changed +sub line_tab +{ + my ($line, $pos, $s) = @_; + my $len = length($line); + # if curser is past position, do nothing + if ($len <= $pos) { $line .= " " x ($pos - $len) . $s; } + return $line; +} + +while (1) +{ + my @T; # time slice? + my $time_inc; # time increment, probably in fractions of seconds + my $velocity; # velocity in feet/sec + my $coeff_elas; # coeeficent of elasticity + my $L; # position on line + + # get input + print "TIME INCREMENT (SEC, 0=QUIT): "; chomp($time_inc = <>); + last if ($time_inc == 0); + print "VELOCITY (FPS): "; chomp($velocity = <>); + print "COEFFICIENT: "; chomp($coeff_elas = <>); + if ($coeff_elas >= 1.0 || $coeff_elas <= 0) + { + print "COEFFICIENT MUST BE > 0 AND < 1.0\n\n\n"; + next; + } + + print "\nFEET\n"; + my $S1 = int(70.0 / ($velocity / (16.0 * $time_inc))); + for my $i (1 .. $S1) + { + $T[$i] = $velocity * $coeff_elas ** ($i - 1) / 16.0; + } + + # draw graph + for (my $height=int(-16.0 * ($velocity / 32.0) ** 2.0 + $velocity ** 2.0 / 32.0 + .5) ; $height >= 0 ; $height -= .5) + { + #print "h=$height\n"; # kevin + if (int($height) == $height) { print sprintf("%2d", $height); } + else { print " "; } + $L = 0; + my $curr_line = ""; + for my $i (1 .. $S1) + { + my $time; + for ($time=0 ; $time <= $T[$i] ; $time += $time_inc) + { + $L += $time_inc; + next if (abs($height - (.5 * (-32) * $time ** 2.0 + $velocity * $coeff_elas ** ($i - 1) * $time)) > .25); + $curr_line = line_tab($curr_line, ($L / $time_inc), "0"); + } + $time = ($T[$i + 1] // 0) / 2; + last if (-16.0 * $time ** 2.0 + $velocity * $coeff_elas ** ($i - 1) * $time < $height); + } + print "$curr_line\n"; + } + + print " ."; + print "." x (int($L + 1) / $time_inc + 1), "\n"; + print " 0"; + my $cl = ""; + for my $i (1 .. int($L + .9995)) { $cl = line_tab($cl, int($i / $time_inc), $i); } + print "$cl\n"; + print " " x (int($L + 1) / (2 * $time_inc) - 2), "SECONDS\n\n"; +} From d24a5feb6b4e72f15016452844884c3355b48620 Mon Sep 17 00:00:00 2001 From: kbrannen Date: Fri, 21 Apr 2023 00:31:04 -0500 Subject: [PATCH 2/3] fixed a var name --- 13_Bounce/perl/bounce.pl | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/13_Bounce/perl/bounce.pl b/13_Bounce/perl/bounce.pl index 023f6f04..0d799514 100755 --- a/13_Bounce/perl/bounce.pl +++ b/13_Bounce/perl/bounce.pl @@ -38,7 +38,8 @@ while (1) my $time_inc; # time increment, probably in fractions of seconds my $velocity; # velocity in feet/sec my $coeff_elas; # coeeficent of elasticity - my $L; # position on line + my $line_pos; # position on line + my $S1 # duration in full seconds? # get input print "TIME INCREMENT (SEC, 0=QUIT): "; chomp($time_inc = <>); @@ -52,7 +53,7 @@ while (1) } print "\nFEET\n"; - my $S1 = int(70.0 / ($velocity / (16.0 * $time_inc))); + $S1 = int(70.0 / ($velocity / (16.0 * $time_inc))); for my $i (1 .. $S1) { $T[$i] = $velocity * $coeff_elas ** ($i - 1) / 16.0; @@ -64,28 +65,28 @@ while (1) #print "h=$height\n"; # kevin if (int($height) == $height) { print sprintf("%2d", $height); } else { print " "; } - $L = 0; + $line_pos = 0; my $curr_line = ""; for my $i (1 .. $S1) { my $time; for ($time=0 ; $time <= $T[$i] ; $time += $time_inc) { - $L += $time_inc; + $line_pos += $time_inc; next if (abs($height - (.5 * (-32) * $time ** 2.0 + $velocity * $coeff_elas ** ($i - 1) * $time)) > .25); - $curr_line = line_tab($curr_line, ($L / $time_inc), "0"); + $curr_line = line_tab($curr_line, ($line_pos / $time_inc), "0"); } - $time = ($T[$i + 1] // 0) / 2; + $time = ($T[$i + 1] // 0) / 2; # we can reach 1 past the end, use 0 if that happens last if (-16.0 * $time ** 2.0 + $velocity * $coeff_elas ** ($i - 1) * $time < $height); } print "$curr_line\n"; } print " ."; - print "." x (int($L + 1) / $time_inc + 1), "\n"; + print "." x (int($line_pos + 1) / $time_inc + 1), "\n"; print " 0"; - my $cl = ""; - for my $i (1 .. int($L + .9995)) { $cl = line_tab($cl, int($i / $time_inc), $i); } - print "$cl\n"; - print " " x (int($L + 1) / (2 * $time_inc) - 2), "SECONDS\n\n"; + my $curr_line = ""; + for my $i (1 .. int($line_pos + .9995)) { $curr_line = line_tab($curr_line, int($i / $time_inc), $i); } + print "$curr_line\n"; + print " " x (int($line_pos + 1) / (2 * $time_inc) - 2), "SECONDS\n\n"; } From 5ae3fe8774d845079591056ed71033162081e3a1 Mon Sep 17 00:00:00 2001 From: kbrannen Date: Fri, 21 Apr 2023 00:37:09 -0500 Subject: [PATCH 3/3] small fixes to make code look better --- 13_Bounce/perl/bounce.pl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/13_Bounce/perl/bounce.pl b/13_Bounce/perl/bounce.pl index 0d799514..4aeacf1f 100755 --- a/13_Bounce/perl/bounce.pl +++ b/13_Bounce/perl/bounce.pl @@ -62,7 +62,6 @@ while (1) # draw graph for (my $height=int(-16.0 * ($velocity / 32.0) ** 2.0 + $velocity ** 2.0 / 32.0 + .5) ; $height >= 0 ; $height -= .5) { - #print "h=$height\n"; # kevin if (int($height) == $height) { print sprintf("%2d", $height); } else { print " "; } $line_pos = 0; @@ -73,8 +72,10 @@ while (1) for ($time=0 ; $time <= $T[$i] ; $time += $time_inc) { $line_pos += $time_inc; - next if (abs($height - (.5 * (-32) * $time ** 2.0 + $velocity * $coeff_elas ** ($i - 1) * $time)) > .25); - $curr_line = line_tab($curr_line, ($line_pos / $time_inc), "0"); + if (abs($height - (.5 * (-32) * $time ** 2.0 + $velocity * $coeff_elas ** ($i - 1) * $time)) <= .25) + { + $curr_line = line_tab($curr_line, ($line_pos / $time_inc), "0"); + } } $time = ($T[$i + 1] // 0) / 2; # we can reach 1 past the end, use 0 if that happens last if (-16.0 * $time ** 2.0 + $velocity * $coeff_elas ** ($i - 1) * $time < $height); @@ -82,11 +83,15 @@ while (1) print "$curr_line\n"; } + # draw scale print " ."; print "." x (int($line_pos + 1) / $time_inc + 1), "\n"; print " 0"; my $curr_line = ""; - for my $i (1 .. int($line_pos + .9995)) { $curr_line = line_tab($curr_line, int($i / $time_inc), $i); } + for my $i (1 .. int($line_pos + .9995)) + { + $curr_line = line_tab($curr_line, int($i / $time_inc), $i); + } print "$curr_line\n"; print " " x (int($line_pos + 1) / (2 * $time_inc) - 2), "SECONDS\n\n"; }