InitGraph(); ScaleWindow(-2.7, -2, 2.7, 2); Colour("red"); Label("a, A, s, S, d, D to rotate",-2,2); v=new Array(7); v[0]=new Array(-1,-1,-1); v[1]=new Array(-1,-1, 1); v[2]=new Array(-1, 1, 1); v[3]=new Array(-1, 1,-1); v[4]=new Array( 1, 1,-1); v[5]=new Array( 1, 1, 1); v[6]=new Array( 1,-1, 1); v[7]=new Array( 1,-1,-1); theta = .05; //the unit of rotation, theta var cth = Math.cos(theta); //saves calculating them var sth = Math.sin(theta); //each time var r=10; //distance of viewpoint function pline (i, j, c){ //draw projected line from vertex i to vertex j var si = 5 / (r - v[i][2]); var sj = 5 / (r - v[j][2]); Colour(c); LineStart(v[i][0] * si, v[i][1] * si) LineTo(v[j][0] * sj, v[j][1] * sj) } function rotate (i, j){ for(k = 0;k<=7;k++){ t = v[k][i] * cth + v[k][j] * sth; v[k][j] = v[k][j] * cth - v[k][i] * sth; v[k][i] = t; } } function showcube (c){ i = 7; for(j = 0;j<=7;j++){ pline (i, j, c); i = j; } pline (0, 3, c); pline (1, 6, c); pline (2, 5, c); pline (4, 7, c); } showcube("black"); //draw cube in black StepModel(); //Start the model running
//function StepModel() if(x!= "") { ctx.lineWidth=3; showcube("white"); //Draw cube in cream to rub out old switch(x){ //Which button has been pressed? case "a": rotate(1, 2); //Rotate about the 0 axis break; case "A": rotate(2, 1); //Reverse rotate about the 0 axis break; case "s": rotate(2, 0); break; case "S": rotate(0, 2); break; case "d": rotate(0, 1); break; case "D": rotate(1, 0); break; case "z": //Zoom - reduce the distance r r = r - .1; break; case "Z": r = r + .1; break; } ctx.lineWidth=1; showcube("black"); } if (x!="q"){ setTimeout('StepModel()',40); }else{Label('Stopped',2,2);};