#This script is to serve as a PSYCO 452 activity # to let students explore the difference between #basis8.net and ortho8.net training sets #Where do all those strange numbers come from? #Author: Michael R.W. Dawson #Date: August 18, 2014 #Make sure that the required R packages are installed #If not, uncomment the two lines below! #install.packages("rgl") #install.packages("compositions") #Load the required libraries; they handle the graphing library("rgl") library("compositions") ##################################################### # Set desired angles of rotation about x, y, z axes # # Values are assumed to be in degrees # ##################################################### #----------------- Define desired rotations xdeg <- 45 #later change this number to something else ydeg <- 30 #later change this number to something else zdeg <- 240 #later change this number to something else #convert these values into radians so that the cos and sin #functions will deliver the correct values #leave these alone! xrad <- (pi * xdeg)/180 yrad <- (pi * ydeg)/180 zrad <- (pi * zdeg)/180 ################################################################ #define the origin as (0,0,0) #define three basis vectors p1, p2, p3 ################################################################ origin <- c(0,0,0) #this is the origin p1 <- c(1,0,0) #this is the basis along the x axis p2 <- c(0,1,0) #this is the basis along the y axis p3 <- c(0,0,1) #this is the basis along the z axis ################################## # Define three rotation matrices # ################################## #rotating a 3D vector requires a 3X3 rotation matrix #whose values rotate the vector around one of the #three axes of 3D space #define the matrix that rotates about the X axis Xrotate = matrix(c(1,0,0,0,cos(xrad),sin(xrad),0, -1*sin(xrad),cos(xrad)),ncol=3) #Display the rotation matrix Xrotate #define the matrix that rotates about the y axis Yrotate = matrix(c(cos(yrad),0,sin(yrad),0,1,0, -1*sin(yrad),0, cos(yrad)),ncol=3) Yrotate #display it on the console #define the matrix that rotates about the X axis Zrotate = matrix(c(cos(zrad),-1*sin(zrad),0, sin(zrad),cos(zrad),0,0,0,1),ncol=3) Zrotate #display it on the console #################################################### # If you multiply all three matrices together # # then you create one new matrix that does all 3 # #rotations in one step # #################################################### # Xrotate * Yrotate * Zrotate = RotateXYZ #RotateXYZ is one transformation matrix that does all #three desired rotations in one operation RotateXY <- Xrotate%*%Yrotate #multiply two matrices RotateXYZ <- RotateXY%*%Zrotate #multiply third matrix #Display the complete transformation matrix RotateXYZ #------------------------------------------------- # Math pretty much ready, now lets use it to graph #------------------------------------------------- ##################################################### # Plot the three orignal vectors, and then plot the # # the three vectors produced by rotating them # #in an interactive graph # ##################################################### open3d(windowRect = c(00,00, 800, 800)) #rgl graphics window #Use rgl to draw a coordinate system decorate3d(c(-2,2), c(-2,2), c(-2,2), xlab = "x", ylab = "y", zlab = "z", box = TRUE, axes = TRUE, main = NULL, sub = NULL) #Draw each of the three original vectors in the system #arrows3d does this, and is part of the compositions #package arrows3D(origin,p1,length=0.1,lwd=5,col="black") arrows3D(origin,p2,length=0.1,lwd=5,col="black") arrows3D(origin,p3,length=0.1,lwd=5,col="black") ################################################################ #Rotate p1, p2, and p3 about the 3 axes using desired angles # ################################################################ #create three new pattern vectors using desired X rotation #this is a matrix algebra step! Newp1 <- RotateXYZ %*% p1 #transformation times p1 gives new p1 Newp2 <- RotateXYZ %*% p2 #ditto for p2 Newp3 <- RotateXYZ %*% p3 #ditto for p3 #Now add each of the three new vectors to the graph arrows3D(origin,t(Newp1),length=0.1,lwd=5,col="red") arrows3D(origin,t(Newp2),length=0.1,lwd=5,col="green") arrows3D(origin,t(Newp3),length=0.1,lwd=5,col="blue") ################################################################ #now that the plotting is done, print out the old and new #vectors to the console so that students can see the #weirdness of the new numbers, but realize from the #graph that the relations amongst the three patterns #is unchanged ################################################################ #print out original and new p1 p1 t(Newp1) #print transpose so it looks like p1 above #ditto for pattern 2 p2 t(Newp2) #print transpose so it looks like p2 above #finally pattern 3 p3 t(Newp3) #print transpose so it looks like p3 above