source("http://macosa.dima.unige.it/r.R")    # If I have not already loaded the library
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
S 18 Animations     
While clock() and clock2(12,25,40) display the
current time or hour that is written, Hour(),
Minute() and Second() contain numerical information
about the current time.
Sec() provides the number of seconds since the
start of the program; wait(x) does spend x seconds
(useful commands for animations).
    
These commands can be used to create animations.
If I want, I can store them in file that I can
load with source command.
For an example run the line:
source("http://macosa.dima.unige.it/R/pythag.txt")
or:
source("http://macosa.dima.unige.it/R/pitag.txt")
( use:  http://macosa.dima.unige.it/R/pitag.txt
  in the browser if you want see the file )
I can also store individual images and then merge them into an animated gif (see) # Program that produces the pictures here above, on the left: HF=3; BF=3 boxW(0,100,0,100) for(k in 0:10) {BoxW(0,100, 0,100) circle(50,82, 7, "brown"); text(50,95,"aspetta") polyline( c(35,30,50,70,75,70,50,50,35,50,65), c(1.5,2,40,2,2.5, 2,40,75,45,75,45), "blue" ); wait(0.5)} polyline(c(53.5,55.85),c(79,79), "brown"); text(71,80,"CIAO"); CLEAN(0,100, 90,100) # CLEAN overlaps a white rectangle BOX() # To overlay a grid, use after one of these 3 commands: BOXw() BOXg() # For the horse see here. # Run the following three lines ... f = function(x) ifelse(x != 0, cos(1/x)*x, 0); BF=4; HF=3; a = 4 for(i in 1:8) {a=a/2; graph2F(f,-a,a,"blue"); if(a<2) abovex("I made a zoom"); wait(3)} underx("STOP") # see
BF=4; HF=4
PLANE(-3,3, -3,3)
q=1; POINT(q,0, "red"); line2p(-1,-2, -1,2, "red")
parabola <- function(x,y) y^2-4*q*x
CURVE(parabola, "black")
fig <- recordPlot()
# the superior part of the parabola
f = function(x) sqrt(4*q*x)
for(x in (22:0)/10) {wait(0.3);replayPlot(fig)
POINT(q,0, "red"); line2p(-1,-2, -1,2, "red")
CURVE(parabola, "black")
y = f(x); POINT(x,y, "brown")
polyL( c(-1,x,1), c(y,y,0), "brown")}
# a moment of the animation ->
   
# See here for the animated costruction of the graph of y=sin(x) # The construction of an asteroid (the figure drawn by a point of a circle that # rotates on a larger circle) PLANE(-4,4, -4,4) f <- function(t) 4*cos(t)^3; g <- function(t) 4*sin(t)^3 circle(0,0, 4, "grey"); param(f,g, 0,2*pi, "grey"); fig0 <- fig <- recordPlot() i <- 0; x <- 4; y <- 0; dt <- 0.3 while(i < 90) {replayPlot(fig); POINT(x,y,"brown"); fig <- recordPlot() i <- i+1 t <- 2*pi/90*i; circle(3*cos(t),3*sin(t), 1, "red"); arrow(3*cos(t),3*sin(t), 4*cos(t)^3, 4*sin(t)^3, "blue"); x <- c(x,4*cos(t)^3); y <- c(y,4*sin(t)^3); wait(dt) } wait(dt); replayPlot(fig0); param(f,g, 0,2*pi, "brown") # For a version without grid click.     # Another example here. # On the construction of curves with parameters we have already focused in S01. # Here we see how to make them animated. # I have to use u, v, w as parameters and du, dv, dw as steps. # By clicking I can change the parameters. # The previous curve is drawn in gray. # Click the blue button to see the charts thicker. # Polar graphs use a monometric scale. # A parabola in (-5,5)×(-10,10) by varying the coefficients h <- function(x) u*x^2+v*x+w u <- 1; du <- 0.5; v <- 1; dv <- 1; w <- 0; dw <- 0.5 BF=5; HF=4 # If you want you can increase the size fun3P(h,-5,5, -10,10) body(h) # u * x^2 + v * x + w text(2.5,7.5,"y = u*x^2+v*x+w") # This tool can be useful for exploration # If I want, I can use less than 3 parameters (if I do not use w I can set w=0) # If I want, I can use Family=1 to construct a family of curves Family = 1 u <- 1; du <- 0.5; v <- 1; dv <- 1; w <- 0; dw <- 0.5 fun3P(h,-5,5, -10,10) # On the left, if I click "+v". On the right, if I click "-u". # To return to the initial mode I can use Family=0 # For a function of 2 variables I can also use k and dk. # A polynomial function of degree 2: u <- 2; du <- 1; v <- 2; dv <- 1; w <- 2; dw <- 1; k <- 2; dk <- 1 G <- function(x,y) x^2*u+y^2*v+w*x - k BF=5; HF=4 curve4P(G, -3,3, -3,3) text(1.5,2.5,"x^2*u+y^2*v+w*x = k") # In the following example I use only 2 parameters # The graph at the variation of t between 0 and 2π in (-3,9)×(-6,6) ro <- function(t) u+v*cos(t) u <- 2; du <- 0.1; v <- 2; dv <- 0.1; w <- 0; dw <- 0 BF=5; HF=4 polar3P(ro, 0,2*pi, -3,9, -6,6) text(6,4.5,"ro = u+v*cos(t)") # I can use "Family" with curve4P and polar3P Family=1 u <- 2; du <- 0.2; v <- 1/2; dv <- 1/2 polar3P(ro, 0,2*pi, -3,8, -5,5) c(u,v) # 2.0 4.5 # v goes from 1/2 to 4.5 step 1/2 # Slope of a graph. Define a function f (or g, h1, …). With the following command you get the graph of f with input between a and b, output between c and d, with highlighted (x0,f(x0)). If you click on a point, you have the graph of the line passing through the points of the chart that have ascisse x0 and that of the point clicked. If you click the ascisse x0 you have the tangent line. Press up to the left to finish. Command: slope(f, a,b, c,d, x0) Try with: h <- function(x) x^2; slope(h, -1,1.5, 0,3, 1) This is a start to the concept of derivative f <- function(x) cos(x) slope(f, -3.2,0, -1.6,1.6, -pi/2) #A dot is marked in the graph. By clicking near the graph, identify an #abscissa. The line for P and the point of the graph with that abscissa #are plotted. If you want, by clicking, you can repeat the test. When #you want to stop click on top left. Now press ENTER. 1: # See here for another specimen # See here for another specimen # See here for another specimen Other examples of use