如何用计算机画立方体,画立方体-计算机图形学实验.doc

a7f4a3f590493a1e451dd952a488fd7c.gif 画立方体-计算机图形学实验.doc

(8页)

b7bef445891b023236dd80b9d2cba5b2.gif

本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦!

9.90 积分

计算机图形学实验——画立方体(通过视向变换等)具体实现如下(仅供参考)一、添加一个头文件Data.hData文件包含立方体数据:具体代码:double Vertex[8][3]={ {0,20,0},{30,20,0},{30,20,15},{0,20,15}, {0,0,0},{30,0,0},{30,0,15},{0,0,15}};int Edge[12][2]={ {0,1},{1,2},{2,3},{3,0}, {4,5},{5,6},{6,7},{7,4}, {0,4},{1,5},{2,6},{3,7}};double Vertex1[8][3];//视点double Eye[3]={20.0f,30.0f,56.1f};//窗口数据//int Window[4]={-30,5,-15,3};int Window[4]={-20,-15,-2,-20};//视区数据int ViewPort[4]={200,250,180,15};二、View类头文件6_Draw cubeView.h // 6_Draw cubeView.h : interface of the CMy6_DrawcubeView class///#if !defined(AFX_6_DRAWCUBEVIEW_H__940E4DA8_261A_46F3_82C2_259B9B805817__INCLUDED_)#define AFX_6_DRAWCUBEVIEW_H__940E4DA8_261A_46F3_82C2_259B9B805817__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CMy6_DrawcubeView : public CView{protected: // create from serialization only CMy6_DrawcubeView(); DECLARE_DYNCREATE(CMy6_DrawcubeView)// Attributespublic: CMy6_DrawcubeDoc* GetDocument();bool draw;void drawCube();// Operationspublic:// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMy6_DrawcubeView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); //}}AFX_VIRTUAL// Implementationpublic: virtual ~CMy6_DrawcubeView();#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;#endifprotected:// Generated message map functionsprotected: //{{AFX_MSG(CMy6_DrawcubeView) afx_msg void Ondrawcube(); afx_msg void OnUpdatedrawcube(CCmdUI* pCmdUI); //}}AFX_MSG DECLARE_MESSAGE_MAP()};#ifndef _DEBUG // debug version in 6_Draw cubeView.cppinline CMy6_DrawcubeDoc* CMy6_DrawcubeView::GetDocument() { return (CMy6_DrawcubeDoc*)m_pDocument; }#endif///{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_6_DRAWCUBEVIEW_H__940E4DA8_261A_46F3_82C2_259B9B805817__INCLUDED_)三、View 类实现代码:6_Draw cubeView.cpp代码:// 6_Draw cubeView.cpp : implementation of the CMy6_DrawcubeView class//#include "stdafx.h"#include "6_Draw cube.h"#include "data.h"#include #include "6_Draw cubeDoc.h"#include "6_Draw cubeView.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/// CMy6_DrawcubeViewIMPLEMENT_DYNCREATE(CMy6_DrawcubeView, CView)BEGIN_MESSAGE_MAP(CMy6_DrawcubeView, CView) //{{AFX_MSG_MAP(CMy6_Dra。省略部分。 CMy6_DrawcubeView drawingvoid CMy6_DrawcubeView::OnDraw(CDC* pDC){ CMy6_DrawcubeDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CClientDC dc(this); dc.TextOut(10,10,"计算计图形学作业六:画立方体"); dc.TextOut(10,30,"姓名:晓晓"); dc.TextOut(10,50,"学号:200812301004"); if(draw) { //dc.TextOut(100,100,"fdsfsdf"); drawCube(); } // TODO: add draw code for native data here}/// CMy6_DrawcubeView printingBOOL CMy6_DrawcubeView::OnPreparePrinting(CPrintInfo* pInfo){ // default preparation return DoPreparePrinting(pInfo);}void CMy6_DrawcubeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/){ // TODO: add extra initialization before printing}void CMy6_DrawcubeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/){ // TODO: add cleanup after printing}/// CMy6_DrawcubeView diagnostics#ifdef _DEBUGvoid CMy6_DrawcubeView::AssertValid() const{ CView::AssertValid();}void CMy6_DrawcubeView::Dump(CDumpContext& dc) const{ CView::Dump(dc);}CMy6_DrawcubeDoc* CMy6_DrawcubeView::GetDocument() // non-debug version is inline{ ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy6_DrawcubeDoc))); return (CMy6_DrawcubeDoc*)m_pDocument;}#endif //_DEBUG/// CMy6_DrawcubeView message handlersvoid CMy6_DrawcubeView::Ondrawcube() { // TODO: Add your command handler code here draw=!draw; CMy6_DrawcubeDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDoc->UpdateAllViews(NULL);}void CMy6_DrawcubeView::OnUpdatedrawcube(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here if(draw) { pCmdUI->SetCheck(1); } else {pCmdUI->SetCheck(0);}}void CMy6_DrawcubeView::drawCube(){ //读入模型 //定义视点 //视向变换 double V[8][3]; int i; double a=sqrt(Eye[0]*Eye[0]+Eye[1]*Eye[1]), b=sqrt(Eye[0]*Eye[0]+Eye[1]*Eye[1]+Eye[2]*Eye[2]); for(i=0;i<8;i++) { V[i][0]= Vertex[i][0]*(-Eye[1]/a) + Vertex[i][1]*(Eye[0]/a) ; V[i][1]= Vertex[i][0]*(-Eye[0]*Eye[2]/(a*b)) + Vertex[i][1]*(Eye[1]*Eye[2]/(a*b)) + Vertex[i][2]*a/b; V[i][2]= Vertex[i][0]*(-Eye[0]/b) + Vertex[i][1]*(-Eye[1]/b) + Vertex[i][2]*(-Eye[2]/b); } //投影到XY平面 //窗口视区变换 double a1=(ViewPort[1]-ViewPort[0])/(Window[1]-Window[0]); double b1=ViewPort[0]-a1*Window[0]; double c1=(ViewPort[3]-ViewPort[2])/(Window[3]-Window[2]); double d1=ViewPort[2]-c1*Window[2]; for(i=0;i<8;i++) { V[i][0]=a1*V[i][0]+b1; V[i][1]=c1*V[i][1]+d1; } //画出投影图 CClientDC dc(this); for(i=0;i<12;i++) { dc.MoveTo((int)V[Edge[i][0]][0],(int)V[Edge[i][0]][1]); dc.LineTo((int)V[Edge[i][1]][0],(int)V[Edge[i][1]][1]); }}三、效果图 关 键 词: 实验 立方体 图形 计算机

4d91c43bfc72ca913299809b07b4968f.gif  天天文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值