#---------------------------------------------------------
# 
# Moto circolare inizialmente uniforme (per un giro),
# poi accelerato, 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)
}

PAUSA = TRUE

# 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  # metri
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      # secondi
ndt <- 16
dt  <- T0/ndt
om0 <- 2*pi/T0
a.om <- 0.1      #accelerazione angolare: -> rad/s^2
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.max.u <- i*dt            # ci ricordiamo il tempo impiegato a compiere  
                                   # il primo giro a velocità costante        
        th.max.u <- om0 * t.max.u  # angolo percorso fino a t.max.u 
        text(-1.1*xyM, xyM, "moto circolare 'accelerato!", col='red', pos=4, cex=2)
    }
    if (i < ndt) {  # moto a velocità angolare costante
       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)
    } else {        # moto a velocità angolare crescente linearmente con il tempo       
        t <- i*dt - t.max.u  # facciamo ripartire il tempo da  t.max.unif
        th <- th.max.u + om0 * t + 1/2 * a.om * t^2
        r <- R * c( cos(th), sin(th) )
        points(r[1],r[2], pch=19, cex=1)
        v[1] <- - (om0 + a.om*t) * R * sin(th) 
        v[2] <- (om0 + a.om*t) * R * cos(th)
        arrows(r[1], r[2], r[1]+sv*v[1], r[2]+sv*v[2], len=0.1, col='blue' , lwd=lwd)
        a[1] <- - a.om * R * sin(th) - (om0 + a.om*t)^2 * R * cos(th)
        a[2] <-   a.om * R * cos(th) - (om0 + a.om*t)^2 * R * sin(th)
        arrows(r[1], r[2], r[1]+sa*a[1], r[2]+sv*a[2], len=0.1, col='red', lwd=lwd)
        if( th > 2* 2*pi) break
    }
    if (PAUSA) {
       pausa()
    } else {
       Sys.sleep(dt)  # pausa di 'dt'
    }
}


