/矩阵的乘法
//*********************
void CTransGraphicsView::MutiTransMarix(double a[1][4],double b[4][4],double MutiResult[1][4])
{
for(int i=0;i<4;i++) MutiResult[0][i] = 0;
for(i=0; i<1;i++)
for(int j=0;j<4;j++)
for(int k=0;k<4;k++)
MutiResult[i][j] += a[i][k]*b[k][j];
}
//******************************
//将坐标转化为矩阵形式(向量)
//******************************
void CTransGraphicsView::TransPointToMarix(ThPoint &tp,double a[1][4])
{
a[0][0] = tp.x;
a[0][1] = tp.y;
a[0][2] = tp.z;
a[0][3] = 1;
}
//******************************
//将矩阵(向量)转化为三维坐标形式
//******************************
void CTransGraphicsView::TransMarixToPoint(double a[1][4],ThPoint &tp)
{
tp.x = a[0][0];
tp.y = a[0][1];
tp.z = a[0][2];
}
//******************************
//立方体的平移算法
//******************************
void CTransGraphicsView::MoveTrans(double x,double y,double z)
{
double a[8][4],result[1][4];
double moveMarix[4][4] =
{
{1,0,0,0},{0,1,0,0},
{0,0,1,0},{x,y,z,1}
};
for(int i=0;i<8;i++)
{
TransPointToMarix(points[i],a+i);
MutiTransMarix(a+i,moveMarix,result);
TransMarixToPoint(result,points[i]);
}
ShadowTrans(-45);
}
//****************************************************
//立方体的斜交投影算法(将三维坐标转化为二维在屏幕显示)
//****************************************************
void CTransGraphicsView::ShadowTrans(int degree)
{
double a[8][4],result[1][4];
ThPoint pts[8];
for(int i=0;i<8;i++)
{
pts[i].x = points[i].x;
pts[i].y = points[i].y;
pts[i].z = points[i].z;
}
double shadowMarix[4][4] =
{
{1,0,0,0},{0,1,0,0},
{cos(degree),sin(degree),0,0},{0,0,0,1}
};
for(i=0;i<8;i++)
{
TransPointToMarix(pts[i],a+i);
MutiTransMarix(a+i,shadowMarix,result);
TransMarixToPoint(result,pts[i]);
}
for(i=0;i<8;i++)
{
cpoints[i].x = (int)pts[i].x;
cpoints[i].y = (int)pts[i].y;
}
}
//***************************************
//立方体旋转算法,旋转中心可以是x,y或z轴
//degree旋转的度数, rc旋转中心
//****************************************
void CTransGraphicsView::RotateTrans(double degree,CString rc)
{
double a[8][4],result[1][4];
int i = 0;
if(rc == "z")
{
double xRotateMarix[4][4] =
{
{cos(degree*3.14/180),sin(degree*3.14/180),0,0},
{-sin(degree*3.14/180),cos(degree*3.14/180),0,0},
{0,0,1,0},{0,0,0,1},
};
for(i=0;i<8;i++)
{
TransPointToMarix(points[i],a+i);
MutiTransMarix(a+i,xRotateMarix,result);
TransMarixToPoint(result,points[i]);
}
}
else if(rc == "x")
{
double yRotateMarix[4][4]=
{
{1,0,0,0},
{0,cos(degree*3.14/180),sin(degree*3.14/180),0},
{0,-sin(degree*3.14/180),cos(degree*3.14/180),0},
{0,0,0,1}
};
for(i=0;i<8;i++)
{
TransPointToMarix(points[i],a+i);
MutiTransMarix(a+i,yRotateMarix,result);
TransMarixToPoint(result,points[i]);
}
}
else if(rc == "y")
{
double zRotateMarix[4][4]=
{
{cos(degree*3.14/180),0,-sin(degree*3.14/180),0},
{0,1,0,0},
{sin(degree*3.14/180),0,cos(degree*3.14/180),0},
{0,0,0,1}
};
for(i=0;i<8;i++)
{
TransPointToMarix(points[i],a+i);
MutiTransMarix(a+i,zRotateMarix,result);
TransMarixToPoint(result,points[i]);
}
}
ShadowTrans(-45);
}
//*********************************
//立方体的比例变换算法
//*********************************
void CTransGraphicsView::BigerOrSmallerTrans(double timeX,double timeY,double timeZ)
{
double a[8][4],result[1][4];
double bsMarix[4][4] =
{
{timeX,0,0,0},{0,timeY,0,0},
{0,0,timeZ,0},{0,0,0,1}
};
for(int i=0;i<8;i++)
{
TransPointToMarix(points[i],a+i);
MutiTransMarix(a+i,bsMarix,result);
TransMarixToPoint(result,points[i]);
}
ShadowTrans(-45);
}
#endif //_DEBUG /////////////////////////////////////////////////////////////////////////////
// CTransGraphicsView message handlers
//********************************
//获取屏幕的大小
//********************************
void CTransGraphicsView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
cxClient = cx;
cyClient = cy;
}
//*****************************************
//根据对话框输入的参数进行比例变换
//*****************************************
void CTransGraphicsView::OnBgsm()
{
// TODO: Add your command handler code here
BGSMDialog B_dlg;
if(IDOK == B_dlg.DoModal())
{
BiggerOrSmallerTrans(B_dlg.m_sx,B_dlg.m_sy,B_dlg.m_sz);
}
Invalidate();
UpdateWindow();
}
//********************************
//根据对话框输入的参数进行平移变换
//********************************
void CTransGraphicsView::OnMove()
{
// TODO: Add your command handler code here
CMoveDialog M_dlg;
if(IDOK == M_dlg.DoModal())
{
MoveTrans(M_dlg.m_dx,M_dlg.m_dy,M_dlg.m_dz);
}
Invalidate();
UpdateWindow();
}
//********************************
//根据对话框输入的参数进行旋转变换
//********************************
void CTransGraphicsView::OnRotate()
{
// TODO: Add your command handler code here
RoateDialog BT_dlg;
if(IDOK == BT_dlg.DoModal())
{
RotateTrans(BT_dlg.m_degree,BT_dlg.m_rc);
}
Invalidate();
UpdateWindow();
}