📄 rubik.java
字号:
import java.awt.*;
import java.applet.Applet;
public final class rubik extends Applet {
int i;
int j;
int k;
int n;
int o;
int p;
int q;
int lastX;
int lastY;
int dx;
int dy;
int rectX[];
int rectY[];
Color colList[];
Color bgcolor;
final double sideVec[] = { 0.0, 0.0, 1.0, 0.0, 0.0, -1, 0.0, -1, 0.0, 1.0,
0.0, 0.0, 0.0, 1.0, 0.0, -1, 0.0, 0.0 };
final double corners[] = { -1, -1, -1, 1.0, -1, -1, 1.0, 1.0, -1, -1, 1.0,
-1, -1, -1, 1.0, 1.0, -1, 1.0, 1.0, 1.0, 1.0, -1, 1.0, 1.0 };
double topCorners[];
double botCorners[];
final int sides[] = { 4, 5, 6, 7, 3, 2, 1, 0, 0, 1, 5, 4, 1, 2, 6, 5, 2, 3,
7, 6, 0, 4, 7, 3 };
final int nextSide[] = { 2, 3, 4, 5, 4, 3, 2, 5, 1, 3, 0, 5, 1, 4, 0, 2, 1,
5, 0, 3, 2, 0, 4, 1 };
final int mainBlocks[] = { 0, 3, 0, 3, 0, 3, 0, 3, 0, 3, 0, 3, 0, 3, 0, 3, 0,
3, 0, 3, 0, 3, 0, 3 };
final int twistDir[] = { -1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1,
-1, 1, 1, 1, 1, -1, 1, -1, 1 };
final int colDir[] = { -1, -1, 1, -1, 1, -1 };
final int circleOrder[] = { 0, 1, 2, 5, 8, 7, 6, 3 };
int topBlocks[];
int botBlocks[];
int sideCols[];
int sideW;
int sideH;
int dragReg;
int twistSide;
int nearSide[];
int buffer[];
double dragCorn[];
double dragDir[];
double eye[] = { 0.3651, 0.1826, -0.9129 };
double eX[] = { 0.9309, -0.0716, 0.3581 };
double eY[];
double Teye[];
double TeX[];
double TeY[];
double light[];
double temp[] = { 0.0, 0.0, 0.0 };
double temp2[] = { 0.0, 0.0, 0.0 };
double newCoord[];
double sx;
double sy;
double sdxh;
double sdyh;
double sdxv;
double sdyv;
double d;
double t1;
double t2;
double t3;
double t4;
double t5;
double t6;
double phi;
double phibase;
double Cphi;
double Sphi;
double currDragDir[];
boolean naturalState;
boolean twisting;
boolean OKtoDrag;
double local0;
Math m;
Graphics offGraphics;
Image offImage;
public void init() {
offImage = createImage(120, 120);
offGraphics = offImage.getGraphics();
rectX = new int[4];
rectY = new int[4];
newCoord = new double[16];
dragDir = new double[24];
dragCorn = new double[96];
topCorners = new double[24];
botCorners = new double[24];
topBlocks = new int[24];
botBlocks = new int[24];
buffer = new int[12];
nearSide = new int[12];
light = new double[3];
Teye = new double[3];
TeX = new double[3];
TeY = new double[3];
currDragDir = new double[2];
eY = new double[3];
vecProd(eye, 0, eX, 0, eY, 0);
normalize(eY, 0);
colList = new Color[120];
for (i = 0; i < 20; i++) {
colList[i] = new Color(103 + i * 8, 103 + i * 8, 103 + i * 8);
colList[i + 20] = new Color(i * 6, i * 6, 84 + i * 9);
colList[i + 40] = new Color(84 + i * 9, i * 5, i * 5);
colList[i + 60] = new Color(i * 6, 84 + i * 9, i * 6);
colList[i + 80] = new Color(84 + i * 9, 84 + i * 9, i * 6);
colList[i + 100] = new Color(84 + i * 9, 55 + i * 8, i * 3);
}
sideCols = new int[54];
for (i = 0; i < 54; i++)
sideCols[i] = i / 9;
bgcolor = findBGColor();
resize(125, 125);
repaint();
}
public Color findBGColor() {
Color color;
String string2 = "0123456789abcdef";
int an[] = new int[6];
String string1 = getParameter("bgcolor");
if (string1 != null && string1.length() == 6) {
for (i = 0; i < 6; i++)
for (j = 0; j < 16; j++)
if (string1.charAt(i) == string2.charAt(j)) an[i] = j;
color = new Color(an[0] * 16 + an[1], an[2] * 16 + an[3], an[4] * 16
+ an[5]);
} else color = Color.lightGray;
return color;
}
public double scalProd(double ad1[], int i, double ad2[], int j) {
return ad1[i] * ad2[j] + ad1[i + 1] * ad2[j + 1] + ad1[i + 2] * ad2[j + 2];
}
public double vNorm(double ad[], int i) {
return Math.sqrt(ad[i] * ad[i] + ad[i + 1] * ad[i + 1] + ad[i + 2]
* ad[i + 2]);
}
public double cosAng(double ad1[], int i, double ad2[], int j) {
return scalProd(ad1, i, ad2, j) / (vNorm(ad1, i) * vNorm(ad2, j));
}
public void normalize(double ad[], int i) {
local0 = vNorm(ad, i);
ad[i] = ad[i] / local0;
ad[i + 1] = ad[i + 1] / local0;
ad[i + 2] = ad[i + 2] / local0;
}
public void scalMult(double ad[], int i, double d) {
ad[i] = ad[i] * d;
ad[i + 1] = ad[i + 1] * d;
ad[i + 2] = ad[i + 2] * d;
}
public void addVec(double ad1[], int i, double ad2[], int j) {
ad2[j] += ad1[i];
ad2[j + 1] += ad1[i + 1];
ad2[j + 2] += ad1[i + 2];
}
public void subVec(double ad1[], int i, double ad2[], int j) {
ad2[j] -= ad1[i];
ad2[j + 1] -= ad1[i + 1];
ad2[j + 2] -= ad1[i + 2];
}
public void copyVec(double ad1[], int i, double ad2[], int j) {
ad2[j] = ad1[i];
ad2[j + 1] = ad1[i + 1];
ad2[j + 2] = ad1[i + 2];
}
public void vecProd(double ad1[], int i, double ad2[], int j, double ad3[],
int k) {
ad3[k] = ad1[i + 1] * ad2[j + 2] - ad1[i + 2] * ad2[j + 1];
ad3[k + 1] = ad1[i + 2] * ad2[j] - ad1[i] * ad2[j + 2];
ad3[k + 2] = ad1[i] * ad2[j + 1] - ad1[i + 1] * ad2[j];
}
public void cutUpCube() {
for (i = 0; i < 24; i++) {
topCorners[i] = corners[i];
botCorners[i] = corners[i];
}
copyVec(sideVec, 3 * twistSide, temp, 0);
copyVec(temp, 0, temp2, 0);
scalMult(temp, 0, 1.3333);
scalMult(temp2, 0, 0.6667);
for (i = 0; i < 8; i++) {
boolean flag = false;
for (j = 0; j < 4; j++)
if (i == sides[twistSide * 4 + j]) flag = true;
if (flag) subVec(temp2, 0, botCorners, i * 3);
else addVec(temp, 0, topCorners, i * 3);
}
for (i = 0; i < 24; i++) {
topBlocks[i] = mainBlocks[i];
botBlocks[i] = mainBlocks[i];
}
for (i = 0; i < 6; i++) {
if (i == twistSide) {
botBlocks[i * 4 + 1] = 0;
botBlocks[i * 4 + 3] = 0;
} else {
k = -1;
for (j = 0; j < 4; j++)
if (nextSide[i * 4 + j] == twistSide) k = j;
switch (k) {
case 0:
topBlocks[i * 4 + 3] = 1;
botBlocks[i * 4 + 2] = 1;
break;
case 1:
topBlocks[i * 4] = 2;
botBlocks[i * 4 + 1] = 2;
break;
case 2:
topBlocks[i * 4 + 2] = 2;
botBlocks[i * 4 + 3] = 2;
break;
case 3:
topBlocks[i * 4 + 1] = 1;
botBlocks[i * 4] = 1;
break;
case -1:
topBlocks[i * 4 + 1] = 0;
topBlocks[i * 4 + 3] = 0;
break;
}
}
}
}
public boolean keyDown(Event event, int i) {
if (i == 114) {
twisting = false;
naturalState = true;
for (this.i = 0; this.i < 54; this.i++)
sideCols[this.i] = this.i / 9;
repaint();
} else if (i == 115) {
twisting = false;
naturalState = true;
for (this.i = 0; this.i < 20; this.i++)
colorTwist((int) (Math.random() * 6), (int) (Math.random() * 3 + 1.0));
repaint();
}
return false;
}
public boolean mouseDrag(Event event, int i, int j) {
if (!twisting && OKtoDrag) {
OKtoDrag = false;
boolean flag = false;
for (this.i = 0; this.i < dragReg; this.i++) {
double d1 = dragCorn[this.i * 8 + 1] - dragCorn[this.i * 8];
double d2 = dragCorn[this.i * 8 + 5] - dragCorn[this.i * 8 + 4];
double d3 = dragCorn[this.i * 8 + 3] - dragCorn[this.i * 8];
double d4 = dragCorn[this.i * 8 + 7] - dragCorn[this.i * 8 + 4];
double d5 = (d4 * ((double) lastX - dragCorn[this.i * 8]) - d3
* ((double) lastY - dragCorn[this.i * 8 + 4]))
/ (d1 * d4 - d3 * d2);
double d6 = (-d2 * ((double) lastX - dragCorn[this.i * 8]) + d1
* ((double) lastY - dragCorn[this.i * 8 + 4]))
/ (d1 * d4 - d3 * d2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -