📄 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 + -