```source("http://macosa.dima.unige.it/r.R")    # If I have not already loaded the library
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
S 11 Perspective. 3D

Examples of perspective views (for more information, see the R guide and the worksheets
for the fifth grade high school here). Ses also S19.

Commands:   house() - or casa() -  GRAF3D()  BOH()  seba(0)  seba(1)

house()
( then press  ENTER  to start the program )
# teta, fi e ind.dist  indicate the position of the eye: the direction of the look
# projected on the horizontal plane, its inclination relative to that plane and the
# distance of the eye from the center of the box.

# These pictures clarify how teta is calculated:

GRAF3Dim()

If you type GRAF3D(10,5,1) you can see the graph of the function that in the circle of
centre (0,0) and radius 1 has output sqrt(x^2+y^2) and outside has output 0, viewed
from the direction teta=10°, fi=5° and from a particular distance. With  GRAF3D(10,5,3)
turn the eye away.
Try with    GRAF3D(0,30,1/2)  GRAF3D(10,30,2)  GRAF3D(10,-30,2)

BOH()

If you type INCL(5,10) you see a figure from a 5° inclination, with a scale such that
the x,y,z axes (black, green and red) are 10 long. We are looking at it a little to the
left of the yz plan.   The figure is a parable.   It's possible?     Try with
INCL(10,10), INCL(15,10), INCL(40,10), INCL(40,200), INCL(40,2000)

INCL(5,10)

INCL(40,10)
# An image similar to "San Sebastiano" by
# Antonello da Messina
# (1.80 cm high, seen by the painter from
# the height of 0.30 m)

# Try to change the point of view (direction
# and distance).

seba(0)  seba(1)
( Press  ENTER  to start the program )

# To get an idea of how 3D figures are made (persp command):

x = c(-1,5); y = c(-1,5) ; w = c(-1,5)        # the box
z = array(c(w[1],w[1],w[1],w[1]), dim=c(2,2)) # z-coord. of the base of the box
# I trace the box, and I choose a point of view with th and ph
# (direction of look projected on horizontal plane, its inclination relative to that plane)
# scale=FALSE  chooses a monometric scale;  ticktype="detailed"  shows the ticks
# with  nticks=3  I reduce the number of ticks; d=3 chooses the distance of the eye
th = 120; ph = 15
F = persp(x,y,z, theta=th,phi=ph, scale=FALSE, xlim=x,ylim=y,zlim=w,
ticktype="detailed",nticks=3,d=3,cex.axis=0.8,cex.lab=0.8)
# the axes (I use  trans3d  to add pieces, I use  pmat  to choose to add them to F)
axes = function(F) {
lines(trans3d(c(0,0),c(0,0),c(0,w[2]),pmat=F),col="red")
lines(trans3d(c(0,x[2]),c(0,0),c(0,0),pmat=F),col="red")
lines(trans3d(c(0,0),c(0,y[2]),c(0,0),pmat=F),col="red")
lines(trans3d(c(0,0),c(0,0),c(w[1],0),pmat=F),col="red",lty=3)
lines(trans3d(c(x[1],0),c(0,0),c(0,0),pmat=F),col="red",lty=3)
lines(trans3d(c(0,0),c(y[1],0),c(0,0),pmat=F),col="red",lty=3) }
axes(F)
# the points, and the triangle that has them as extremes
points(trans3d(0,0,3,pmat=F),col="blue",pch=19)
points(trans3d(2,0,0,pmat=F),col="blue",pch=19)
points(trans3d(0,4,0,pmat=F),col="blue",pch=19)
lines(trans3d(c(0,2,0,0),c(0,0,4,0),c(3,0,0,3),pmat=F),col="blue")
#
# Here's how to get the following representations:

F = persp(x,y,z, theta=th,phi=ph, scale=FALSE, xlim=x,ylim=y,zlim=w,
d=2,cex.axis=0.8,cex.lab=0.8)
# or:
F = persp(x,y,z, theta=th,phi=ph, scale=FALSE, xlim=x,ylim=y,zlim=w,
d=2,border="white",box=FALSE)
#
# Another example:
marg = function(x) par( mai=c(x,x,x,x) )              # I can change the margin size
BF=8; HF=3; NW()      # or  BOXW(0,0,0,0)             # I open a white box
x = c(0,25); y = c(-2,2); w = c(0,4); z = array(c(0,0,0,0),dim=c(2,2))
marg(0); th = -30; ph = 20                            # I choose a null margin
F = persp(x,y,z, theta=th,phi=ph, scale=FALSE, xlim=x,ylim=y,zlim=w,
d=2, border="white", box=FALSE)
lines(trans3d(c(0,25),0.5,0,pmat=F),col="black")      # one track
lines(trans3d(c(0,25),-0.5,0,pmat=F),col="black")     # the other one
for(xx in seq(0,25,0.25)) lines(trans3d(xx,c(-0.5,0.5),0,pmat=F),col="black")
lines(trans3d(c(5,17),1,0,pmat=F),col="black")        # the back side of the gallery
t = seq(0,pi, len=1000)
lines(trans3d(17,cos(t),sin(t),pmat=F),col="black")   # the end of the gallery
# the gallery
for(xx in seq(5.01,16.99,0.01)) lines(trans3d(xx,cos(t),sin(t),pmat=F),col="brown")
lines(trans3d(c(5,17),-1,0,pmat=F),col="black")       # the front side of the gallery
lines(trans3d(5,cos(t),sin(t),pmat=F),col="black")    # the beginning of the gallery
t = seq(pi/3,pi, len=1000)
lines(trans3d(17,cos(t),sin(t),pmat=F),col="black")   # part of the back side

# See here for more elaborate drawings, and to obtain the 2-dimensional coordinates of
# the 3D points (commands PXY, xyz_tfd,  tfd_xyz):

# See here for antother example:

# See here for the barrel above.
# See here for the mouse-oriented animation, shown below:

# The sum of 3D vectors
x = c(-5,5); y = c(-5,5) ; w = c(-1,7)        # the box
z = array(c(w[1],w[1],w[1],w[1]), dim=c(2,2)) # z-coord. of the base of the box

axes = function(F) {                          # the axes
lines(trans3d(c(0,0),c(0,0),c(0,w[2]),pmat=F),col="red")
lines(trans3d(c(0,x[2]),c(0,0),c(0,0),pmat=F),col="red")
lines(trans3d(c(0,0),c(0,y[2]),c(0,0),pmat=F),col="red")
lines(trans3d(c(0,0),c(0,0),c(w[1],0),pmat=F),col="red",lty=3)
lines(trans3d(c(x[1],0),c(0,0),c(0,0),pmat=F),col="red",lty=3)
lines(trans3d(c(0,0),c(y[1],0),c(0,0),pmat=F),col="red",lty=3) }

fig = function(F) {                          # the vectors
u1=1;u2=-2;u3=2; v1=-4; v2=1; v3=3;  D=19
lines(trans3d(c(0,u1),c(0,u2),c(0,u3),pmat=F),col="blue")
lines(trans3d(c(0,v1),c(0,v2),c(0,v3),pmat=F),col="blue")
points(trans3d(u1,u2,u3,pmat=F),col="blue",pch=D)
points(trans3d(v1,v2,v3,pmat=F),col="blue",pch=D)
lines(trans3d(c(u1,u1+v1),c(u2,u2+v2),c(u3,u3+v3),pmat=F),col="seagreen")
lines(trans3d(c(v1,u1+v1),c(v2,u2+v2),c(v3,u3+v3),pmat=F),col="seagreen")
lines(trans3d(c(0,u1+v1),c(0,u2+v2),c(0,u3+v3),pmat=F),col="brown")
points(trans3d(u1+v1,u2+v2,u3+v3,pmat=F),col="seagreen",pch=D) }
# (A)
th=140; ph=15; dev.new()
F=persp(x,y,z, theta=th,phi=ph, scale=FALSE, xlim=x,ylim=y,zlim=w, ticktype="detailed",nticks=3,d=5,cex.axis=0.8,cex.lab=0.8)
axes(F); fig(F)
# (B)
th=95; ph=15; dev.new()
F=persp(x,y,z, theta=th,phi=ph, scale=FALSE, xlim=x,ylim=y,zlim=w, ticktype="detailed",nticks=3,d=5,cex.axis=0.8,cex.lab=0.8)
axes(F); fig(F)
# (C)
th=185; ph=15; dev.new()
F=persp(x,y,z, theta=th,phi=ph, scale=FALSE, xlim=x,ylim=y,zlim=w, ticktype="detailed",nticks=3,d=5,cex.axis=0.8,cex.lab=0.8)
axes(F); fig(F)

# If we want change something:

axes = function(F) {
lines(trans3d(c(0,0),c(0,0),c(0,w[2]),pmat=F),col="red")
lines(trans3d(c(0,x[2]),c(0,0),c(0,0),pmat=F),col="red")
lines(trans3d(c(0,0),c(0,y[2]),c(0,0),pmat=F),col="red")
lines(trans3d(c(0,0),c(0,0),c(w[1],0),pmat=F),col="red",lty=3)
lines(trans3d(c(x[1],0),c(0,0),c(0,0),pmat=F),col="red",lty=3)
lines(trans3d(c(0,0),c(y[1],0),c(0,0),pmat=F),col="red",lty=3)
points(trans3d(0,0,0:w[2],pmat=F),col="red",pch=20)
points(trans3d(0:x[2],0,0,pmat=F),col="red",pch=20)
points(trans3d(0,0:y[2],0,pmat=F),col="red",pch=20)
points(trans3d(0,0,w[1]:0,pmat=F),col="red",pch=20)
points(trans3d(x[1]:0,0,0,pmat=F),col="red",pch=20)
points(trans3d(0,y[1]:0,0,pmat=F),col="red",pch=20)
}
th=140; ph=10; dev.new()
F=persp(x,y,z, theta=th,phi=ph, scale=FALSE, xlim=x,ylim=y,zlim=w, box=FALSE,border="white", d=5)
axes(F); fig(F)
points(trans3d(-3,-1,0,pmat=F),col="seagreen",pch=20)
lines(trans3d(c(-3,0),-1,0,pmat=F),col="seagreen",lty=3)
lines(trans3d(-3,c(-1,0),0,pmat=F),col="seagreen",lty=3)
lines(trans3d(-3,-1,c(0,5),pmat=F),col="seagreen",lty=3)
lines(trans3d(c(0,-3),c(0,-1),5,pmat=F),col="seagreen",lty=3)
#
points(trans3d(1,-2,0,pmat=F),col="blue",pch=20)
lines(trans3d(c(1,0),-2,0,pmat=F),col="blue",lty=3)
lines(trans3d(1,c(-2,0),0,pmat=F),col="blue",lty=3)
lines(trans3d(1,-2,c(0,2),pmat=F),col="blue",lty=3)
lines(trans3d(c(0,1),c(0,-2),2,pmat=F),col="blue",lty=3)
#
points(trans3d(-4,1,0,pmat=F),col="blue",pch=20)
lines(trans3d(c(-4,0),1,0,pmat=F),col="blue",lty=3)
lines(trans3d(-4,c(0,1),0,pmat=F),col="blue",lty=3)
lines(trans3d(-4,1,c(0,3),pmat=F),col="blue",lty=3)
lines(trans3d(c(0,-4),c(0,1),3,pmat=F),col="blue",lty=3)
#
text(trans3d(0.5,0,-0.4,pmat=F),"O"); text(trans3d(-3,-1,5.5,pmat=F),"P")

Other examples of use

```