
arco <- function(C=c(0,0), r=1, th1=0, th2=1, deg=FALSE, ...) {
  th=seq(th1, th2, by=0.02)
  if (deg) th = th * pi/180 
  points(C[1] + r*cos(th), C[2] + r*sin(th), ty='l', ...)
}

ruota.prob <- function(prob=NULL, perc=TRUE, parti=10, show.p=TRUE) {
  r <- 0.9
  plot(NULL, xlim=c(-1,1),ylim=c(-1,1), axes=FALSE, xlab='',ylab='', asp=1)
  symbols(0, 0, circles=r, bg=rgb(0.9,0.9,0.9), add=TRUE, inches=FALSE)
  th <- seq(0, 2*pi, len=(parti+1))
  for(a in th) lines(c(0,r*cos(a)), c(0,r*sin(a)), lty=2, lwd=0.8)
  #lines(c(-r,r),c(0,0), lty=2, lwd=0.8)
  #lines(c(0,0), c(-r,r), lty=2, lwd=0.8)
  if(is.null(prob)) {
    cat(sprintf("\n-> Cliccare su due punti sul cerchio"))
    cat(sprintf("\n   in SENSO ANTIORARIO per definire un arco\n"))   
    p = locator(2)
  } else {
    if (perc) prob = prob/100
    th1 <- 0
    th2 <- prob*2*pi
    p <- list( x=c(r,r*cos(th2)), y=c(0, r*sin(th2)) )
  }
  lines(c(0, p$x[1]), c(0, p$y[1]), lwd=1.8, col='red')
  lines(c(0, p$x[2]), c(0, p$y[2]), lwd=1.8, col='red')
  th1 <- atan2(p$y[1], p$x[1])
  th2 <- atan2(p$y[2], p$x[2])
  if( (th2 < 0) & ( (th1 >= 0))) th2 <- 2*pi + th2
  thm <- (th1+th2)/2
  prob <- (th2-th1)/(2*pi)*100
  if(show.p) text(1.2*r*cos(thm), 1.2*r*sin(thm),
                    sprintf("%.0f%%", prob), cex=1.5, col='red' )
  arco(th1=th1, th2=th2, r=r, lwd=2.0, col='red' )
}

gira <- function(r=0.8) {
  thr <- runif(1)*2*pi
  arrows(0, 0, r*cos(thr), r*sin(thr), col='blue', lwd=1.8, len=0.15)
}

