//============================================================================= // The CubeSurfacePoint is used to describe one point on the surface of the cube, // i.e. one face of one cubelet. Instances can be created form a (x,y) pair of // screen coordinates, the class knows how to transform them. // // The class ignores the "mirrors" of the hidden faces. These are only drawn // and can't be used to manipulate the cube. //============================================================================= // Author: Michael Schubart // class CubeSurfacePoint { // on which cubelet lies the point? public int cubeX; public int cubeY; public int cubeZ; // and on which face of that cubelet? public int face; //========================================================================= // Constructor, finds out on which face of the cube the point is. Does // so by trying each face. // xPos, yPos: screen coordinates // xOffset, yOffset: offset, used when the background image does not start // at (0,0) //========================================================================= public CubeSurfacePoint(int xPos, int yPos, int xOffset, int yOffset) { transform(xPos, yPos, xOffset, yOffset, Cubelet.FACE_XV); if (!isOnCube()) { transform(xPos, yPos, xOffset, yOffset, Cubelet.FACE_YV); } if (!isOnCube()) { transform(xPos, yPos, xOffset, yOffset, Cubelet.FACE_ZV); } } //========================================================================= // Constructor, we specify on which face of the cube the point is. // xPos, yPos: screen coordinates // xOffset, yOffset: background image offset // face: the point is on this face //========================================================================= public CubeSurfacePoint(int xPos, int yPos, int xOffset, int yOffset, int face) { transform(xPos, yPos, xOffset, yOffset, face); } //========================================================================= // Can be used to query whether the object describes an existing surface // point. // return value: yes, it does //========================================================================= public boolean isOnCube() { return (cubeX >= 0 && cubeY >=0 && cubeZ >=0 && cubeX <= 2 && cubeY <=2 && cubeZ <=2); } //========================================================================= // Transforms screen coordinates to a surface point, we specify on which // face of the cube the point is. // xPos, yPos: screen coordinates // xOffset, yOffset: background image offset // face: the point is on this face //========================================================================= void transform(int xPos, int yPos, int xOffset, int yOffset, int face) { double x,y; switch (face) { case Cubelet.FACE_XV: x = xPos - 237 - xOffset; y = yPos - 182 - yOffset; cubeX = 2; cubeY = (int) Math.floor(-x / 64 - y / 32); cubeZ = (int) Math.floor(-x / 28); break; case Cubelet.FACE_YV: x = xPos - 153 - xOffset; y = yPos - 45 - yOffset; cubeX = (int) Math.floor(x / 56 + y / 28); cubeY = 2; cubeZ = (int) Math.floor(-x / 56 + y / 28); break; case Cubelet.FACE_ZV: x = xPos - 70 - xOffset; y = yPos - 182 - yOffset; cubeX = (int) Math.floor (x / 28); cubeY = (int) Math.floor (x / 64 - y / 32); cubeZ = 2; break; } this.face=face; } }