⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rotator3d.java

📁 please read it,you can
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                colours[i][1]= new Color(255-(ncolour-1-i)*100/ncolour,0,0);                // green                colours[i][2]= new Color(0,255-(ncolour-1-i)*100/ncolour,0);                // blue                colours[i][3]= new Color(0,0,255-(ncolour-1-i)*100/ncolour);                // yellow                colours[i][4]= new Color(255-(ncolour-1-i)*100/ncolour,                    255-(ncolour-1-i)*100/ncolour,0);                // cyan                colours[i][5]= new Color(0, 255-(ncolour-1-i)*100/ncolour,                    255-(ncolour-1-i)*100/ncolour);                // magenta                colours[i][6]= new Color(255-(ncolour-1-i)*100/ncolour, 0,                    255-(ncolour-1-i)*100/ncolour);            }                double len = Math.sqrt(lightvec[0]*lightvec[0] +                                    lightvec[1]*lightvec[1] +                                    lightvec[2]*lightvec[2]);            lightvec[0] = lightvec[0]/len;            lightvec[1] = lightvec[1]/len;            lightvec[2] = lightvec[2]/len;                double max = 0;            for (int i = 0; i < npoint; i++) {                len = Math.sqrt(points[i][0]*points[i][0]                         + points[i][1]*points[i][1]                         + points[i][2]*points[i][2]);                if (len >max) {                    max = len;                }            }                   for (int i = 0; i < nface; i++) {                len = Math.sqrt(points[i][0]*points[i][0]                         + points[i][1]*points[i][1]                         + points[i][2]*points[i][2]);                points[i][0] = points[i][0]/len;                points[i][1] = points[i][1]/len;                points[i][2] = points[i][2]/len;            }                orient = new Matrix3D();            tmp = new Matrix3D();            tmp2 = new Matrix3D();            tmp3 = new Matrix3D();            tmp.Rotation(2,0,Math.PI/50);            CalcScrPts((double) w/3,(double) h/3, 0);                scale = Math.min(w/3/max/1.2, h/3/max/1.2);            scaleAmt = scale;            scale *= Math.random() * 1.5;            scaleDirection = scaleAmt < scale ? DOWN : UP;        }            // gets the color based on the current rotation and lightsource        private Color getColour(int f,int index) {            colour = (int) ((rotPts[f][0]*lightvec[0]                        + rotPts[f][1]*lightvec[1]                         + rotPts[f][2]*lightvec[2])*ncolour);             if (colour < 0) {                colour = 0;            }            if (colour > ncolour-1) {                colour = ncolour-1;            }            return colours[colour][polygons[faces[f][index]][1]];        }                  // performs matrix multiplication to generate rotated points        private void CalcScrPts(double x, double y, double z) {            for (p = 0; p < npoint; p++) {                rotPts[p][2] = points[p][0]*orient.M[2][0] +                                points[p][1]*orient.M[2][1] +                               points[p][2]*orient.M[2][2];                rotPts[p][0] = points[p][0]*orient.M[0][0] +                                points[p][1]*orient.M[0][1] +                               points[p][2]*orient.M[0][2];                rotPts[p][1] = -points[p][0]*orient.M[1][0] -                                points[p][1]*orient.M[1][1] -                               points[p][2]*orient.M[1][2];            }            for (p = nface; p < npoint; p++) {                rotPts[p][2] += z;                persp = (Zeye - rotPts[p][2])/(scale*Zeye);                scrPts[p][0] = (int)(rotPts[p][0]/persp+x);                scrPts[p][1] = (int)(rotPts[p][1]/persp+y);            }        }            // tests if the specified face is showing        private boolean faceUp(int f) {            return (rotPts[f][0]*rotPts[nface+f][0] +                     rotPts[f][1]*rotPts[nface+f][1] +                     rotPts[f][2]*(rotPts[nface+f][2]-Zeye) < 0);        }                /*         * advances the x and y coordinates, scaling factors and         * directions and angle of rotation         */        public void step(int w, int h) {            x += ix;            y += iy;            if (x > w-scale) {                x = w-scale-1;                ix = -w/100 - 1;            }            if (x-scale < 0) {                x = 2+scale;                ix = w/100 + Math.random()*3;            }            if (y > h-scale) {                y = h-scale-2;                iy = -h/100 - 1;            }            if (y-scale < 0) {                y = 2+scale;                iy = h/100 + Math.random()*3;            }                angle += Math.random() * 0.15;            tmp3.Rotation(1, 2, angle);            tmp2.Rotation(1, 0, angle*Math.sqrt(2)/2);            tmp.Rotation(0, 2, angle*Math.PI/4);            orient.M = tmp3.Times(tmp2.Times(tmp.M));            bounce = Math.abs(Math.cos(0.5*(angle)))*2-1;                if (scale > scaleAmt*1.4) {                scaleDirection = DOWN;            }            if (scale < scaleAmt*0.4) {                scaleDirection = UP;            }            if (scaleDirection == UP) {                scale += Math.random();            }            if (scaleDirection == DOWN) {                scale -= Math.random();            }                CalcScrPts(x, y, bounce);        }            // calls DrawPoly on each of the showing faces of the 3D objects        public void Draw(Graphics2D g2) {            for (int f = 0;f < nface; f++) {                if (faceUp(f)) {                    for (j = 1;j < faces[f][0]+1; j++) {                        DrawPoly(g2, faces[f][j], getColour(f,j));                    }                }            }         }             // draws and fills the faces of the specified polygon        private void DrawPoly(Graphics2D g2, int poly, Color colour) {            for (int p = 2; p < polygons[poly][0]+2; p++) {                xx[p-2] = scrPts[polygons[poly][p]][0];                yy[p-2] = scrPts[polygons[poly][p]][1];            }            g2.setColor(colour);             g2.fillPolygon(xx,yy,polygons[poly][0]);            g2.setColor(Color.black);             g2.drawPolygon(xx,yy,polygons[poly][0]);        }                /**          * The Matrix3D class defines a matrix to be used to perform         * the rotation.         */        public class Matrix3D {                    public double[][] M = { { 1, 0, 0 },                                    { 0, 1, 0 },                                    { 0, 0, 1 } };            private double[][] tmp = new double[3][3];            private int row, col, k;                     public void Rotation(int i, int j, double angle) {                for (row = 0; row < 3; row++) {                    for (col = 0; col <3; col++) {                        if (row != col) {                             M[row][col] = 0.0;                         } else {                             M[row][col] = 1.0;                         }                    }                }                M[i][i] = Math.cos(angle);                M[j][j] = Math.cos(angle);                M[i][j] = Math.sin(angle);                M[j][i] = -Math.sin(angle);            }                    public double[][] Times(double[][] N) {                for (row = 0; row < 3; row++) {                    for (col = 0; col < 3; col++) {                        tmp[row][col] = 0.0;                        for (k = 0; k < 3; k++) {                            tmp[row][col] += M[row][k] * N[k][col];                        }                    }                }                return tmp;            }        } // End Matrix3D class    } // End Objects3D class    public static void main(String argv[]) {        final Rotator3D demo = new Rotator3D();        demo.init();        Frame f = new Frame("Java 2D(TM) Demo - Rotator3D");        f.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {System.exit(0);}            public void windowDeiconified(WindowEvent e) { demo.start(); }            public void windowIconified(WindowEvent e) { demo.stop(); }        });        f.add(demo);        f.pack();        f.setSize(new Dimension(400,300));        f.show();        demo.start();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -