[Neworleans-pm] Fwd: Perl 'Expert' Quiz of the Week #26 (Tk roller coaster simulation)

E. Strade, B.D. estrabd at yahoo.com
Thu Oct 21 09:56:38 CDT 2004




=====
http://www.brettsbsd.net/~estrabd

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

----- Original message -----
From: "Xavier Noria" <fxn at hashref.com>
To: perl-qotw at plover.com
Date: Thu, 21 Oct 2004 08:09:53 -0400
Subject: Perl 'Expert' Quiz of the Week #26 (Tk roller coaster
simulation) 


IMPORTANT: Please do not post solutions, hints, or other spoilers
        until at least 60 hours after the date of this message.
        Thanks.

IMPORTANTE: Por favor, no enviéis soluciones, pistas, o cualquier otra
        cosa que pueda echar a perder la resolución del problema hasta
        que hayan pasado por lo menos 60 horas desde el envío de este
        mensaje. Gracias.

BELANGRIJK: Stuur aub geen oplossingen, hints of andere tips in de
        eerste 60 uur na het verzendingstijdstip van dit
        bericht. Waarvoor dank.

UWAGA: Prosimy nie publikowac rozwiazan, dodatkowych  badz pomocniczych
	informacjii przez co najmniej 60 godzin od daty tej wiadomosci.
	Dziekuje.


----------------------------------------------------------------

You will write a program that simulates a two-dimensional roller 
coaster.

A 2-D roller coaster track is given by stdin as a sequence of scaled
line segments:

      ...
      153.167706326906 170.818594853651
      152.705297542038 170.778692088403
      152.230255082701 170.621111633323
      151.772089241241 170.340303566736
      ...

All numbers are positive floats.  The first coordinate (x) is between
0 and 600, and the second coordinate (y) is between 0 and 400. In
principle each 10 in the input file means 1 meter, so the roller
coasters described have a maximum height of 40 meters, but feel free
to play with the scale.  There are three examples here:

      http://perl.plover.com/qotw/misc/e026/quad.rc
      http://perl.plover.com/qotw/misc/e026/loops.rc
      http://perl.plover.com/qotw/misc/e026/huricane.rc

Write a Tk application that draws the track in a Tk::Canvas and
simulates the run of a car of mass m left to the effect of gravity at
the start.  

We assume there's no friction. The run ends either when some extreme of
the track is reached, or the car is stopped in a balanced position. A
track may make the car to go backwards at some point.

For instance, a possible solution would run loops.rc more or less like
this:

      http://perl.plover.com/qotw/misc/e026/loops.mov

Since physics is not the topic of QOTW here's a summary of an approach
to the problem in case you don't want to work that part out yourself.

Let's suppose that the car starts motionless at (50, 60) and slides
down a slope to (20, 20).  Its potential energy at the top is m*g*60,
where m is the mass of the car, and g is a constant that represents
the force of gravity.  At the bottom, the potential energy is only
m*g*20.  The law of conservation of energy says that the extra m*g*40
has to go somewhere, and in fact it has turned into kinetic energy,
which is the energy of the speed of the car.

Physics says that the kinetic energy (k) of the car is m* v**2 / 2,
where m is the mass of the car and v is the velocity.  So we have
m*g*20 = m*v**2/2, so v = sqrt(40g) at the bottom.  The average
velocity over the entire slope is half this, or sqrt(10g).  Since the
length of the slope is 50 units, the car takes 50/sqrt(10g) time units
to slide down the slope.  How fast it actually slides depends on g,
the force of gravity; if g is large it will slide faster, and if g is
zero there is no gravity, so it won't slide down at all; it will stay
at the top forever.  The gravity constant g can be built into your
program or supplied via a command-line option.

In summary:

         p = m*g*y
         k = m*(v**2)/2
         p = k
         v(average) = (v(start) + v(end))/2;
         t * v(average)   = l


      y = height above the ground at a particular time
      p = potential energy at that time
      m = mass of the car
      g = gravity constant

      k = kinetic energy at a particular time
      v = velocity of the car at that time

      v(start)   = velocity of the car at the start of a segment of
      track
      v(end)     = velocity of the car at the end of the segment
      v(average) = average velocity of the car over the segment

      l = length of the segment
      t = time to traverse the segment

To get a more accurate simulation, your program can divide each
straight segment of track into several smaller straight segments and
simulate each one separately.




More information about the NewOrleans-pm mailing list