How to resolve the algorithm Particle fountain step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Particle fountain step by step in the Perl programming language

Table of Contents

Problem Statement

Implement a particle fountain. Emulate a fountain of water droplets in a gravitational field being sprayed up and then falling back down. The particle fountain should be generally ordered but individually chaotic; the particles should be going mostly in the same direction, but should have slightly different vectors. Your fountain should have at least several hundred particles in motion at any one time, and ideally several thousand. It is optional to have the individual particle interact with each other. If at all possible, link to a short video clip of your fountain in action. Off-site link to a demo video

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Particle fountain step by step in the Perl programming language

Source code in the perl programming language

#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Particle_fountain
use warnings;
use Tk;

my $size = 900;
my @particles;
my $maxparticles = 500;
my @colors = qw( red green blue yellow cyan magenta orange white );

my $mw = MainWindow->new;
my $c = $mw->Canvas( -width => $size, -height => $size, -bg => 'black',
  )->pack;
$mw->Button(-text => 'Exit', -command => sub {$mw->destroy},
  )->pack(-fill => 'x');

step();
MainLoop;
-M $0 < 0 and exec $0;

sub step
  {
  $c->delete('all');
  $c->createLine($size / 2 - 10, $size, $size / 2, $size - 10,
    $size / 2 + 10, $size, -fill => 'white' );
  for ( @particles )
    {
    my ($ox, $oy, $vx, $vy, $color) = @$_;
    my $x = $ox + $vx;
    my $y = $oy + $vy;
    $c->createRectangle($ox, $oy, $x, $y, -fill => $color, -outline => $color);
    if( $y < $size )
      {
      $_->[0] = $x;
      $_->[1] = $y;
      $_->[3] += 0.006; # gravity :)
      }
    else { $_ = undef }
    }
  @particles = grep defined, @particles;
  if( @particles < $maxparticles and --$| )
    {
    push @particles, [ $size >> 1, $size - 10,
      (1 - rand 2) / 2.5 , -3 - rand 0.05, $colors[rand @colors] ];
    }
  $mw->after(1 => \&step);
  }


  

You may also check:How to resolve the algorithm Increment a numerical string step by step in the Scheme programming language
You may also check:How to resolve the algorithm Fibonacci word step by step in the zkl programming language
You may also check:How to resolve the algorithm HTTPS step by step in the Delphi programming language
You may also check:How to resolve the algorithm Function composition step by step in the BQN programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the PicoLisp programming language