#
# Copia e incolla tutto in R
# Poi ricopia col tasto "SU" l'ultima riga:   prova()
# Ripeti la cosa finché vuoi
#
dev.new(width=5,height=5)
par( mai = c(0.5,0.5,0.1,0.1) )
plot(c(-1,1.1),c(-0.5,1.6),type="n",xlab="",ylab="",asp=1)
n <- 0; ok <- 0
prova <- function() {
n <<- n+1
plot(c(-1,1.1),c(-0.5,1.6),type="n",xlab="",ylab="",asp=1)
abline(h=axTicks(2),v=axTicks(1),lty=3,col="grey")
lines(c(0,1),c(1.1,1.1)); lines(c(0,1),c(1.2,1.2)); lines(c(0,1),c(1.3,1.3))
x <<- runif(1); y <<- runif(1); z <<- runif(1)
lines(c(0,x),c(1.1,1.1),lwd=3,col="red"); lines(c(0,y),c(1.2,1.2),lwd=3,col="blue")
lines(c(0,z),c(1.3,1.3),lwd=3,col="green3")
points(x,1.1); points(y,1.2); points(z,1.3)
r1 <- y; xc1 <- 0
lines(c(0,z),c(0,0),lwd=3,col="green3")
points(0,0); points(z,0)
ysemcer1 <- function(q) sqrt(r1^2-(q-xc1)^2)
curve(ysemcer1,xc1-r1,xc1+r1,n=1e4,add=TRUE,lty=3,col="blue")
q1 <- xc1-r1; q2 <- xc1+r1
r2 <- x; xc2 <- z
q3 <- xc2-r2; q4 <- xc2+r2
ysemcer2 <- function(q) sqrt(r2^2-(q-xc2)^2)
curve(ysemcer2,xc2-r2,xc2+r2,n=1e4,add=TRUE,lty=3,col="red")
if(q1 < q3 & q2 < q4 & q3 < q2) {
ok <<- ok+1
h <- function (q) ysemcer2(q)-ysemcer1(q)
x0 <- uniroot(h, c(q3, q2), tol = 0.00001)$root
# print(x0);
text(-0.2,1.2,"OK");lines(c(0,x0),c(0,ysemcer1(x0)),lwd=3,col="blue");
lines(c(x0,z),c(ysemcer1(x0),0),lwd=3,col="red"); points(x0,ysemcer1(x0)) } else
text(-0.2,1.2,"KO")
# text(0.5,1,q1)
# text(0.5,0.9,q2)
perc <- round(ok/n*100, digits=2)
text(-0.75,1.2,perc)
text(-0.75,1.1,"%")
text(-0.75,1.35,n)
text(-0.75,1.45,"n.prove")
}
prova()