?? testview.cpp
字號:
// TestView.cpp : implementation of the CTestView class
//
#include "stdafx.h"
#include "Test.h"
#include "TestDoc.h"
#include "TestView.h"
#define ROUND(a) int(a+0.5)//四舍五入
#define PI 3.1415926//圓周率
#include "math.h"//數學頭文件
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestView
IMPLEMENT_DYNCREATE(CTestView, CView)
BEGIN_MESSAGE_MAP(CTestView, CView)
//{{AFX_MSG_MAP(CTestView)
ON_COMMAND(ID_MENULeft, OnMENULeft)
ON_COMMAND(ID_MENU3D, OnMENU3D)
ON_COMMAND(ID_MENUFront, OnMENUFront)
ON_COMMAND(ID_MENUBack, OnMENUBack)
ON_COMMAND(ID_MENUright, OnMENUright)
ON_COMMAND(ID_MENUUp, OnMENUUp)
ON_COMMAND(ID_MENUDown, OnMENUDown)
ON_COMMAND(ID_MENUIncrease, OnMENUIncrease)
ON_COMMAND(ID_MENUDecrease, OnMENUDecrease)
ON_COMMAND(ID_MENUzrotate, OnMENUzrotate)
ON_COMMAND(ID_MENUxrotate, OnMENUxrotate)
ON_COMMAND(ID_MENUyrotate, OnMENUyrotate)
ON_COMMAND(ID_MENUReset, OnMENUReset)
ON_COMMAND(ID_MENUxoy, OnMENUxoy)
ON_COMMAND(ID_MENUyoz, OnMENUyoz)
ON_COMMAND(ID_MENUzox, OnMENUzox)
ON_COMMAND(ID_MENUXdirection, OnMENUXdirection)
ON_COMMAND(ID_MENUYdirection, OnMENUYdirection)
ON_COMMAND(ID_MENUZdirection, OnMENUZdirection)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestView construction/destruction
CTestView::CTestView()
{
// TODO: add construction code here
}
CTestView::~CTestView()
{
}
BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTestView drawing
void CTestView::OnDraw(CDC* pDC)//繪制坐標系
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
GetMaxX();GetMaxY();
CPen MyPen,*OldPen;
MyPen.CreatePen(PS_SOLID,1,RGB(0,0,255));
OldPen=pDC->SelectObject(&MyPen);
pDC->MoveTo(MaxX/2,MaxY/2);//繪制y軸
pDC->LineTo(MaxX,MaxY/2);
pDC->TextOut(MaxX-20,MaxY/2-30,"y");
pDC->MoveTo(MaxX/2,MaxY/2);//繪制z軸
pDC->LineTo(MaxX/2,0);
pDC->TextOut(MaxX/2-20,10,"z");
pDC->MoveTo(MaxX/2,MaxY/2);//繪制x軸
pDC->LineTo(MaxX/2-MaxY/2,MaxY);//夾角為135°
pDC->TextOut(MaxX/2-MaxY/2-20,MaxY-30,"x");
pDC->TextOut(MaxX/2-20,MaxY/2-20,"0");
pDC->SelectObject(OldPen);
MyPen.DeleteObject();
}
/////////////////////////////////////////////////////////////////////////////
// CTestView printing
BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTestView diagnostics
#ifdef _DEBUG
void CTestView::AssertValid() const
{
CView::AssertValid();
}
void CTestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTestDoc* CTestView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestView message handlers
void CTestView::GetMaxX()//獲得屏幕寬度
{
CRect Rect;
GetClientRect(&Rect);
MaxX=Rect.right;
}
void CTestView::GetMaxY()//獲得屏幕高度
{
CRect Rect;
GetClientRect(&Rect);
MaxY=Rect.bottom;
}
void CTestView::ReadCube()//讀入立方體頂點
{
//后面的三維坐標
int a=100;//a為立方體邊長
P3D[0][0]=0;P3D[0][1]=0;P3D[0][2]=0;P3D[0][3]=1;//A點(0,0,0)
P3D[1][0]=0;P3D[1][1]=a;P3D[1][2]=0;P3D[1][3]=1;//B點(0,a,0)
P3D[2][0]=0;P3D[2][1]=a;P3D[2][2]=a;P3D[2][3]=1;//C點(0,a,a)
P3D[3][0]=0;P3D[3][1]=0;P3D[3][2]=a;P3D[3][3]=1;//D點(0,0,a)
//前面的三維坐標
P3D[4][0]=a;P3D[4][1]=0;P3D[4][2]=0;P3D[4][3]=1;//E點(a,0,0)
P3D[5][0]=a;P3D[5][1]=a;P3D[5][2]=0;P3D[5][3]=1;//F點(a,a,0)
P3D[6][0]=a;P3D[6][1]=a;P3D[6][2]=a;P3D[6][3]=1;//G點(a,a,a)
P3D[7][0]=a;P3D[7][1]=0;P3D[7][2]=a;P3D[7][3]=1;//H點(a,0,a)
}
void CTestView::OnMENU3D()//繪制立方體
{
// TODO: Add your command handler code here
RedrawWindow();
ReadCube();
DrawCube();
}
void CTestView::DrawCube()//繪制立方體
{
CPoint p[4];//定義多邊形頂點數組
Transform3DTo2D(P3D,P2D,8);
//繪制立方體左面
p[0]=CPoint(ROUND(MaxX/2+P2D[0][0]),ROUND(MaxY/2+P2D[0][1]));
p[1]=CPoint(ROUND(MaxX/2+P2D[3][0]),ROUND(MaxY/2+P2D[3][1]));
p[2]=CPoint(ROUND(MaxX/2+P2D[7][0]),ROUND(MaxY/2+P2D[7][1]));
p[3]=CPoint(ROUND(MaxX/2+P2D[4][0]),ROUND(MaxY/2+P2D[4][1]));
Line(p);
//繪制立方體后面
p[0]=CPoint(ROUND(MaxX/2+P2D[0][0]),ROUND(MaxY/2+P2D[0][1]));
p[1]=CPoint(ROUND(MaxX/2+P2D[1][0]),ROUND(MaxY/2+P2D[1][1]));
p[2]=CPoint(ROUND(MaxX/2+P2D[2][0]),ROUND(MaxY/2+P2D[2][1]));
p[3]=CPoint(ROUND(MaxX/2+P2D[3][0]),ROUND(MaxY/2+P2D[3][1]));
Line(p);
//繪制立方體底面
p[0]=CPoint(ROUND(MaxX/2+P2D[0][0]),ROUND(MaxY/2+P2D[0][1]));
p[1]=CPoint(ROUND(MaxX/2+P2D[4][0]),ROUND(MaxY/2+P2D[4][1]));
p[2]=CPoint(ROUND(MaxX/2+P2D[5][0]),ROUND(MaxY/2+P2D[5][1]));
p[3]=CPoint(ROUND(MaxX/2+P2D[1][0]),ROUND(MaxY/2+P2D[1][1]));
Line(p);
//繪制立方體右面
p[0]=CPoint(ROUND(MaxX/2+P2D[1][0]),ROUND(MaxY/2+P2D[1][1]));
p[1]=CPoint(ROUND(MaxX/2+P2D[2][0]),ROUND(MaxY/2+P2D[2][1]));
p[2]=CPoint(ROUND(MaxX/2+P2D[6][0]),ROUND(MaxY/2+P2D[6][1]));
p[3]=CPoint(ROUND(MaxX/2+P2D[5][0]),ROUND(MaxY/2+P2D[5][1]));
Line(p);
//繪制立方體頂面
p[0]=CPoint(ROUND(MaxX/2+P2D[3][0]),ROUND(MaxY/2+P2D[3][1]));
p[1]=CPoint(ROUND(MaxX/2+P2D[7][0]),ROUND(MaxY/2+P2D[7][1]));
p[2]=CPoint(ROUND(MaxX/2+P2D[6][0]),ROUND(MaxY/2+P2D[6][1]));
p[3]=CPoint(ROUND(MaxX/2+P2D[2][0]),ROUND(MaxY/2+P2D[2][1]));
Line(p);
//繪制立方體前面
p[0]=CPoint(ROUND(MaxX/2+P2D[4][0]),ROUND(MaxY/2+P2D[4][1]));
p[1]=CPoint(ROUND(MaxX/2+P2D[5][0]),ROUND(MaxY/2+P2D[5][1]));
p[2]=CPoint(ROUND(MaxX/2+P2D[6][0]),ROUND(MaxY/2+P2D[6][1]));
p[3]=CPoint(ROUND(MaxX/2+P2D[7][0]),ROUND(MaxY/2+P2D[7][1]));
Line(p);
}
void CTestView::Line(CPoint p[])//繪制四邊形
{
CClientDC dc(this);
for (int i=0;i<4;i++)
if(i==0)
dc.MoveTo(p[0]);
else
dc.LineTo(p[i]);
dc.LineTo(p[0]);
}
void CTestView::Transform3DTo2D(const double P3D[8][4],double P2D[8][2],const int n)
{//三維坐標變換為二維坐標
for(int i=0;i<n;i++)
{
P2D[i][0]=P3D[i][1]-P3D[i][0]/sqrt(2);
P2D[i][1]=-P3D[i][2]+P3D[i][0]/sqrt(2);
}
}
void CTestView::ClearMatrix(double A[4][4])//清除變換矩陣
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
A[i][j]=0;
}
}
void CTestView::Calculate(double P0[][4],double T[][4])//兩個矩陣相乘
{
double Ptemp[8][4];
KeepOriginalMatrix(P3D,Ptemp);
for(int i=0;i<8;i++)
for(int j=0;j<4;j++)
P3D[i][j]=Ptemp[i][0]*T[0][j]+Ptemp[i][1]*T[1][j]+Ptemp[i][2]*T[2][j]+Ptemp[i][3]*T[3][j];
}
void CTestView::OnMENUFront()//向前平移
{
// TODO: Add your command handler code here
Tmove(10,0,0);
}
void CTestView::OnMENUBack()//向后平移
{
// TODO: Add your command handler code here
Tmove(-10,0,0);
}
void CTestView::OnMENULeft()//向左平移
{
// TODO: Add your command handler code here
Tmove(0,-10,0);
}
void CTestView::OnMENUright()//向右平移
{
// TODO: Add your command handler code here
Tmove(0,10,0);
}
void CTestView::OnMENUUp()//向上平移
{
// TODO: Add your command handler code here
Tmove(0,0,10);
}
void CTestView::OnMENUDown()//向下平移
{
// TODO: Add your command handler code here
Tmove(0,0,-10);
}
void CTestView::OnMENUIncrease()//比例放大
{
// TODO: Add your command handler code here
Tscale(2,2,2);
}
void CTestView::OnMENUDecrease()//比例縮小
{
// TODO: Add your command handler code here
Tscale(0.5,0.5,0.5);
}
void CTestView::OnMENUzrotate()//繞z旋轉
{
// TODO: Add your command handler code here
Tzrotate(30);
}
void CTestView::OnMENUxrotate()//繞x旋轉
{
// TODO: Add your command handler code here
Txrotate(30);
}
void CTestView::OnMENUyrotate()//繞y旋轉
{
// TODO: Add your command handler code here
Tyrotate(30);
}
void CTestView::OnMENUxoy()//XOY面反射矩陣
{
// TODO: Add your command handler code here
Treflect(1,1,-1);
}
void CTestView::OnMENUyoz()//YOZ面反射矩陣
{
// TODO: Add your command handler code here
Treflect(-1,1,1);
}
void CTestView::OnMENUzox()//ZOX面反射矩陣
{
// TODO: Add your command handler code here
Treflect(1,-1,1);
}
void CTestView::OnMENUXdirection()//x方向錯切
{
// TODO: Add your command handler code here
Treform(0,0,1,0,1,0);
}
void CTestView::OnMENUYdirection()//y方向錯切
{
// TODO: Add your command handler code here
Treform(1,0,0,0,0,1);
}
void CTestView::OnMENUZdirection()//z方向錯切
{
// TODO: Add your command handler code here
Treform(0,1,0,1,0,0);
}
void CTestView::Tmove(double Tx,double Ty,double Tz)//平移變換矩陣
{
ClearMatrix(TM);
RedrawWindow();
TM[0][0]=1;
TM[1][1]=1;
TM[2][2]=1;
TM[3][0]=Tx;
TM[3][1]=Ty;
TM[3][2]=Tz;
TM[3][3]=1;
Calculate(P3D,TM);
AfxGetMainWnd()->SetWindowText("案例13:三維圖形幾何變換-平移變換");
DrawCube();
}
void CTestView::Tscale(double Sx,double Sy,double Sz)//比例變換矩陣
{
ClearMatrix(TS);
RedrawWindow();
TS[0][0]=Sx;
TS[1][1]=Sy;
TS[2][2]=Sz;
TS[3][3]=1;
Calculate(P3D,TS);
AfxGetMainWnd()->SetWindowText("案例13:三維圖形幾何變換-比例變換");
DrawCube();
}
void CTestView::Tzrotate(double thta)//Z旋轉變換矩陣
{
ClearMatrix(TZR);
RedrawWindow();
TZR[0][0]=cos(thta*PI/180);
TZR[0][1]=sin(thta*PI/180);
TZR[1][0]=-sin(thta*PI/180);
TZR[1][1]=cos(thta*PI/180);
TZR[2][2]=1;
TZR[3][3]=1;
Calculate(P3D,TZR);
AfxGetMainWnd()->SetWindowText("案例13:三維圖形幾何變換-Z旋轉變換");
DrawCube();
}
void CTestView::Txrotate(double thta)//X旋轉變換矩陣
{
ClearMatrix(TXR);
RedrawWindow();
TXR[0][0]=1;
TXR[1][1]=cos(thta*PI/180);
TXR[1][2]=sin(thta*PI/180);
TXR[2][1]=-sin(thta*PI/180);
TXR[2][2]=cos(thta*PI/180);
TXR[3][3]=1;
Calculate(P3D,TXR);
AfxGetMainWnd()->SetWindowText("案例13:三維圖形幾何變換-X旋轉變換");
DrawCube();
}
void CTestView::Tyrotate(double thta)//Y旋轉變換矩陣
{
ClearMatrix(TYR);
RedrawWindow();
TYR[0][0]=cos(thta*PI/180);
TYR[0][2]=-sin(thta*PI/180);
TYR[1][1]=1;
TYR[2][0]=sin(thta*PI/180);
TYR[2][2]=cos(thta*PI/180);
TYR[3][3]=1;
Calculate(P3D,TYR);
AfxGetMainWnd()->SetWindowText("案例13:三維圖形幾何變換-Y旋轉變換");
DrawCube();
}
void CTestView::Treflect(double Fx,double Fy,double Fz)//反射變換矩陣
{
ClearMatrix(TF);
RedrawWindow();
TF[0][0]=Fx;
TF[1][1]=Fy;
TF[2][2]=Fz;
TF[3][3]=1;
Calculate(P3D,TF);
AfxGetMainWnd()->SetWindowText("案例13:三維圖形幾何變換-反射變換");
DrawCube();
}
void CTestView::Treform(double b,double c,double d,double f,double g,double h)//錯切變換矩陣
{
ClearMatrix(TC);
RedrawWindow();
TC[0][0]=1;
TC[0][1]=b;
TC[0][2]=c;
TC[1][0]=d;
TC[1][1]=1;
TC[1][3]=f;
TC[2][0]=g;
TC[2][1]=h;
TC[2][2]=1;
TC[3][3]=1;
Calculate(P3D,TC);
AfxGetMainWnd()->SetWindowText("案例13:三維圖形幾何變換-錯切變換");
DrawCube();
}
void CTestView::KeepOriginalMatrix(double Orig[8][4],double Dest[8][4])//保留矩陣
{
int i,j;
for(i=0;i<8;i++)
for(j=0;j<4;j++)
Dest[i][j]=Orig[i][j];
}
void CTestView::OnMENUReset()//復位
{
// TODO: Add your command handler code here
RedrawWindow();
AfxGetMainWnd()->SetWindowText("案例13");
OnMENU3D();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -