How to resolve the algorithm Galton box animation step by step in the Yabasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Galton box animation step by step in the Yabasic programming language

Table of Contents

Problem Statement

A   Galton device   Sir Francis Galton's device   is also known as a   bean machine,   a   Galton Board,   or a   quincunx.

In a Galton box, there are a set of pins arranged in a triangular pattern.   A number of balls are dropped so that they fall in line with the top pin, deflecting to the left or the right of the pin.   The ball continues to fall to the left or right of lower pins before arriving at one of the collection points between and to the sides of the bottom row of pins. Eventually the balls are collected into bins at the bottom   (as shown in the image),   the ball column heights in the bins approximate a   bell curve.   Overlaying   Pascal's triangle   onto the pins shows the number of different paths that can be taken to get to each bin.

Generate an animated simulation of a Galton device.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Galton box animation step by step in the Yabasic programming language

Source code in the yabasic programming language

bola$ = "0000ff"
obst$ = "000000"

maxBalls = 10
cx = 1
cy = 2
dim Balls(maxBalls, 2)

open window 600,600
window origin "ct"

maxh = peek("winheight")

REM Draw the pins:

FOR row = 1 TO 7
  FOR col = 1 TO row
    FILL circle 40*col - 20*row, 40*row+80, 10
  NEXT col
NEXT row

REM Animate
tick = 0
bolas = 0
color 0,0,255

do
	if (bolas < maxBalls) then
		if tick = 3 then
			tick = 0
			bolas = bolas + 1
			Balls(bolas, cx) = 20
			Balls(bolas, cy) = 10
		end if
		tick = tick + 1
	end if
	for n = 1 to bolas
		if Balls(n, cy) then
			color$ = right$(getbit$(Balls(n,cx),Balls(n,cy) + 10,Balls(n,cx),Balls(n,cy) + 10),6)
			if (color$ = bola$) or (Balls(n,cy) >= maxh - 15) then
				Balls(n,cy) = 0
				break
			end if
			clear fill circle Balls(n,cx),Balls(n,cy),10
			if color$ = obst$ then
				if int(ran(2)) then
					Balls(n,cx) = Balls(n,cx) - 20
				else
					Balls(n,cx) = Balls(n,cx) + 20
				end if
			end if
			Balls(n,cy) = Balls(n,cy)+10
			fill circle Balls(n,cx),Balls(n,cy),10
			wait .001
		else
			wait .001
		end if
	next n
loop

  

You may also check:How to resolve the algorithm Undefined values step by step in the Prolog programming language
You may also check:How to resolve the algorithm User input/Graphical step by step in the Racket programming language
You may also check:How to resolve the algorithm Approximate equality step by step in the Haskell programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Nim programming language
You may also check:How to resolve the algorithm Jaro similarity step by step in the Java programming language