File: plot.rgl.die.R

package info (click to toggle)
r-cran-teachingdemos 2.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: makefile: 2
file content (67 lines) | stat: -rw-r--r-- 2,523 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
rgl.die <- function(x = 1:6, col.cube='white',col.pip='black',sides=x, ...) {

  if(!requireNamespace('rgl', quietly = TRUE)) stop("This function depends on the 'rgl' package wich is not available")

  rgl::rgl.viewpoint(45,45)

  pip.coords <- function( x,y ) {
    xc <- yc <- numeric(0)
    for(i in 0:39){
      xc <- c(xc, x, 0.05*cos(pi/20*i)+x, 0.05*cos(pi/20*(i+1))+x)
      yc <- c(yc, y, 0.05*sin(pi/20*i)+y, 0.05*sin(pi/20*(i+1))+y)
    }
    cbind(xc,yc)
  }

  pip.loc <- list(matrix( 0.5, ncol=2, nrow=1),
                  cbind( c(.25, .75), c(.25, .75)),
                  cbind( c(.25, .5, .75), c(.25, .5, .75)),
                  cbind( c(.25, .25, .75, .75), c(.25, .75, .75, .25)),
                  cbind( c(.25, .25, .75, .75, .5), c(.25, .75, .75, .25, .5)),
                  cbind( c(.25, .25, .25, .75, .75, .75),
                         c(.25, .5, .75, .75, .5, .25)))

  rgl::rgl.quads( c(0,0,1,1), c(0,1,1,0), c(0,0,0,0), col=col.cube)
  rgl::rgl.quads( c(0,0,1,1), c(0,1,1,0), c(1,1,1,1), col=col.cube)
  rgl::rgl.quads( c(0,0,0,0), c(0,1,1,0), c(0,0,1,1), col=col.cube)
  rgl::rgl.quads( c(1,1,1,1), c(0,1,1,0), c(0,0,1,1), col=col.cube)
  rgl::rgl.quads( c(0,0,1,1), c(0,0,0,0), c(0,1,1,0), col=col.cube)
  rgl::rgl.quads( c(0,0,1,1), c(1,1,1,1), c(0,1,1,0), col=col.cube)

  tmp <- pip.loc[[ sides[1] ]]
  for( i in 1:nrow(tmp) ){
    xy <- pip.coords( tmp[i,1], tmp[i,2] )
    rgl::rgl.triangles(xy[,1], rep(1.001, nrow(xy)), xy[,2], col=col.pip,lit=FALSE)
  }

  tmp <- pip.loc[[ sides[2] ]]
  for( i in 1:nrow(tmp) ){
    xy <- pip.coords( tmp[i,1], tmp[i,2] )
    rgl::rgl.triangles(xy[,1], xy[,2], rep(1.001, nrow(xy)), col=col.pip,lit=FALSE)
  }

  tmp <- pip.loc[[ sides[3] ]]
  for( i in 1:nrow(tmp) ){
    xy <- pip.coords( tmp[i,1], tmp[i,2] )
    rgl::rgl.triangles( rep(1.001, nrow(xy)), xy[,1], xy[,2], col=col.pip,lit=FALSE)
  }

  tmp <- pip.loc[[ sides[4] ]]
  for( i in 1:nrow(tmp) ){
    xy <- pip.coords( tmp[i,1], tmp[i,2] )
    rgl::rgl.triangles( rep(-0.001, nrow(xy)), xy[,1], xy[,2], col=col.pip,lit=FALSE)
  }

  tmp <- pip.loc[[ sides[5] ]]
  for( i in 1:nrow(tmp) ){
    xy <- pip.coords( tmp[i,1], tmp[i,2] )
    rgl::rgl.triangles(xy[,1], xy[,2], rep(-0.001, nrow(xy)), col=col.pip,lit=FALSE)
  }

  tmp <- pip.loc[[ sides[6] ]]
  for( i in 1:nrow(tmp) ){
    xy <- pip.coords( tmp[i,1], tmp[i,2] )
    rgl::rgl.triangles(xy[,1], rep(-0.001, nrow(xy)), xy[,2], col=col.pip,lit=FALSE)
  }

}