#---------------------------------------------------------
# 
# Moto circolare uniforme, con rappresentazione grafica
#
# GdA, marzo 2023
#--------------------------------------------------------

pausa <- function() { cat ("\n >> press Enter to continue\n"); scan() }

# funzione che disegna un cerchio 
cerchio <- function(C=c(0,0), r=1, col='blue') {
  th <- seq(0, 2*pi, by=0.02)
  points(C[1] + r*cos(th), C[2] + r*sin(th), ty='l', col)
}


# plot vuoto
xyM <- 10
plot(NULL,  xaxt = 'n', yaxt = 'n', bty = 'n', pch = '', asp=1,
     ylab = '', xlab = '', xlim=c(-xyM,xyM), ylim=c(-xyM,xyM))

# aggiungiamo gli assi cartesiani
abline(v=0)
abline(h=0)

# ... con tanto di frecce (l'uso di f è un piccolo trucco pratico)
f <- 1.2; arrows(f*xyM, 0, f*xyM*1.01, 0, length=0.2 )
text(f*xyM, -0.04*xyM, pos=1, 'x', cex=1.5)
f <-1.; arrows(0, f*xyM, 0, f*xyM*1.01, length=0.2 )
text(0.06*xyM, f*xyM, pos=1, 'y', cex=1.5)

# disegniamo il cerchio
R <- 6
cerchio(r=R, col='gray')

# decidiamo il periodo e tracciamo le frecce a ogmi frazione di periodo 
# (chiamato T0 per motivi che vedremo nel seguito)
T0  <- 8
ndt <- 16
dt  <- T0/ndt
om0 <- 2*pi/T0
sv <- 1; sa <- 1  # fattori di scala per plottare i vettori vel e acc
                  # (anche perché sommare  'v' o 'a' a 'r' è una mostruosità!)
lwd = 1
for (i in 0:(2*ndt)) {    # facciamo due giri     
    if (i==ndt) lwd=2*lwd # al secondo giro intensifichiamo le frecce
    t <- i*dt
    r <- R * c( cos(om0*t), sin(om0*t) )
    points(r[1],r[2], pch=19, cex=0.5)
    v <- om0*R* c(-sin(om0*t), cos(om0*t))
    arrows(r[1], r[2], r[1]+sv*v[1], r[2]+sv*v[2], len=0.1, col='blue' , lwd=lwd)
    a <-  - om0^2 * r
    arrows(r[1], r[2], r[1]+sa*a[1], r[2]+sv*a[2], len=0.1, col='red', lwd=lwd)
    Sys.sleep(dt)  # pausa di 'dt' 
}


