Author Topic: Pascal's Triangle in Preparation - Completed: no need to view this thread  (Read 4276 times)

0 Members and 1 Guest are viewing this topic.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
I thought that I'd amuse myself by writing a graphics program representing the pinball demonstration of Pascal's Triangle.  A ball falls under gravity between a triangular array of pins on a board, at each pin the ball has an equal chance of falling to the right or left of the pin.  After a number of goes, the number of passes through each exit is given by the Pascal Triangle formula.

The final version will feature proper ball movement mechanics, acceleration under a constant force (gravity), friction and bouncing off the pins.  This preliminary demo just does the random left-right probabilities.  You will need to let the program run a number of goes to accumulate exit statistics.

Any suggestions for useful features?  Actual ball rolling (as opposed to sliding which will be my default) could be one.  I'll have a proper display for the exit counts (and compared to the Pascal Triangle figures), at least two balls - one travelling through the triangle and one returning from the bottom to Start.

You don't have to point out that this program will not do very much in the way of excitement!
Code: QB64: [Select]
  1. CONST BallDiam%% = 60, PinRad%% = 7, Spacing%% = BallDiam%% + 2 * PinRad%% + 6, XScreen% = 900, YScreen% = 700
  2. H! = Spacing%% * SQR(3 / 4)
  3.  
  4. DIM Pascal%(8)
  5.  
  6.  
  7. TempImg& = _NEWIMAGE(BallDiam%%, BallDiam%%, 32)
  8. _DEST TempImg&
  9. _PUTIMAGE (0, 0)-(BallDiam%% - 1, BallDiam%% - 1), _LOADIMAGE("whiteball.png", 32)
  10. Ball& = MakeHardware&(TempImg&)
  11.  
  12. TempImg& = _NEWIMAGE(PinRad%% * 2 + 1, PinRad%% * 2 + 1, 32)
  13. _DEST TempImg&
  14. FOR N%% = PinRad%% TO 1 STEP -1 '!!! TBF
  15.     CIRCLE (PinRad%%, PinRad%%), N%%, _RGB32(CINT(181 * (N%% + 5) / (PinRad%% + 5)), CINT(166 * (N%% + 5) / (PinRad%% + 5)), CINT(66 * (N%% + 5) / (PinRad%% + 5)))
  16.     PAINT (PinRad%%, PinRad%%), _RGB32(CINT(181 * (N%% + 5) / (PinRad%% + 5)), CINT(166 * (N%% + 5) / (PinRad%% + 5)), CINT(66 * (N%% + 5) / (PinRad%% + 5)))
  17. NEXT N%%
  18. Pin& = MakeHardware&(TempImg&)
  19.  
  20. XPosn%% = 0: YPosn%% = 0
  21.  
  22. SCREEN _NEWIMAGE(XScreen%, YScreen%, 32)
  23.  
  24.     _LIMIT 1
  25.     '_PUTIMAGE (287, 502), Ball&
  26.     _PUTIMAGE (CINT((XScreen% - BallDiam%% + (XPosn%% * Spacing%%)) \ 2), 40 + CINT(H! * YPosn%%) - (BallDiam%% \ 2)), Ball&
  27.     '_PUTIMAGE (350, 40), Pin&
  28.     'FOR N%% = 0 TO 7 'no. of rows !!! TBF
  29.     '    FOR M%% = -(N%% + 1) TO N%% + 1
  30.     '        _PUTIMAGE ((XScreen% \ 2) - (Spacing%% * (N%% + 1) / 2) + Spacing%% / 2 + M%% * Spacing%% - PinRad%%, 40 + H! * N%%), Pin&
  31.     '    NEXT M%%
  32.     'NEXT N%%
  33.  
  34.     FOR N%% = 0 TO 8 'no. of rows !!! TBF
  35.         FOR M%% = -(N%% + 1) TO (N%% + 1) STEP 2
  36.             _PUTIMAGE ((XScreen% \ 2) + CINT(M%% * Spacing%% / 2) - PinRad%%, 40 - PinRad%% + CINT(H! * N%%)), Pin&
  37.         NEXT M%%
  38.     NEXT N%%
  39.  
  40.     LOCATE 1, 1
  41.     FOR N%% = 0 TO 8
  42.         PRINT Pascal%(N%%);
  43.     NEXT N%%
  44.     PRINT
  45.  
  46.     _DISPLAY
  47.  
  48.  
  49.     YPosn%% = YPosn%% + 1
  50.     IF RND < 0.5 THEN
  51.         XPosn%% = XPosn%% - 1
  52.     ELSE
  53.         XPosn%% = XPosn%% + 1
  54.     END IF
  55.     IF YPosn%% = 8 THEN
  56.         Pascal%(4 + XPosn%% / 2) = Pascal%(4 + XPosn%% / 2) + 1
  57.     ELSEIF YPosn%% > 8 THEN
  58.         XPosn%% = 0: YPosn%% = 0
  59.         RANDOMIZE (TIMER)
  60.     END IF
  61.  
  62.  
  63.  
  64. FUNCTION MakeHardware& (Img&)
  65.     MakeHardware& = _COPYIMAGE(Img&, 33)
  66.     _FREEIMAGE Img&
  67.  
whiteball.png
* whiteball.png (Filesize: 72.74 KB, Dimensions: 300x300, Views: 253)
« Last Edit: May 15, 2019, 10:57:43 am by Qwerkey »

FellippeHeitor

  • Guest
Re: Pascal's Triangle (Pinball Demo) in Preparation
« Reply #1 on: April 26, 2019, 10:41:41 am »
I was hoping to see the balls pile up.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Pascal's Triangle (Pinball Demo) in Preparation
« Reply #2 on: April 26, 2019, 03:47:18 pm »
Qwerky, thanks I had not seen this interpretation of Pascal's triangle before, interesting.

The ball movement is OK just increase _LIMIT.

You could maybe get an image of the whole pin triangle to _PUTIMAGE instead of redrawing the pins in loops.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Pascal's Triangle (Pinball Demo) in Preparation
« Reply #3 on: April 26, 2019, 05:30:04 pm »
I started checking out this method of moving the ball for bunching up random numbers about a center point and ran into something I did not expect.

If you make an even amount of +1/-1 moves from 0, you will always be an even distance away from 0.
Likewise for odd amounts, you will always be an odd distance away from 0 (and never be back on 0).

Code: QB64: [Select]
  1. _TITLE "RND walk + or - 1" 'B+ 2019-04-26
  2.  
  3. steps = 5 'try odd and even numbers
  4.  
  5. 'if steps are even all distances from 0 are even
  6. 'if steps are odd then all distances from 0 are odd
  7. ' This had me scratching my head for a minute until I did it by hand on paper.
  8.  
  9. DIM results(-steps TO steps) AS INTEGER
  10. FOR j = 1 TO 1000
  11.     x = 0
  12.     FOR i = 1 TO steps
  13.         r = RND
  14.         IF r < .5 THEN x = x - 1 ELSE x = x + 1
  15.         'PRINT r, x
  16.     NEXT
  17.     'PRINT x,
  18.     results(x) = results(x) + 1
  19. 'SLEEP
  20. FOR i = -steps TO steps
  21.     PRINT i, results(i)
  22.     t = t + results(i)
  23.     IF i < 0 THEN lt = lt + results(i)
  24.     IF i > 0 THEN rt = rt + results(i)
  25. PRINT "  Total tested amt"; t
  26. PRINT "==============================="
  27. PRINT "Less than zero amt"; lt
  28. PRINT "       Equal 0 amt"; t - rt - lt
  29. PRINT "Greater than 0 amt"; rt
  30.  
  31.  

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Pascal's Triangle (Pinball Demo) in Preparation
« Reply #4 on: April 27, 2019, 04:01:26 am »
Fellippe, naturally when I first thought to do this I'd thought of making the balls pile up in columns - the good visual demonstration of Pascal's triangle.  However, the thing that interests me is simulating the mechanics of the balls rolling and bouncing off the pins and whether I could get the simulation to properly do the 50% left-right selection correctly.  That's the engineer talking cf. a mathematician!

For a 9-row triangle, the centre Pascal's triangle number in the ninth row is 70.  The vertical size of the simultaion would be very large and what I want to do is show the ball motions in detail.  So I believe that I'll have to sacrifice the column piling-up (but that would be more entertaining than displaying the numbers).  I might try allowing the piled-up balls to overlap - that might be a (rather silly) compromise.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Pascal's Triangle (Pinball Demo) in Preparation
« Reply #5 on: April 27, 2019, 05:44:36 am »
My memory of the machines which demonstrate this was a little hazy: in the actual machines, the balls do not fall uniquely onto the first row position but are spread out by extra pins (see attached image and check the Wiki page for a video en.wikipedia.org/wiki/Bean_machine).

I am assuming that my method will give the correct result. See image of this preliminary program after 30,000 ball runs, where that ratios relative to the expected Pascal Triangle numbers are given (normalised to the central column).  I was surprised at how large a number of runs was required to get all the column figures near to expected values, but that's randomness for you.

beanmachine.jpg
* beanmachine.jpg (Filesize: 80.36 KB, Dimensions: 320x561, Views: 190)
results.jpg
* results.jpg (Filesize: 51.73 KB, Dimensions: 913x736, Views: 220)