//3DCube.cpp
#include <afxwin.h>
#include <math.h>
#include "3DCube.h"
#define PI 3.1415926
#define INCREMENT PI/30
CMyApp myApp;
/////////////////////////////////////////////////////////////////////////
// CMyApp member functions
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_CREATE ()
ON_WM_MOUSEMOVE ()
ON_WM_MOUSEWHEEL ()
ON_WM_PAINT ()
END_MESSAGE_MAP ()
CMainWindow::CMainWindow ()
{
Create (NULL, _T ("三维立方体"), WS_OVERLAPPEDWINDOW&~WS_SIZEBOX&~WS_MAXIMIZEBOX);
}
int CMainWindow::OnCreate (LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate (lpCreateStruct) == -1)
return -1;
//
// Initialize datas of the 3D cube.
InitPoints ();
InitLines ();
//
// Initialize theta.
theta_z = PI/3;
theta = PI/5;
//
// initialize the distance between cube and eyes.
nDistance = 500;
//
// Shift points of the 3D cube to view.
ShiftToView ();
//
// Shift points of the 3D cube to screen.
ShiftToScreen ();
return 0;
}
void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
{
//
static CPoint last_point;
if (last_point.x>point.x)
{
theta += INCREMENT;
}
else if (last_point.x<point.x)
{
theta -= INCREMENT;
}
if (last_point.y>point.y)
{
theta_z -= INCREMENT;
}
else if (last_point.y<point.y)
{
theta_z += INCREMENT;
}
last_point = point;
CRect rect;
GetClientRect (&rect);
CClientDC dc (this);
dc.SetViewportOrg (rect.Width ()/2, rect.Height ()/2);
//
// Draw new cube.
DrawNew (&dc);
}
BOOL CMainWindow::OnMouseWheel (UINT nFlags, short zDelta, CPoint point)
{
//
int dis = 30;
if (zDelta < 0)
{
dis = -30;
}
nDistance += dis;
if (nDistance < 150)
{
nDistance = 150;
}
else if (nDistance > 1000)
{
nDistance = 1000;
}
CRect rect;
GetClientRect (&rect);
CClientDC dc (this);
dc.SetViewportOrg (rect.Width ()/2, rect.Height ()/2);
//
// Draw new cube.
DrawNew (&dc);
return TRUE;
}
void CMainWindow::OnPaint ()
{
CRect rect;
GetClientRect (&rect);
CPaintDC dc (this);
dc.SetViewportOrg (rect.Width ()/2, rect.Height ()/2);
//
// Draw the cube.
DrawCube (&dc);
}
void CMainWindow::InitPoints ()
{
//
// initialize the points of 3D cube.
n3DPoint[0].SetPoint (-50, -50, -50);
n3DPoint[1].SetPoint (-50, 50, -50);
n3DPoint[2].SetPoint ( 50, 50, -50);
n3DPoint[3].SetPoint ( 50, -50, -50);
n3DPoint[4].SetPoint (-50, -50, 50);
n3DPoint[5].SetPoint (-50, 50, 50);
n3DPoint[6].SetPoint ( 50, 50, 50);
n3DPoint[7].SetPoint ( 50, -50, 50);
}
void CMainWindow::InitLines ()
{
//
// initialize the lines of 3D cube.
nLine[0][0] = 0;nLine[0][1] = 1;
nLine[1][0] = 1;nLine[1][1] = 2;
nLine[2][0] = 2;nLine[2][1] = 3;
nLine[3][0] = 3;nLine[3][1] = 0;
nLine[4][0] = 4;nLine[4][1] = 5;
nLine[5][0] = 5;nLine[5][1] = 6;
nLine[6][0] = 6;nLine[6][1] = 7;
nLine[7][0] = 7;nLine[7][1] = 4;
nLine[8][0] = 0;nLine[8][1] = 4;
nLine[9][0] = 1;nLine[9][1] = 5;
nLine[10][0] = 2;nLine[10][1] = 6;
nLine[11][0] = 3;nLine[11][1] = 7;
}
void CMainWindow::ShiftToView ()
{
int length_x, length_y, length_z;
double temp;
length_z = (int) (nDistance*cos (theta_z));
temp = nDistance*sin (theta_z);
length_x = (int) (temp*cos (theta));
length_y = (int) (temp*sin (theta));
//
// Shift points of the 3D cube to view.
for (int i=0; i<NUM_POINTS; i++)
{
n3DPoint[i].ShiftToView (length_x, length_y, length_z);
}
}
void CMainWindow::ShiftToScreen ()
{
//
// Shift points of the 3D cube to screen.
for (int i=0; i<NUM_POINTS; i++)
{
n3DPoint[i].ShiftToScreen (400);
}
}
void CMainWindow::CleanCube (CDC *pDC)
{
pDC->SelectStockObject (WHITE_PEN);
//
// Draw the lines of the cube.
for (int i=0; i<NUM_LINES; i++)
{
pDC->MoveTo (n3DPoint[nLine[i][0]].GetPoint ());
pDC->LineTo (n3DPoint[nLine[i][1]].GetPoint ());
}
pDC->SelectStockObject (BLACK_PEN);
}
void CMainWindow::DrawCube (CDC *pDC)
{
//
// Draw the lines of the cube.
for (int i=0; i<NUM_LINES; i++)
{
pDC->MoveTo (n3DPoint[nLine[i][0]].GetPoint ());
pDC->LineTo (n3DPoint[nLine[i][1]].GetPoint ());
}
}
void CMainWindow::DrawNew (CDC *pDC)
{
//
// Clean the last cube
CleanCube (pDC);
//
// Shift points of the 3D cube to view.
ShiftToView ();
//
// Shift points of the 3D cube to screen.
ShiftToScreen ();
//
// Draw the cube.
DrawCube (pDC);
}
旋转的三维方块(计算机图形学)
4星 · 超过85%的资源 28 浏览量
2007-12-01
15:13:28
上传
评论
收藏 17KB RAR 举报
脱离语言
- 粉丝: 350
- 资源: 50
最新资源
- 312749069629470selfieU重绘.apk
- http%3A%2F%2Fimg.wsdl.vivo.com.cn%2Fappstore%2Fdeveloper%2Ficon%2F201412%2F201412231038336
- 主要记录B站up主莫烦matplotlib教程中的代码.zip
- Java SE Development Kit 8u411 Windows x64 Installer
- Java SE Development Kit 11.0.23 Windows x64 Installer
- 毕业设计-后端项目-网站民宿展示网站SSM
- 基于python绘图,程序详细展示了绘图过程中详尽的命令语句,使得图片达到可发表程度 .zip
- Java SE Development Kit 11.0.23 macOS x64 DMG Installer
- Java SE Development Kit 11.0.23 macOS ARM64 DMG Installer
- python matplotlib 可视化编程.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈