-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.java
More file actions
210 lines (167 loc) · 6.17 KB
/
Cube.java
File metadata and controls
210 lines (167 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* View license at project root. */
package com.gen.cube;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
/* The main cube used for display */
public class Cube{
private SubCube[][][] cube; // the set of 3*3*3=27 small cubes
private VirtualVect vect; // associated virtual axis
private ArrayList<SubCube> displayOrder; // display order of the sub cubes
private MetaCube meta; // the meta cube to store side colors
private VirtualVect spinVirt; // virtual axis used when rotating a side
private int numSpin; // spins into rotation
private int rotateDir; // direction or rotation
private int rotateSide; // side of rotation
public static final double drawFreq = 1.0; // draw frequency
public static final double rotateMove = 0.04 / drawFreq; // step size
public static final int maxNumSpin = (int) (40 * drawFreq); // max number of spins
// related to cube place and rotation
public static final int top=0, bottom=1, front=2, back=3, left=4, right=5;
public static final int counter=-1, clock=1;
public static final double third = 1.0/3.0;
public static final double half = 1.0/2.0;
/* Cube */
public Cube(){
meta = new MetaCube();
vect = new VirtualVect();
buildCube();
}
/* Set sub cube info */
public void buildCube() {
spinVirt = new VirtualVect();
numSpin = maxNumSpin;
cube = new SubCube[3][3][3];
for(int x=0;x<3;x++)
for(int y=0;y<3;y++)
for(int z=0;z<3;z++)
cube[x][y][z] = new SubCube(meta.colors(x,y,z),vect,
x*third-half,
y*third-half,
z*third-half);
generate();
}
/* Create array list of subcubes */
private void generate(){
displayOrder = new ArrayList<SubCube>();
for(int x=0;x<3;x++)
for(int y=0;y<3;y++)
for(int z=0;z<3;z++)
displayOrder.add(cube[x][y][z]);
}
/* Paint subcubes */
public void paint(Graphics g){
order();
for(int i=0;i<displayOrder.size();i++)
displayOrder.get(i).paint(g);
}
/* Order subcubes */
private void order(){
Collections.sort(displayOrder);
}
/* Rotate a side */
public void rotate(int side,int direct){
rotateDir = direct;
rotateSide = side;
switch(rotateSide){
case front: rotateBackFront(2); break;
case back: rotateBackFront(0); break;
case top: rotateBottomTop(2); break;
case bottom: rotateBottomTop(0); break;
case left: rotateRightLeft(2); break;
case right: rotateRightLeft(0); break;
}
}
/* Rotate */
private void rotateBackFront(int val){
spinVirt = new VirtualVect(vect.getV1(),vect.getV2(),vect.getV3());
numSpin = 0;
for(int x=0;x<3;x++)
for(int z=0;z<3;z++)
cube[x][val][z] = new SubCube(meta.colors(x,val,z),spinVirt,
x *third-half,
val*third-half,
z *third-half);
for(int x=0;x<3;x++)
for(int z=0;z<3;z++)
cube[x][val][z].setRot(rotateSide);
generate();
}
/* Rotate */
private void rotateBottomTop(int val){
spinVirt = new VirtualVect(vect.getV1(),vect.getV2(),vect.getV3());
numSpin = 0;
for(int x=0;x<3;x++)
for(int y=0;y<3;y++)
cube[x][y][val] = new SubCube(meta.colors(x,y,val),spinVirt,
x *third-half,
y *third-half,
val*third-half);
for(int x=0;x<3;x++)
for(int y=0;y<3;y++)
cube[x][y][val].setRot(rotateSide);
generate();
}
/* Rotate */
private void rotateRightLeft(int val){
spinVirt = new VirtualVect(vect.getV1(),vect.getV2(),vect.getV3());
numSpin = 0;
for(int y=0;y<3;y++)
for(int z=0;z<3;z++)
cube[val][y][z] = new SubCube(meta.colors(val,y,z),spinVirt,
val*third-half,
y *third-half,
z *third-half);
for(int y=0;y<3;y++)
for(int z=0;z<3;z++)
cube[val][y][z].setRot(rotateSide);
generate();
}
/* Rotate meta side */
public void rotateMetaSide(){
meta.rotate(rotateSide,rotateDir);
}
/* Mid rotation action */
public void continueRotate(){
if(rotateSide==right || rotateSide==left)
spinVirt.v1Still( rotateDir * rotateMove);
if(rotateSide==top || rotateSide==bottom)
spinVirt.v3Still( rotateDir * rotateMove);
if(rotateSide==back || rotateSide==front)
spinVirt.v2Still(-rotateDir * rotateMove);
numSpin++;
}
/* Indicates if still rotating a side */
public boolean stillRotate(){
if(numSpin<maxNumSpin)
return true;
return false;
}
/* Indicates if a face is clicked on
x=0:left x=2:right y=0:bottom y=2:top z=0:back z=2:front */
public boolean mouse(int side,int x,int y){
if(side == left ) return inFace(cube[2][1][1],x,y,side);
if(side == right ) return inFace(cube[0][1][1],x,y,side);
if(side == bottom) return inFace(cube[1][1][0],x,y,side);
if(side == top ) return inFace(cube[1][1][2],x,y,side);
if(side == back ) return inFace(cube[1][0][1],x,y,side);
if(side == front ) return inFace(cube[1][2][1],x,y,side);
return false;
}
/* True if (x,y) are in the side */
private boolean inFace(SubCube cube,int x,int y,int side){
if(cube.getCent().getZ() > 0)
return cube.getFace(side).contains(x,y);
return false;
}
/* Horizontal movement */
public void horiz(double x){
vect.horiz(x);
spinVirt.horiz(x);
}
/* Vertical movement */
public void vert(double y){
vect.vert(y);
spinVirt.vert(y);
}
}