1、魔方源代码Java Applet小程序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, -
2、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,
3、 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
4、, 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 =
5、 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; doubl
6、e 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();
7、 rectX = new int4; rectY = new int4; newCoord = new double16; dragDir = new double24; dragCorn = new double96; topCorners = new double24; botCorners = new double24; topBlocks = new int24; botBlocks = new int24; buffer = new int12; nearSide = new int12; light = new double3; Teye = new double3; TeX =
8、new double3; TeY = new double3; currDragDir = new double2; eY = new double3; vecProd(eye, 0, eX, 0, eY, 0); normalize(eY, 0); colList = new Color120; for (i = 0; i 20; i+) colListi = new Color(103 + i * 8, 103 + i * 8, 103 + i * 8); colListi + 20 = new Color(i * 6, i * 6, 84 + i * 9); colListi + 40
9、= new Color(84 + i * 9, i * 5, i * 5); colListi + 60 = new Color(i * 6, 84 + i * 9, i * 6); colListi + 80 = new Color(84 + i * 9, 84 + i * 9, i * 6); colListi + 100 = new Color(84 + i * 9, 55 + i * 8, i * 3); sideCols = new int54; for (i = 0; i 54; i+) sideColsi = i / 9; bgcolor = findBGColor(); res
10、ize(125, 125); repaint(); public Color findBGColor() Color color; String string2 = 0123456789abcdef; int an = new int6; 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) ani = j; co
11、lor = new Color(an0 * 16 + an1, an2 * 16 + an3, an4 * 16 + an5); else color = Color.lightGray; return color; public double scalProd(double ad1, int i, double ad2, int j) return ad1i * ad2j + ad1i + 1 * ad2j + 1 + ad1i + 2 * ad2j + 2; public double vNorm(double ad, int i) return Math.sqrt(adi * adi +
12、 adi + 1 * adi + 1 + adi + 2 * adi + 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); adi = adi / local0; adi + 1 = adi + 1 / local0; adi + 2 = adi + 2 / loc
13、al0; public void scalMult(double ad, int i, double d) adi = adi * d; adi + 1 = adi + 1 * d; adi + 2 = adi + 2 * d; public void addVec(double ad1, int i, double ad2, int j) ad2j += ad1i; ad2j + 1 += ad1i + 1; ad2j + 2 += ad1i + 2; public void subVec(double ad1, int i, double ad2, int j) ad2j -= ad1i;
14、 ad2j + 1 -= ad1i + 1; ad2j + 2 -= ad1i + 2; public void copyVec(double ad1, int i, double ad2, int j) ad2j = ad1i; ad2j + 1 = ad1i + 1; ad2j + 2 = ad1i + 2; public void vecProd(double ad1, int i, double ad2, int j, double ad3, int k) ad3k = ad1i + 1 * ad2j + 2 - ad1i + 2 * ad2j + 1; ad3k + 1 = ad1i
15、 + 2 * ad2j - ad1i * ad2j + 2; ad3k + 2 = ad1i * ad2j + 1 - ad1i + 1 * ad2j; public void cutUpCube() for (i = 0; i 24; i+) topCornersi = cornersi; botCornersi = cornersi; copyVec(sideVec, 3 * twistSide, temp, 0); copyVec(temp, 0, temp2, 0); scalMult(temp, 0, 1.3333); scalMult(temp2, 0, 0.6667); for
16、(i = 0; i 8; i+) boolean flag = false; for (j = 0; j 4; j+) if (i = sidestwistSide * 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+) topBlocksi = mainBlocksi; botBlocksi = mainBlocksi; for (i = 0; i 6; i+) if (i = twis
17、tSide) botBlocksi * 4 + 1 = 0; botBlocksi * 4 + 3 = 0; else k = -1; for (j = 0; j 4; j+) if (nextSidei * 4 + j = twistSide) k = j; switch (k) case 0: topBlocksi * 4 + 3 = 1; botBlocksi * 4 + 2 = 1; break; case 1: topBlocksi * 4 = 2; botBlocksi * 4 + 1 = 2; break; case 2: topBlocksi * 4 + 2 = 2; botB
18、locksi * 4 + 3 = 2; break; case 3: topBlocksi * 4 + 1 = 1; botBlocksi * 4 = 1; break; case -1: topBlocksi * 4 + 1 = 0; topBlocksi * 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+) sideColsthis.i = t
19、his.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 = fa
20、lse; boolean flag = false; for (this.i = 0; this.i 0.0 & d5 0.0 & d6 0.6) flag = true; twistSide = nearSidethis.i; this.i = 100; if (flag) if (naturalState) cutUpCube(); naturalState = false; twisting = true; phi = 0.02 * (currDragDir0 * (i - lastX) + currDragDir1 * (j - lastY) / Math.sqrt(currDragD
21、ir0 * currDragDir0 + currDragDir1 * currDragDir1); repaint(); return false; OKtoDrag = false; if (!twisting) dx = lastX - i; copyVec(eX, 0, temp, 0); scalMult(temp, 0, (double)dx * 0.016); addVec(temp, 0, eye, 0); vecProd(eY, 0, eye, 0, eX, 0); normalize(eX, 0); normalize(eye, 0); dy = j - lastY; co
22、pyVec(eY, 0, temp, 0); scalMult(temp, 0, (double)dy * 0.016); addVec(temp, 0, eye, 0); vecProd(eye, 0, eX, 0, eY, 0); normalize(eY, 0); normalize(eye, 0); lastX = i; lastY = j; repaint(); else phi = 0.02 * (currDragDir0 * (i - lastX) + currDragDir1 * (j - lastY) / Math.sqrt(currDragDir0 * currDragDi
23、r0 + currDragDir1 * currDragDir1); repaint(); return false; public boolean mouseDown(Event event, int i, int j) lastX = i; lastY = j; OKtoDrag = true; return false; public boolean mouseUp(Event event, int i, int j) if (twisting) double d; twisting = false; phibase += phi; phi = 0.0; for (d = phibase
24、; d 0.0; d += 125.662) /* null body */ ; int k = (int)(d * 3.183); if (k % 5 = 0 | k % 5 = 4) k = (k + 1) / 5 % 4; if (colDirtwistSide 0) k = (4 - k) % 4; phibase = 0.0; naturalState = true; colorTwist(twistSide, k); repaint(); return false; public void colorTwist(int i1, int j1) int k4 = 0; int j4
25、= j1 * 2; for (int k1 = 0; k1 8; k1+) bufferj4 = sideColsi1 * 9 + circleOrderk1; j4 = (j4 + 1) % 8; for (int i2 = 0; i2 8; i2+) sideColsi1 * 9 + circleOrderi2 = bufferi2; j4 = j1 * 3; for (int j2 = 0; j2 4; j2+) for (int i3 = 0; i3 4; i3+) if (nextSidenextSidei1 * 4 + j2 * 4 + i3 = i1) k4 = i3; for (int j3 = 0; j3 3; j3+) switch (k4) case 0: bufferj4 = sideColsnextSidei1 * 4 + j2 * 9 + j3; break; case 1: bufferj4 = sideColsnextSidei1 * 4 + j2 * 9 + 2 + 3 * j3; break; case 2: bufferj4
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1