www.pudn.com > assigment1.zip > myGraphicsView.cpp


// myGraphicsView.cpp : implementation of the CMyGraphicsView class 
// 
 
#include "stdafx.h" 
#include "myGraphics.h" 
 
#include "myGraphicsDoc.h" 
#include "myGraphicsView.h" 
#include "graph.h" 
#include "PenColorDlg.h" 
#include  
#include "line_property.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyGraphicsView 
 
IMPLEMENT_DYNCREATE(CMyGraphicsView, CView) 
 
BEGIN_MESSAGE_MAP(CMyGraphicsView, CView) 
	//{{AFX_MSG_MAP(CMyGraphicsView) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_RBUTTONDOWN() 
	ON_WM_CREATE() 
	ON_WM_DESTROY() 
	ON_WM_SIZE() 
	ON_COMMAND(ID_BASIC_POLYGON, OnBasicPolygon) 
	ON_COMMAND(ID_BASIC_LINES, OnBasicLines) 
	ON_COMMAND(ID_MAGIC_CIRCLE, OnMagicCircle) 
	ON_WM_MOUSEMOVE() 
	ON_WM_LBUTTONUP() 
	ON_COMMAND(ID_FRACTALS_TREE, OnFractalsTree) 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyGraphicsView construction/destruction 
 
CMyGraphicsView::CMyGraphicsView() 
{ 
	m_pN = 0; 
	w = h = 0; 
	m_circle=0; 
	m_Flag=-1; 
	mPenColor = RGB(0,0,0); 
	    m_line_method=0; 
		m_line_width=0; 
        iRed=0; 
		iBlue=0; 
		iGreen=0; 
} 
 
CMyGraphicsView::~CMyGraphicsView() 
{ 
} 
 
BOOL CMyGraphicsView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	TRACE0("PreCreateWindow\r\n"); 
	// settings for OpenGL 
	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyGraphicsView drawing 
 
void CMyGraphicsView::OnDraw(CDC* pDC) 
{ 
	CMyGraphicsDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyGraphicsView printing 
 
BOOL CMyGraphicsView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMyGraphicsView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMyGraphicsView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyGraphicsView diagnostics 
 
#ifdef _DEBUG 
void CMyGraphicsView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CMyGraphicsView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CMyGraphicsDoc* CMyGraphicsView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyGraphicsDoc))); 
	return (CMyGraphicsDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMyGraphicsView message handlers 
 
void CMyGraphicsView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	CMyGraphicsDoc *pDoc = GetDocument(); 
	CGraph  dc(this); 
	CClientDC dc2(this); 
	CClientDC dc3(this); 
	CPen pen; 
//	HWND hWnd=GetSafeHwnd(); 
//	HDC hDC =::GetDC(hWnd);    
	int i; 
 
	if(pDoc->mFlag){ 
		pDoc->mFlag = FALSE; 
		m_pN = 0; 
	} 
	m_p[m_pN].x = point.x; 
	m_p[m_pN++].y = point.y; 
 
	switch(pDoc->mCurrent){ 
	case ID_BASIC_POLYGON: 
		if(m_pN>1){ 
			switch(m_line_method){ 
			case 0: 
			//使用selectpen,成功解决粗线问题 
					//CPen pen; 
				//if(m_pN==2)//初始化画笔!只能做一次!!! 
				//{ 
					pen.CreatePen(PS_SOLID,m_line_width,RGB(iRed,iGreen,iBlue)); 
				    dc2.SelectObject(pen); 
				//} 
					dc2.MoveTo(m_p[m_pN-2]); 
					dc2.LineTo(m_p[m_pN-1]); 
						 
			break; 
			case 1: 
			    for (i=0;imCurrent){ 
	case ID_BASIC_POLYGON: 
	 
		if(m_pN>1){ 
			switch(m_line_method) 
			{ 
			case 1: 
			dc.lineDDA(m_p[0].x, m_p[0].y,  
				m_p[m_pN-1].x, m_p[m_pN-1].y, RGB(iRed,iGreen,iBlue));  
			break; 
            case 2: 
 
					w1=w;h1=h; 
				   myinit(); 
				  // 	glPushMatrix(); 
	             //   glTranslatef(-0.5f, -0.7f, 0.0f); 
				   glLoadIdentity(); 
				   glLineWidth(m_line_width); //放大的尺寸 
				   r=(double)iRed/255;g=(double)iGreen/255;b=(double)iBlue/255; 
				   glColor3f(r,g,b);				   
	               glBegin(GL_LINE_STRIP); 
				   //失败原因:使用x,y整数坐标,无法在屏幕上正确显示 
				   for(i=0;imCurrent=ID_BASIC_POLYGON; 
} 
 
 
 
void CMyGraphicsView::OnBasicLines()  
{ 
	CClientDC dc(this); 
 	CMyGraphicsDoc *doc=GetDocument(); 
	doc->mCurrent=ID_BASIC_LINES; 
} 
 
void CMyGraphicsView::OnMagicCircle()  
{ 
    CMyGraphicsDoc *doc=GetDocument(); 
	doc->mCurrent=ID_MAGIC_CIRCLE;     
	CClientDC dc2(this); 
	dc2.TextOut(2,2,"Please click to draw a magic circle"); 
} 
 
void CMyGraphicsView::DrawMagicCircle() 
{					 
	//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
					   	HWND hWnd=GetSafeHwnd(); 
	                HDC hDC =::GetDC(hWnd);    
	                wglMakeCurrent(hDC, hglrc); 
 
	    double r,g,b,corner; 
		int i; 
		i=0; 
		corner=0; 
		glBegin(GL_POLYGON); 
        for (r=0.0;r<=1;r=r+0.05) 
			for (g=0.0;g<=1;g=g+0.05) 
				for (b=0.0;b<=1;b=b+0.05) 
				{ 
					switch((int)(corner/3.1415926*180)) 
					{ 
					case 0: 
                     glColor3f(0,0,1); 
					 glNormal3f(0,0,1); 
					 break; 
					case 60: 
						glColor3f(0,1,1); 
						glNormal3f(1,0,0); 
					 break; 
					 case 120: 
						glColor3f(0,1,0); 
						glNormal3f(0,-1,0); 
					 break; 
					 case 180: 
                     glColor3f(1,1,0); 
					 glNormal3f(0,0,-1); 
					 break; 
					case 240: 
						glColor3f(1,0,0); 
						glNormal3f(0,1,0); 
					 break; 
					 case 300: 
						glColor3f(1,0,1); 
						glNormal3f(-1,0,0); 
					 break; 
					} 
				//	 glVertex2f((100*sin(corner))/w*2,(100*cos(corner))*w/2); 
                     glVertex2f(sin(corner),cos(corner)); 
					 corner=corner+3.1415926/4000; 
					 //corner=corner+0.1; 
				} 
        glEnd(); 
		glFlush();   
		wglMakeCurrent(NULL, NULL); 
	                 SwapBuffers(hDC); 
} 
 
void CMyGraphicsView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	CMyGraphicsDoc *pDoc=GetDocument(); 
	CClientDC dc(this); 
//	SetCursor(m_HCursor); 
	dc.SetROP2(R2_NOT); 
	CPen pen2; 
	pen2.CreatePen(PS_INSIDEFRAME ,1,RGB(iRed,iGreen,iBlue)); 
		dc.SelectObject(pen2); 
	switch(pDoc->mCurrent) 
	{ 
	case ID_BASIC_LINES: 
//linedda fail to functoin !!!,use flag to have a good draw 
		if(m_Flag==1) 
		{ 
			//dc.LineTo( m_pPrev.x, m_pPrev.y); 
			//dc.LineTo(m_p[0].x, m_p[0].y, point.x, point.y,RGB(1,0,0)); 
			//dc.LineTo( point.x, point.y); 
			dc.Ellipse(m_p[0].x, m_p[0].y, m_pPrev.x, m_pPrev.y); 
			dc.Ellipse(m_p[0].x, m_p[0].y, point.x, point.y); 
		} 
		else 
		if(m_Flag==0) 
			//the first point 
		{ 
            dc.Ellipse(m_p[0].x, m_p[0].y, m_p[0].x, m_p[0].y); 
			m_Flag=1; 
		} 
			break; 
	} 
		m_pPrev=point; 
 
	CView::OnMouseMove(nFlags, point); 
} 
 
void CMyGraphicsView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
		CMyGraphicsDoc *pDoc=GetDocument(); 
	switch(pDoc->mCurrent) 
	{ 
	case ID_BASIC_LINES: 
	m_Flag=-1; 
    m_pN=0; 
	} 
	CView::OnLButtonUp(nFlags, point); 
} 
 
void CMyGraphicsView::OnFractalsTree()  
{ 
	// TODO: Add your command handler code here 
	OneTree(300,40,300,410); 
} 
 
void SingleTree(int x1,int y1,int x2,int y2) 
{ 
	; 
} 
 
void CMyGraphicsView::OneTree(double x1, double y1, double x2, double y2) 
{ 
    double xa,ya,xb,yb,xc,yc,xd,yd,a1,a2,vx,vy,factor; 
	//   1 
	// d b 
	//   a c 
	//   2 
	//a1:the angle for the sub trees 
	a1=3.14/5.5; 
	a2=0-a1; 
	//the value to control when to termine 
	factor=0.15; 
	xa=(x1-x2)/3+x2;ya=(y1-y2)/3+y2; 
	xb=(x1-x2)/3*2+x2;yb=(y1-y2)/3*2+y2; 
 
	//count the right tree c 
	vx=xb-xa;vy=yb-ya; 
	//rotate 
	xc=cos(a2)*vx+sin(a2)*vy+xa; 
	yc=-1*sin(a2)*vx+cos(a2)*vy+ya; 
 
    //count the left tree d 
    vx=x1-xb;vy=y1-yb; 
	//rotate 
	xd=cos(a1)*vx+sin(a1)*vy+xb; 
	yd=-1*sin(a1)*vx+cos(a1)*vy+yb; 
 
	CGraph dc(this); 
	dc.lineDDA(x1,y1,x2,y2,RGB(1,0,0)); 
	dc.lineDDA(xb,yb,xd,yd,RGB(1,0,0)); 
	dc.lineDDA(xa,ya,xc,yc,RGB(1,0,0)); 
    if(abs(x1-xb)>factor ||abs(y1-yb)>factor) 
		OneTree(x1,y1,xb,yb); 
    if(abs(xa-xb)>factor ||abs(ya-yb)>factor) 
		OneTree(xb,yb,xa,ya); 
    if(abs(xd-xb)>factor ||abs(yd-yb)>factor) 
		OneTree(xd,yd,xb,yb); 
	if(abs(xc-xa)>factor ||abs(yc-ya)>factor) 
		OneTree(xc,yc,xa,ya); 
	if(abs(x2-xa)>factor ||abs(y2-ya)>factor) 
		OneTree(xa,ya,x2,y2); 
}