source("http://macosa.dima.unige.it/r.R") # If I have not already loaded the library ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- # How to get more elaborate drawings, and the 2-dimensional coordinates of the 3D points # [Only PXY, xyz_tfd and tfd_xyz are already included in the program] # You can copy this document into a text editor, change it and then copy it to R # An example: an object illuminated by a light bulb and its shadow. # SELECT everything (from here to ###) and paste in R # We use co[] for the colors of the various parts of the image co=NULL co[1]="white" co[2]="white" co[3:6]="black" co[7]="white" co[8:9]="black" co[10]="white" co[11:13]="black" co[14]="white" co[15:18]="red" co[19]="white" co[20:22]="red" co[23:24]="grey50" co[25:26]="white" co[27:28]="grey50" co[29]="red" x = c(-0.1,6.5); y = c(-0.1,6.5); w = c(-0.1,6.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, 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; di chooses the distance of the eye thphdi = function(t,p,di) { par( mai = c(0,0,0,0) ) # I reduce the margins F = persp(x,y,z,theta=t,phi=p,scale=FALSE,zlim=w,xlim=x,ylim=y,d=di,box=FALSE,border="white") FFF <<- F # I put image in FFF to then use PXY command (or other command on the image) xm=7; ym=7; zm=7 # grid (z=0 plane, x=0 plane, y=0 plane) if(co[1]!="white") for(i in seq(1,xm,1)) lines(trans3d(i,c(0,ym),0,pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(1,ym,1)) lines(trans3d(c(0,xm),i,0,pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(1,ym,1)) lines(trans3d(0,i,c(0,zm),pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(1,zm,1)) lines(trans3d(0,c(0,ym),i,pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(1,xm,1)) lines(trans3d(i,0,c(0,zm),pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(1,zm,1)) lines(trans3d(c(0,xm),0,i,pmat=F),col=co[1],lty=3) # axes if(co[2]!="white") lines(trans3d(c(0,0),c(0,0),c(0,zm),pmat=F),col=co[2],lty=3) if(co[2]!="white") lines(trans3d(c(0,xm),c(0,0),c(0,0),pmat=F),col=co[2],lty=3) if(co[2]!="white") lines(trans3d(c(0,0),c(0,ym),c(0,0),pmat=F),col=co[2],lty=3) # figure (top) if(co[3]!="white") lines(trans3d(c(0,0),c(0,1),c(3,3),pmat=F),col=co[3]) if(co[4]!="white") lines(trans3d(c(0,2),c(1,1),c(3,3),pmat=F),col=co[4]) if(co[5]!="white") lines(trans3d(c(2,2),c(1,0),c(3,3),pmat=F),col=co[5]) if(co[6]!="white") lines(trans3d(c(2,0),c(0,0),c(3,3),pmat=F),col=co[6]) # figure (bottom) if(co[7]!="white") lines(trans3d(c(0,0),c(0,1),c(0,0),pmat=F),col=co[7]) if(co[8]!="white") lines(trans3d(c(0,2),c(1,1),c(0,0),pmat=F),col=co[8]) if(co[9]!="white") lines(trans3d(c(2,2),c(1,0),c(0,0),pmat=F),col=co[9]) if(co[10]!="white") lines(trans3d(c(2,0),c(0,0),c(0,0),pmat=F),col=co[10]) # figure (side) if(co[11]!="white") lines(trans3d(c(0,0),c(1,1),c(0,3),pmat=F),col=co[11]) if(co[12]!="white") lines(trans3d(c(2,2),c(1,1),c(0,3),pmat=F),col=co[12]) if(co[13]!="white") lines(trans3d(c(2,2),c(0,0),c(0,3),pmat=F),col=co[13]) if(co[14]!="white") lines(trans3d(c(0,0),c(0,0),c(0,3),pmat=F),col=co[14]) # rays if(co[15]!="white") lines(trans3d(c(-1,0),c(-1,0),c(5,3),pmat=F),col=co[15]) if(co[16]!="white") lines(trans3d(c(-1,2),c(-1,0),c(5,3),pmat=F),col=co[16]) if(co[17]!="white") lines(trans3d(c(-1,2),c(-1,1),c(5,3),pmat=F),col=co[17]) if(co[18]!="white") lines(trans3d(c(-1,0),c(-1,1),c(5,3),pmat=F),col=co[18]) if(co[19]!="white") lines(trans3d(c(0,1.5),c(0,1.5),c(3,0),pmat=F),col=co[19]) if(co[20]!="white") lines(trans3d(c(2,6.5),c(0,1.5), c(3,0),pmat=F),col=co[20]) if(co[21]!="white") lines(trans3d(c(2,6.5),c(1,4), c(3,0),pmat=F),col=co[21]) if(co[22]!="white") lines(trans3d(c(0,1.5),c(1,4), c(3,0),pmat=F),col=co[22]) # shadow if(co[23]!="white") lines(trans3d(c(1.5,6.5),c(4,4),c(0,0),pmat=F),col=co[23]) if(co[24]!="white") lines(trans3d(c(6.5,6.5),c(4,1.5),c(0,0),pmat=F),col=co[24]) if(co[25]!="white") lines(trans3d(c(6.5,1.5),c(1.5,1.5),c(0,0),pmat=F),col=co[25]) if(co[26]!="white") lines(trans3d(c(1.5,1.5),c(1.5,4),c(0,0),pmat=F),col=co[26]) if(co[27]!="white") lines(trans3d(c(6.5,2),c(1.5,0),c(0,0),pmat=F),col=co[27]) if(co[28]!="white") lines(trans3d(c(1.5,0),c(4,1),c(0,0),pmat=F),col=co[28]) # lamp if(co[29]!="white") points(trans3d(-1,-1,5,pmat=F),pch=19,col=co[29]) } ### BF=3; HF=3; NW(); thphdi(125,20,1) # the first immage # [ NW prepares a new window of the same dimensions as the last one: so I can size # the graph obtained with thphdi ] # co[1]="grey"; co[2]="blue" BF=3; HF=3; NW(); thphdi(125,20,1) # the second immage # co[7:14]="black"; co[15:22]="red"; co[25:26]="grey50" BF=3; HF=3; NW(); thphdi(125,20,1) # the third immage # co[1:2]="white"; co[7]="white"; co[10]="white"; co[29]="white"; co[15:22]="white"; co[14]="white"; co[19]="white"; co[25:26]="white" BF=3; HF=3; NW(); thphdi(125,20,1) # the fourth immage without colors # # If I want I color the figure after copying it into a drawing program, if not I can # use the PXY command to find the 2-dimensional coordinates of the 3D points. I can # also use it to identify where to write texts. BF=4; HF=4; NW(); thphdi(130,40,2) # I have changed the point of view # I want print a text; I look for the position of the upper part of the box PXY(0,0,3,FFF); PXY(0,1,3,FFF) # where are the points (0,0,3) and (0,1,3) # -0.03506306 0.24399184 # 0.0337755 0.2118994 text(-0.035,0.3, "A box and its shadow") # The (x,y) coordinates of the shadow: W = PXY(c(6.5,6.5,1.5,0,0,2),c(1.5,4,4,1,0,0),0,FFF) W # -0.278743345 -0.118545513 0.114112883 0.001952617 -0.044654704 -0.120370662 # -0.305274773 -0.391661891 -0.188112014 -0.098416038 -0.084254381 -0.127224445 # We use X and Y for the vertices instead of x and y that we are using in another mode X=W[1:6]; Y=W[7:12]; polygon(X,Y,col="grey") # In a similar way: W = PXY(c(0,2,2,0),c(0,0,1,1),c(3,3,3,3),FFF); X=W[1:4]; Y=W[5:8]; polygon(X,Y,col="yellow") W = PXY(c(2,2,2,2),c(0,1,1,0),c(0,0,3,3),FFF); X=W[1:4]; Y=W[5:8]; polygon(X,Y,col="yellow") W = PXY(c(2,0,0,2),c(1,1,1,1),c(0,0,3,3),FFF); X=W[1:4]; Y=W[5:8]; polygon(X,Y,col="yellow") # The image on the right: x = c(-1,7); y = c(-1,7); w = c(-0.1,6.5) BF=3; HF=3; NW(); thphdi(90,90,1e3) # If I want the polar coordinates of the lamp I use xyz_tfd (or xyz_tpd): xyz_tfd(-1,-1,5) # 225.000000 74.206831 5.196152 more(xyz_tfd(-1,-1,5)) # 225.00000000000000 74.20683095173604 5.19615242270663 # vice versa I use tfd_xyz (or tpd_xyz): tfd_xyz(225,74.20683095173604,5.19615242270663) # -1 -1 5 ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- # Another example: # If you write From(P) (see below) you have a view from a point in the half-line OP. # The figure is stored in xx, yy, zz # co=NULL co[1]="white" co[2]="brown" co[3]="black" x = c(-2,2); y = c(-2,2); w = c(-1,2) # 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 xx = c(-1,-1,1, 1, 1, 1, 1, 1, 1,-1,-1, -1, 1, -1, -1,-1,-1, 1,1,1, 1, 1, 1, 1,-1) yy = c(-1, 1,1, 0.25,0.25,-0.25,-0.25,0.25, -1,-1,-1, 0, 0, 0, 1, 1, 1, 1,1,1, 0, -1,-1,-1,-1) zz = c( 0, 0,0, 0, 0.6, 0.6, 0, 0, 0, 0, 1, 1.5,1.5,1.5, 1, 0, 1, 1,0,1,1.5, 1, 0, 1, 1) x1 = xx; y1 = yy; z1 = zz thphdi = function(t,p,di) { par( mai = c(0,0,0,0) ) # riduco i margini F = persp(x,y,z,theta=t,phi=p,scale=FALSE,zlim=w, xlim=x,ylim=y,d=di,border="white",box=FALSE) # grid (z=0 plane, x=0 plane, y=0 plane) if(co[1]!="white") for(i in seq(-2,2,1)) lines(trans3d(i,c(-2,2),0,pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(-2,2,1)) lines(trans3d(c(-2,2),i,0,pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(-2,2,1)) lines(trans3d(0,i,c(-1,2),pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(-1,2,1)) lines(trans3d(0,c(-2,2),i,pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(-2,2,1)) lines(trans3d(i,0,c(-1,2),pmat=F),col=co[1],lty=3) if(co[1]!="white") for(i in seq(-1,2,1)) lines(trans3d(c(-2,2),0,i,pmat=F),col=co[1],lty=3) # axes if(co[2]!="white") lines(trans3d(c(0,0),c(0,0),c(0,2),pmat=F),col=co[2]) if(co[2]!="white") lines(trans3d(c(0,2),c(0,0),c(0,0),pmat=F),col=co[2]) if(co[2]!="white") lines(trans3d(c(0,0),c(0,2),c(0,0),pmat=F),col=co[2]) # house if(co[3]!="white") lines(trans3d(x1,y1,z1,pmat=F),col=co[3]) } From = function(x,y,z) {teta = ifelse(x!=0,atan(y/x)/pi*180+90,0); phi = atan(z/sqrt(x^2+y^2))/pi*180; di = sqrt(x^2+y^2+z^2); if(teta > 180) teta = teta-180; thphdi(teta,phi,di) } BF=2.5; HF=2.5; NW(); From(3,1,1) # (1) BF=2.5; HF=2.5; NW(); From(1,3,1) # (2) d=500; BF=2.5; HF=2.5; NW(); From(0,0,1e3) # (3) # x1 = 0; y1 = yy; z1 = zz d=1; BF=2.5; HF=2.5; NW(); From(3,1,1) # (4) x1 = xx; y1 = 0; z1 = zz BF=2.5; HF=2.5; NW(); From(3,1,1) # (5) x1 = xx; y1 = yy; z1 = 0 BF=2.5; HF=2.5; NW(); From(3,1,1) # (6) x1 = 0.5*xx+0.5*yy; y1 = x1; z1 = zz BF=2.5; HF=2.5; NW(); From(3,1,1) # (7) ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- # Another example: # x = c(-0.1,7); y = c(-0.1,7); w = c(-0.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 thphdi = function(t,p,di) { # definition of the "space" par( mai = c(0,0,0,0) ) # I reduce the margins F = persp(x,y,z,theta=t,phi=p,scale=FALSE,zlim=w,xlim=x,ylim=y,d=di,box=FALSE,border="white") FFF <<- F # I put image in FFF to then use PXY command (or other command on the image) xm=7; ym=7; zm=7; co1="seagreen"; co2="black" # grid (z=0 plane, x=0 plane, y=0 plane) if(co1!="white") for(i in seq(1,xm,1)) lines(trans3d(i,c(0,ym),0,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,ym,1)) lines(trans3d(c(0,xm),i,0,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,ym,1)) lines(trans3d(0,i,c(0,zm),pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,zm,1)) lines(trans3d(0,c(0,ym),i,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,xm,1)) lines(trans3d(i,0,c(0,zm),pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,zm,1)) lines(trans3d(c(0,xm),0,i,pmat=F),col=co1,lty=3) # axes if(co2!="white") lines(trans3d(c(0,0),c(0,0),c(0,zm),pmat=F),col=co2,lty=3,lwd=3) if(co2!="white") lines(trans3d(c(0,xm),c(0,0),c(0,0),pmat=F),col=co2,lty=3,lwd=3) if(co2!="white") lines(trans3d(c(0,0),c(0,ym),c(0,0),pmat=F),col=co2,lty=3,lwd=3) } ##### BF=3; HF=3; NW(); thphdi(125,20,1) # the first immage X=c(3,5,1,3); Y=c(1,5,3,1); Z=c(0,0,0,0); lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=3) X=c(3,5,1,3); Z=c(1,5,3,1); Y=c(0,0,0,0); lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=3) Z=c(3,5,1,3); Y=c(1,5,3,1); X=c(0,0,0,0); lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=3) # BF=3; HF=3; NW(); thphdi(80,45,1) # the second immage X=c(3,5,1,3); Y=c(1,5,3,1); Z=c(0,0,0,0); lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=3) X=c(3,5,1,3); Z=c(1,5,3,1); Y=c(0,0,0,0); lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=3) Z=c(3,5,1,3); Y=c(1,5,3,1); X=c(0,0,0,0); lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=3) ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- # Another example: # x = c(-0.1,30); y = c(-0.1,2); w = c(-0.1,15) # 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 thphdi = function(t,p,di) { # definition of the "space" par( mai = c(0,0,0,0) ) # I reduce the margins F = persp(x,y,z,theta=t,phi=p,scale=FALSE,zlim=w,xlim=x,ylim=y,d=di,box=FALSE,border="white") FFF <<- F # I put image in FFF to then use PXY command (or other command on the image) xm=30; ym=2; zm=15; co1="seagreen"; co2="black" # grid (z=0 plane, x=0 plane, y=0 plane) if(co1!="white") for(i in seq(1,xm,1)) lines(trans3d(i,c(0,ym),0,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,ym,1)) lines(trans3d(c(0,xm),i,0,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,ym,1)) lines(trans3d(0,i,c(0,zm),pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,zm,1)) lines(trans3d(0,c(0,ym),i,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,xm,1)) lines(trans3d(i,0,c(0,zm),pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,zm,1)) lines(trans3d(c(0,xm),0,i,pmat=F),col=co1,lty=3) # axes if(co2!="white") lines(trans3d(c(0,0),c(0,0),c(0,zm),pmat=F),col=co2,lty=3,lwd=3) if(co2!="white") lines(trans3d(c(0,xm),c(0,0),c(0,0),pmat=F),col=co2,lty=3,lwd=3) if(co2!="white") lines(trans3d(c(0,0),c(0,ym),c(0,0),pmat=F),col=co2,lty=3,lwd=3) } ### BF=3; HF=2.5; NW(); thphdi(130,30,0.1) S=2 X=c(29,29,28,27,26,25,25,26,26,27,28,28,29) Y=rep(0,12) Z=c(0,12,12,9,12,12,0,0,9,6,9,0,0) lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=S) X=c(24,22.5,21.5,20,21,21.75,22.25,23,24) Y=rep(0,9) Z=c(0,12,12,0,0,6,6,0,0) lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=S) lines(trans3d(c(12,12)+10,c(0,0),c(7.8,8.2),pmat=FFF),col="red",lwd=S) X=c(16,19,19,16,16,18,18,16,16) Y=rep(0,9) Z=c(0,0,12,12,11,11,1,1,0) lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=S) X=c(15,15,12,12,15) Y=rep(0,5) Z=c(0,12,12,0,0) lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=S) X=c(14,14,13,13,14) Y=rep(0,5) Z=c(1,11,11,1,1) lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=S) X=c(11,8,8,10,10,8,8,11,11,9,9,11,11) Y=rep(0,12) Z=c(0,0,7,7,11,11,12,12,6,6,1,1,0) lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=S) X=c(14,12.5,11.5,10,11,11.75,12.25,13,14)-7 Y=rep(0,9) Z=c(0,12,12,0,0,6,6,0,0) lines(trans3d(X,Y,Z,pmat=FFF),col="red",lwd=S) lines(trans3d(c(12,12)-7,c(0,0),c(7.8,8.2),pmat=FFF),col="red",lwd=S) # For the second image: thphdi = function(t,p,di) { par( mai = c(0,0,0,0) ) F = persp(x,y,z,theta=t,phi=p,scale=FALSE,zlim=w,xlim=x,ylim=y,d=di,box=FALSE,border="white") FFF <<- F # I put image in FFF to then use PXY command (or other command on the image) xm=30; ym=2; zm=15; co1="white"; co2="white" # grid (z=0 plane, x=0 plane, y=0 plane) if(co1!="white") for(i in seq(1,xm,1)) lines(trans3d(i,c(0,ym),0,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,ym,1)) lines(trans3d(c(0,xm),i,0,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,ym,1)) lines(trans3d(0,i,c(0,zm),pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,zm,1)) lines(trans3d(0,c(0,ym),i,pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,xm,1)) lines(trans3d(i,0,c(0,zm),pmat=F),col=co1,lty=3) if(co1!="white") for(i in seq(1,zm,1)) lines(trans3d(c(0,xm),0,i,pmat=F),col=co1,lty=3) # axes if(co2!="white") lines(trans3d(c(0,0),c(0,0),c(0,zm),pmat=F),col=co2,lty=3,lwd=3) if(co2!="white") lines(trans3d(c(0,xm),c(0,0),c(0,0),pmat=F),col=co2,lty=3,lwd=3) if(co2!="white") lines(trans3d(c(0,0),c(0,ym),c(0,0),pmat=F),col=co2,lty=3,lwd=3) } # With: BF=3; HF=2.5; NW(); thphdi(0,0,1e4) Other examples of use