是什么旋转矩阵?
是不是三维坐标变换的?

解决方案 »

  1.   

    这是我以前从一本书上抄下来的
    你拿回去研究研究吧public class Cube {
      Point3d w[] = new Point3d[8];
      Point v[] = new Point[8];
      Square sides[] = new Square[6];
      Matrix3d metrix = new Matrix3d();
      Color color =Color.black;  public Cube(float size) {
        w[0] = new Point3d ( -size, -size, -size);
        w[1] = new Point3d ( -size,  size, -size);
        w[2] = new Point3d (  size,  size, -size);
        w[3] = new Point3d (  size, -size, -size);
        w[4] = new Point3d ( -size, -size,  size);
        w[5] = new Point3d ( -size,  size,  size);
        w[6] = new Point3d (  size,  size,  size);
        w[7] = new Point3d (  size, -size,  size);
        sides[0] = new Square( this, 0, 1, 2, 3 ); // 顶面
        sides[1] = new Square( this, 7, 6, 5, 4 ); // 底面
        sides[2] = new Square( this, 0, 1, 5, 4 ); // 前面
        sides[3] = new Square( this, 1, 2, 6, 5 ); // 右面
        sides[4] = new Square( this, 2, 3, 7, 6 ); // 背面
        sides[5] = new Square( this, 3, 0, 4, 7 ); // 左面
      }  public void draw(Graphics g ,Point offset){
        g.setColor(color);
        for(int i=0; i < sides.length; i++)
          sides[i].draw(g ,offset);
      }  public void transform(Matrix3d toscreen){
          Matrix3d m = new Matrix3d();
          m.multiply(toscreen,Matrix3d.REPLACE);
          m.multiply(metrix,Matrix3d.PRECONCAT);      for(int i=0; i < v.length; i++)
            v[i] = m.transform2D(w[i]);
      }
    }public class Matrix3d {
      float e00,e01,e02,e03;
      float e10,e11,e12,e13;
      float e20,e21,e22,e23;
      float e30,e31,e32,e33;  static final double toRad = Math.PI/180.0;  static int REPLACE =0;
      static int PRECONCAT = 1;
      static int POSTCONCAT = 2;  public Matrix3d() {
        identity();
      }  public void identity(){
        e01 = e02 = e03 = 0.0f;
        e10 = e12 = e13 = 0.0f;
        e20 = e21 = e23 = 0.0f;
        e30 = e32 = e32 = 0.0f;
        e00 = 1.0f;
        e11 = 1.0f;
        e22 = 1.0f;
      }  public Point3d transform(Point3d p){
        float x = p.x * e00 + p.y * e01 + p.z * e02 + e03;
        float y = p.x * e10 + p.y * e11 + p.z * e12 + e13;
        float z = p.x * e20 + p.y * e21 + p.z * e22 + e23;    return(new Point3d(x,y,z));
      }  public void multiply(Matrix3d matrix, int concat){ //并置矩阵
        Matrix3d A = null;
        Matrix3d B = null;
        if(concat == Matrix3d.PRECONCAT){
          A = matrix;
          B = this;
        }else if(concat == Matrix3d.POSTCONCAT){
          A = this;
          A.identity();
          B = matrix;
        }    float n00 = A.e00 * B.e00 + A.e10 * B.e01 + A.e20 * B.e02;
        float n01 = A.e01 * B.e00 + A.e11 * B.e01 + A.e21 * B.e02;
        float n02 = A.e02 * B.e00 + A.e12 * B.e01 + A.e22 * B.e02;
        float n03 = A.e03 * B.e00 + A.e13 * B.e01 + A.e23 * B.e02 + B.e03;    float n10 = A.e00 * B.e10 + A.e10 * B.e11 + A.e20 * B.e12;
        float n11 = A.e01 * B.e10 + A.e11 * B.e11 + A.e21 * B.e12;
        float n12 = A.e02 * B.e10 + A.e12 * B.e11 + A.e22 * B.e12;
        float n13 = A.e03 * B.e10 + A.e13 * B.e11 + A.e23 * B.e12 + B.e13;    float n20 = A.e00 * B.e20 + A.e10 * B.e21 + A.e20 * B.e22;
        float n21 = A.e01 * B.e20 + A.e11 * B.e21 + A.e21 * B.e22;
        float n22 = A.e02 * B.e20 + A.e12 * B.e21 + A.e22 * B.e22;
        float n23 = A.e03 * B.e20 + A.e13 * B.e21 + A.e23 * B.e22 + B.e23;    e00 = n00;
        e01 = n01;
        e02 = n02;
        e03 = n03;    e10 = n10;
        e11 = n11;
        e12 = n12;
        e13 = n13;    e20 = n20;
        e21 = n21;
        e22 = n22;
        e23 = n23;
      }  public void translate(float x,float y,float z){ //平移
        e03 += x;
        e13 += y;
        e23 += z;
      }  public void scale(float x,float y,float z){ //缩放
        e00 *= x;
        e11 *= y;
        e22 *= z;
      }
      public void scale(float s){ //缩放
        this.scale(s,s,s);
      }  public void rotateX(double theta){ //X旋转
        theta *= toRad;
        double theta_cos = Math.cos(theta);
        double theta_sin = Math.sin(theta);
        Matrix3d m = new Matrix3d();
        m.e11 = (float)theta_cos;
        m.e12 = (float)-theta_sin;
        m.e21 = (float)theta_sin;
        m.e22 = (float)theta_cos;
        this.multiply(m,Matrix3d.POSTCONCAT);
      }
      public void rotateY(double theta){ //Y旋转
        theta *= toRad;
        double theta_cos = Math.cos(theta);
        double theta_sin = Math.sin(theta);
        Matrix3d m = new Matrix3d();
        m.e00 = (float)theta_cos;
        m.e02 = (float)theta_sin;
        m.e20 = (float)-theta_sin;
        m.e22 = (float)theta_cos;
        this.multiply(m,Matrix3d.POSTCONCAT);
      }
      public void rotateZ(double theta){ //Z旋转
        theta *= toRad;
        double theta_cos = Math.cos(theta);
        double theta_sin = Math.sin(theta);
        Matrix3d m = new Matrix3d();
        m.e00 = (float)theta_cos;
        m.e01 = (float)-theta_sin;
        m.e10 = (float)theta_sin;
        m.e11 = (float)theta_cos;
        this.multiply(m,Matrix3d.POSTCONCAT);
      }  public Point transform2DPer(Point3d p,float d){ //透视
        float xf = p.x * e00 + p.y * e01 + p.z * e02 +e03;
        float yf = p.x * e10 + p.y * e11 + p.z * e12 +e13;
        float zf = p.x * e20 + p.y * e21 + p.z * e22 +e23;    int x = Math.round(d * xf / zf);
        int y = Math.round(d * yf / zf);    return(new Point(x,y));
      }  public Point transform2D(Point3d p){
        float xf = p.x * e00 + p.y * e01 + p.z * e02 +e03;
        float yf = p.x * e10 + p.y * e11 + p.z * e12 +e13;
        float zf = p.x * e20 + p.y * e21 + p.z * e22 +e23;    int x = Math.round(xf / zf);
        int y = Math.round(yf / zf);    return(new Point(x,y));
      }
    }public class Point3d {
      float x = 0f;
      float y = 0f;
      float z = 0f;  public Point3d(float x0,float y0,float z0) {
        x = x0;
        y = y0;
        z = z0;
      }  public String toString(){
        return(new String("(" + x + "," + y + "," + z +")"));
      }
    }class Square {
      int index[] = null;
      Cube cube;  public Square(Cube c,int v0,int v1,int v2,int v3) {
        cube = c;
        index[0] = v0;
        index[1] = v1;
        index[2] = v2;
        index[3] = v3;
      }  public void draw(Graphics g,Point offset){
        Polygon poly = new Polygon();
        for(int i=0; i < index.length; i++){
          poly.addPoint(offset.x+cube.v[index[i]].x,offset.y+cube.v[index[i]].y);
          poly.addPoint(offset.x+cube.v[index[0]].x,offset.y+cube.v[index[0]].y);
          g.drawPolygon(poly);
        }
      }}