plot.rgl.coin {TeachingDemos}R Documentation

Animated die roll or coin flip

Description

Open an rgl window, plot either a representation of a coin or a die then animate the flipping/rolling.

Usage

plot.rgl.coin(x, col = "black", heads = x[[1]], tails = x[[2]], ...)

plot.rgl.die(x=1:6, col.cube = "white", col.pip = "black", sides = x, ...)

flip.rgl.coin(side = sample(2, 1), steps = 150)

roll.rgl.die(side = sample(6, 1), steps = 250)

Arguments

x for plot.rgl.coin a list with information for drawing the faces of the coin, defaults to coin.faces. For plot.rgl.die a vector with the number of pips to put on the sides of the die (alternative way of specifying sides).
col Color of lines on the coin faces.
heads Design to use as "heads" side of coin.
tails Design to use as "tails" side of coin.
col.cube Color of the cube for the die.
col.pip Color of the pips (spots) on the die
sides Vector of length 6 indicating which numbers to show on the die.
side Which side of the coin (1 or 2) or die (1 through 6) should end up face up.
steps The number of steps in each part of the animation, higher values will be smoother and slower, lower values will be faster but more jumpy.
... Currently any additional options are silently ignored.

Details

You must use the plot function first to create the coin or die, then use the flip or roll function to start the animation. You can animate multiple times for a single use of the plotting function.

You can manually rotate the image as well, see the rgl package for details.

The defaults plot a regular coin and die, but arguments are available to create special casses (2 headed coin, die with 2 6's and no 1, ...).

The data list coin.faces contains information on designs for the faces of the coins in case you want to choose a different design.

The default rolling and flipping options ranomly choose which side will be face up following a uniform distribution. You can specify the side yourself, or use the sample function to do a biased random flip/roll.

Value

Which side ended up face up (1 or 2 for coin, 1 through 6 for die). This is the internal numbering and does not match a change in the sides argument.

Note

The current algorithm for animating the die roll shows all the sides, but I am not satisfied with it. Please suggest improvements.

Author(s)

Greg Snow greg.snow@intermountainmail.org

See Also

dice, plot.dice, coin.faces, sample

Examples

## Not run: 
plot.rgl.coin()
flip.rgl.coin()
flip.rgl.coin(1)
flip.rgl.coin(2)

rgl.clear()

# two-headed coin
plot.rgl.coin(tails=coin.faces$qh)

rgl.clear()

# letters instead of pictures
plot.rgl.coin(heads=coin.faces$H, tails=coin.faces$T)

# biased flip
flip.rgl.coin( sample(2,1, prob=c(0.65, 0.35) ) )

rgl.clear()

plot.rgl.die()
roll.rgl.die()
roll.rgl.die(6)

# biased roll
roll.rgl.die( sample(6,1, prob=c(1,2,3,3,2,1) ) )
## End(Not run)

[Package TeachingDemos version 1.4 Index]