Rotating cube program
************************************************
file cube.java:
import java.applet.*;
import java.awt.*;
public class cube extends Applet {
/*draw a wireframe image of a cube and rotate it; */
double theta=30; /*theta and phi give angles of rotation*/
double phi=0;
wirefr cube= new wirefr();
public void init () {
setBackground(Color .black);
/*l1 to l4 are vertices on the lower face of the cube;
t1 to t4 are vertices on the upper face of the cube*/
xyzpoint l1=new xyzpoint(-1,-1,-1);
xyzpoint l2=new xyzpoint( 1,-1,-1);
xyzpoint l3=new xyzpoint( 1, 1,-1);
xyzpoint l4=new xyzpoint(-1, 1,-1);
xyzpoint t1=new xyzpoint(-1,-1, 1);
xyzpoint t2=new xyzpoint( 1,-1, 1);
xyzpoint t3=new xyzpoint( 1, 1, 1);
xyzpoint t4=new xyzpoint(-1, 1, 1);
Color c1=Color.cyan;
Color c2=Color.blue;
Color c3=Color.magenta;
cube.addline(l1,l2,c1);
cube.addline(l2,l3,c1);
cube.addline(l3,l4,c1);
cube.addline(l4,l1,c1);
cube.addline(t1,t2,c2);
cube.addline(t2,t3,c2);
cube.addline(t3,t4,c2);
cube.addline(t4,t1,c2);
cube.addline(l1,t1,c3);
cube.addline(l2,t2,c3);
cube.addline(l3,t3,Color.white);
cube.addline(l4,t4,Color.white);
cpjxy.xscale=5;
cpjxy.yscale=5;
cpjxy.xc=0;
cpjxy.yc=0;
} /*end init*/
public void paint(Graphics g) {
double d=1; /*amount to rotate angles, in degrees*/
cube.show(theta,phi,g);
phi+=d;
/*the next lines introduce a time delay of
(200 milliseconds)*/
try {
Thread.currentThread().sleep (200);
} catch (InterruptedException ex) { }
repaint();
} /*end paint*/
} /*end class cube*/
**********************************
file wirefr.java:
import java.awt.*;
public class wirefr {
/*This class represents a wireframe object as an array of xyzline objects*/
int num=0; /*number of lines*/
int maxlines=50; /*maximum number of lines*/
xyzline linelist[]=new xyzline[maxlines+1];
/*method*/ void addline(xyzpoint a, xyzpoint b, Color c) {
/*add one additional line to the wireframe object*/
xyzline L=new xyzline(a,b,c);
num++;
linelist[num]=L;
} /*end addline*/
/*method*/ void addline(double ax, double ay, double az,
double bx, double by, double bz, Color c) {
/*add one additional line to the wireframe object;
the coordinates of the end points of the line in the original
coordinate space are (ax, ay, az) and (bx, by, bz) */
xyzpoint a=new xyzpoint(ax,ay,az);
xyzpoint b=new xyzpoint(bx,by,bz);
addline(a,b,c);
} /*end addline*/
/*method*/ void show(double theta, double phi, Graphics g) {
for (int i=1; i<=num; i++) {
linelist[i].show(theta,phi,g);
} /*end i loop*/
} /*end show*/
} /*end class wirefr*/
*****************************
file xyzline.java:
import java.awt.*;
public class xyzline {
/*this class represents a line connecting two points in three dimensional space*/
xyzpoint p1;
xyzpoint p2;
Color c;
/*constructor*/ public xyzline(xyzpoint p1, xyzpoint p2, Color c) {
this.p1=p1;
this.p2=p2;
this.c = c;
} /*end constructor*/
/*method*/ void show(double theta, double phi, Graphics g) {
p1.rotate(theta,phi);
p2.rotate(theta,phi);
cpjxy.dline(p1.x2, p1.y2, p2.x2, p2.y2, g, c);
} /*end show*/
} /*end xyzline*/
******************************************
file xyzpoint.java:
public class xyzpoint {
/*this class represents a point in three dimensional space*/
/*these are the coordinates as measured in the original coordinate system*/
double x=0;
double y=0;
double z=0;
/*these are the coordinates measured in the rotated coordinate system*/
double x2=0;
double y2=0;
double z2=0;
/*constructor*/ public xyzpoint(double x, double y, double z) {
this.x=x; this.y=y; this.z=z;
} /*end constructor*/
/*method*/ void rotate(double theta, double phi) {
/*this method takes the angles in degrees; here
they are converted to radians:*/
double t=theta*Math.PI/180;
double p=phi*Math.PI/180;
x2=x*Math.cos(t)*Math.cos(p) + y*Math.sin(t)*Math.cos(p) - z*Math.sin(p);
y2=y*Math.cos(t)-x*Math.sin(t);
z2=z*Math.cos(p)+x*Math.cos(t)*Math.sin(p)+y*Math.sin(t)*Math.sin(p);
} /*end rotate*/
} /*end xyzpoint*/