8
1
Print, on STDOUT, a pattern which shows in what direction a bouncing ball will take.
The following assumptions are made:
- The ball starts at the top left corner:
0, 0
with zero initial velocity. - Gravity is
9.8ms^-2
exactly, towards the floor (y positive.) - The ball weighs
500g
exactly. - The ball bounces at 45 or 135 degrees to the floor unless you want to add the appropriate calculations to add variable trajectories. (Bonus arbitrary points!)
- The ball has a
spring constantcoefficient of restitution/bouncyness constant of0.8
exactly. - The ball is perfectly spherical and does not deform when it bounces.
- The room is 25 characters high, 130 characters wide. Each x and y is 1 metre and each ball position represents a discrete sample -- the exact time period is deliberately unspecified, but the display should make the ball's path sufficiently clear. The output should show the path of the ball, not just the final position.
- The floor and ball should be indicated using characters on STDOUT, which may be the same. The presence of no ball or floor surface shall be indicated with a space character.
- You are allowed to assume rounding to three decimal places in any calculations. (Solutions using purely integers may be particularly interested in this rule.)
- The simulation stops when either the ball does not move from the floor or it leaves the room (
x > width of area
.) - The program must simulate the ball's path, not simply load it from a file or have it encoded somehow in the program. The test for this will be to optionally change one of the constants. If the program does not calculate a new, correct result, then it does not qualify.
Example output:
*
*
*
*
*
*
*
*
*
* ***********
* ***** ****
* **** ***
* *** ***
* ** **
* *** **
* ** ** *********
* * ** **** ****
* ** * ** **
* * ** ** **
* * * ** ** ********
* ** * ** ** **** ****
* * ** ** ** ** **
* * ** ** ** ** ** **
** ** ** ** ** ** **
* **** **** ***
**********************************************************************************************************************************
Determination of winner. I will accept the answer which:
- Meets the rules as defined above.
- Bonus featureness will be considered.
- Is the shortest and most elegant (subjective decision.)
1Is the input and/or starting speed (energy?) missing, or am I blind? As I see it now I could just draw the box and say the ball is stationary, or gzip your image and display it on every run. – shiona – 2013-01-22T15:13:36.180
@shiona The ball starts as if you have just let it go at 0,0. I will update the rules to clarify that actual calculation must take place. – Thomas O – 2013-01-22T15:19:00.400
1but If I decide the ball has the speed of 0.0001 m/s (or whatever small enough) to the direction of 45°, the ball does not visibly leave the ground with your chosen resolution. I think problems should always have some sort of an input (user, random, etc.) to make hard coding a single answer impossible. – shiona – 2013-01-22T15:39:22.230
0,0 is at the top left of the screen, so gravity will get it moving. It falls ~24 m in the first bounce. See the diagram. – Thomas O – 2013-01-22T15:40:00.053
4Are the stars on the left the y-axis or are they marking the ball's path? If they mark the ball's path, this physics simulations seems a little dubious because the ball has no motion component to the right, so when bouncing on a flat floor, it should bounce back straight up and not to the right. Also, the angles would get flatter on each bounce (if we have a motion component to the right). – Thomas W. – 2013-01-22T15:51:59.723
@ThomasW. They are the ball's path. As I said, the ball always bounces at 45 or 135 degrees, to make the simulation a little easier (no sin/cos necessary), and more interesting. I know a real ball wouldn't do this. – Thomas O – 2013-01-22T15:54:05.473
@ThomasO Oh, sorry. I was sure I missed something about the problem. I thought the ball was shot from the lower left. – shiona – 2013-01-22T17:05:30.633
"The ball has a spring constant of 0.8 exactly." What you apparently mean has nothing to do with a "spring constant" (that could only matter if the ball did in fact deform on bouncing), it is rather a kind of friction term. – ceased to turn counterclockwis – 2013-01-22T18:39:16.793
@leftaroundabout It's been a long time since I did physics. I'll call it the "bouncyness constant", if you want. – Thomas O – 2013-01-22T18:46:34.457
I like bouncyness constant! But that 45° thing, meh... – ceased to turn counterclockwis – 2013-01-22T20:04:57.773
Is the "spring constant" or "bouncyness constant" a coefficient of restitution, a coefficient of restitution squared, or what? – Peter Taylor – 2013-01-22T20:47:24.073
@Peter It's a simple coefficient of restitution. Upon researching it, that appears to be what I was thinking of when I typed spring constant. – Thomas O – 2013-01-22T20:56:48.090
I've adjusted the rules to allow for any trajectory. – Thomas O – 2013-01-22T21:02:59.700
1Is this
code-golf
orcode-challenge
or what ? Please tag appropriately. – Paul R – 2013-01-22T21:58:26.7671Given that the ball is simply dropped, what makes the ball move rightward? Why doesn't it simply move up and down? – DavidC – 2013-01-23T16:59:53.963
I'm with David on this -- how does your ball pick up that rightward motion, and why doesn't it gain more of it with every bounce? – boothby – 2013-03-24T20:12:22.500