// the figure to render on the screen Figure figure; // viewpoint from which we look at the 3D scene Point3D EYE = new Point3D(0,0,-3); // angle by which to rotate double ANGLE = 0.1; // amount by which to translate double TRANS = 0.5; // this number denotes the plane z = ZOOM on which the scene // is going to be projected double ZOOM = -2; // factor by which to zoom in or out double ZOOM_DIST = 0.5; void setup() { size(350,350); // set the size of the canvas noLoop(); // we do not need animations figure = makeCube(); //figure = makeTetrahedron(); } void draw() { background(255); // clear the screen first figure.draw(EYE, ZOOM); } // make a cube based on 6 quadrilaterals Figure makeCube() { // TODO: fill in the points and the side planes Point3D a = new Point3D(0,0,0); Point3D b = new Point3D(0,0,0); Point3D c = new Point3D(0,0,0); Point3D d = new Point3D(0,0,0); Point3D e = new Point3D(0,0,0); Point3D f = new Point3D(0,0,0); Point3D g = new Point3D(0,0,0); Point3D h = new Point3D(0,0,0); Polygon[] faces = new Polygon[6]; faces[0] = makeQuad(a,a,a,a); faces[1] = makeQuad(a,a,a,a); faces[2] = makeQuad(a,a,a,a); faces[3] = makeQuad(a,a,a,a); faces[4] = makeQuad(a,a,a,a); faces[5] = makeQuad(a,a,a,a); return new Figure(faces); } // make a tetrahedron based on 4 triangle sides Figure makeTetrahedron() { // TODO: fill in points and plane coordinates Point3D a = new Point3D(0,0,0); Point3D b = new Point3D(0,0,0); Point3D c = new Point3D(0,0,0); Point3D d = new Point3D(0,0,0); Polygon[] faces = new Polygon[4]; faces[0] = makeTriangle(a,a,a); faces[1] = makeTriangle(a,a,a); faces[2] = makeTriangle(a,a,a); faces[3] = makeTriangle(a,a,a); return new Figure(faces); } Polygon makeQuad(Point3D a, Point3D b, Point3D c, Point3D d) { return new Polygon(new Point3D[] { a,b,c,d }); } Polygon makeTriangle(Point3D a, Point3D b, Point3D c) { return new Polygon(new Point3D[] { a,b,c }); } // called when we want to zoom in void zoomIn(double distance) { // TODO: zoom in code println("zoom in"); } // called when we want to zoom out void zoomOut(double distance) { // TODO: zoom out code println("zoom out"); } // called when we want to rotate void rotateX(double angle) { figure.rotateX(angle); println("rotateX"); } void rotateY(double angle) { figure.rotateY(angle); println("rotateY"); } void rotateZ(double angle) { figure.rotateZ(angle); println("rotateZ"); } // called when we want to translate public void translate(double x, double y, double z) { figure.translate(x,y,z); println("translate"); } /** * A figure is a 3D polyhedron consisting of a number of faces, * each being a polygon in 3-space */ class Figure { Polygon[] faces; Figure(Polygon[] faces) { this.faces = faces; } /* Translate the figure using offsets along the three principal axes */ void translate(double xofs, double yofs, double zofs) { for (int i=0; i