计算机图形学 4.1.4 三维旋转 4.2.1 正投影(三视图)

三维旋转

x 轴转 α

变换矩阵为:

Trx=10000cosαsinα00sinαcosα00001

y 轴转 β

变换矩阵为:

Try=cosβ0sinβ00100sinβ0cosβ00001

z 轴转 γ

变换矩阵为:

Trz=cosγsinγ00sinγcosγ0000100001

正投影变换(三视图)

正面(V面)投影(主视图)

将物体向正面投影,即令 y=0 ,变换矩阵为:

TV=1000000000100001

点在 V 面上投影的坐标变换为:
[xyz1]=[xyz1]Tv=[x0z1]

水平面(H面)投影(俯视图)

将物体向水平面(H面)投影,即令 z=0 ,然后将得到的投影图绕 x 轴顺时针旋转 90 ,使其与 V 面共面,再沿负 z 方向平移一段距离,以使 H 面投影与 V 面投影之间保持一段距离。使变换矩阵为:

TH=100000000010000110000cos(π2)sin(π2)00sin(π2)cos(π2)0000110000100001n0001=10000000010n0001

点在 H 面上投影的坐标变换为:
[xyz1]=[xyz1]TH=[x0yn1]

侧面(W面)投影(侧视图)

将物体向侧面作正投影,即令 x=0 ,然后绕 z 轴逆时针旋转 90 ,使其与 V 面共面,为保证与正面投影有一段距离,再沿负 x 方向平移一段距离,这样即得到侧视图。
变换矩阵为:

TW=1000000000100001cosπ2sinπ200sinπ2cosπ20000100001100k010000100001=010k000000100001

点在 W 面上投影的坐标变换为:
[xyz1]=[xyz1]TW=[yk0z1]


Code and Demo

Demo
Processing3.0

class box2d{
  ArrayList points;
  int X,Y,Z;
  box2d(int x,int y,int z){
    X=x;
    Y=y;
    Z=z;
    points=new ArrayList();

    points.add(new PVector(0,0,0));
    points.add(new PVector(x,0,0));
    points.add(new PVector(0,y,0));
    points.add(new PVector(0,0,z));
    points.add(new PVector(x,y,0));
    points.add(new PVector(x,0,z));
    points.add(new PVector(0,y,z));
    points.add(new PVector(x,y,z));

    for(int i=1;inew PVector(i,0,0));
      points.add(new PVector(i,0,z));
      points.add(new PVector(i,y,0));
      points.add(new PVector(i,y,z));
    }
    for(int i=1;inew PVector(0,i,0));
      points.add(new PVector(0,i,z));
      points.add(new PVector(x,i,0));
      points.add(new PVector(x,i,z));
    }
    for(int i=1;inew PVector(0,0,i));
      points.add(new PVector(x,0,i));
      points.add(new PVector(0,y,i));
      points.add(new PVector(x,y,i));
    }

    for(int i=0;i" "+t.y+" "+t.z);
    }
  }

  void TH(PVector t){
    float x,y;
    x=t.x;
    y=t.y;
    point(int(x),int(-y+300));
  }
  void TV(PVector t){
    float x,z;
    x=t.x;
    z=t.z;
    point(int(x),int(z));
  }
  void TW(PVector t){
    float y,z;
    y=t.y;
    z=t.z;
    point(int(-y+300),int(z));
  }


  void show(){
    translate(100,100);
    fill(0, 102, 153, 204);
      text("V view",-20,-20);
      text("H view",-20,280);
      text("W view",280,-20);
      textSize(16);
      text("use x,y,z key to ratate, use +,- to control the speed.\nOther key will interrupt the ratate, and esc to exit",100,300);
    for(int i=0;ifloat a){
      return new PVector(t.x,t.y*cos(a)-t.z*sin(a),t.y*sin(a)+t.z*cos(a));
  }
  PVector ry(PVector t,float a){
      return new PVector(t.x*cos(a)+t.z*sin(a),t.y,-t.x*sin(a)+t.z*cos(a));
  }
  PVector rz(PVector t,float a){
      return new PVector(t.x*cos(a)-t.y*sin(a),t.x*sin(a)+t.y*cos(a),t.z);
  }
  void rotate(char f,float r){
    int n=points.size();
    for(int i=0;iswitch (f){
        case 'x':
          points.add(rx(points.remove(0),r));
          break;
        case 'y':
          points.add(ry(points.remove(0),r));
          break;
        case 'z':
          points.add(rz(points.remove(0),r));
          break;
      }
    }
  }
}

box2d bx;

void setup(){
  size(700,500);
  background(255);
  bx = new box2d(50,50,50);
}

char d;
float r=0.3;
void keyPressed(){
  switch (key){
    case '+':
      r+=0.1;
      break;
    case '-':
      r-=0.1;
      break;
    case ESC:
      exit();
    default:
      d=key;
  }
  if(r>1)
    r=1;
  if(r<0)
    r=0;
  println("R: "+r);
}


void draw(){
  background(255); 
  bx.rotate(d,r);
  bx.show();
}

你可能感兴趣的:(计算机图形学)