#--------------------------------------------------------
# Polarizzazione lineare

#
#   GdA gen 2022
# --------------------------------------------------------


E0  <- 1

theta <- 30 #45
th    <- theta/180*pi
E0y   <- E0*cos(th)
E0z   <- E0*sin(th)

om    <- 1 # pulsazione  unitaria
T     <- 2*pi/om

old.mar = par("mar")
par(mar=c(4.0,4.0,3.0,0.1))   # margini ottimizzati

dt <- T/32 # intervalli di tempo in unità del periodo

t = - dt
while(1) {
    t <- t + dt
    Ey <- E0y*cos(om*t)
    Ez <- E0z*cos(om*t)
    By <- -Ez  
    Bz <- Ey   
    plot(NULL, xlim=1.0*c(-E0,E0), ylim=1.0*c(-E0,E0), xlab='', ylab='',        
         xaxt='n', yaxt='n', asp=1, bty='n')    
    abline(h='0', lty=2, col='gray')
    abline(v='0', lty=2, col='gray')
    text(E0*1.0,0,'y',cex=2)
    text(0,E0*1.0,'z',cex=2)
    if(abs(Ey)>0.001) arrows(0,0, Ey, Ez, col='red', lwd=2, length=sqrt(Ey^2+Ez^2)*0.1)
    if(abs(Ey)>0.001) arrows(0,0, Ey, 0, col='red', lty=2, lwd=2, length=abs(E0y)*0.1)
    if(abs(Ey)>0.001) arrows(0,0, 0, Ez, col='red', lty=2, lwd=2, length=abs(E0z)*0.1)
    if(abs(Ey)>0.001) arrows(0,0, By, Bz, col='green', lwd=2, length=sqrt(Ey^2+Ez^2)*0.1)
    points(0,0, col='blue', pch=19, cex=abs(Ey^2+Ey^2))
    Sys.sleep(0.1)
    #break()
    if( t > 4*T) break()
}
par(mar=old.mar)
