⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphicview.cpp

📁 计算机图形学课程设计源码 这是大二的时候学计算机图形学时做的课程设计
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{	
	if((m_bLine||m_bSircle)&&(nFlags&&MK_LBUTTON)){
		int nDrawmode=m_pDC->GetROP2();
		m_pDC->SetROP2(R2_NOT);
		m_pDC->MoveTo(m_StartPoint);
		m_pDC->LineTo(m_LastEndPoint);
		m_pDC->SetROP2(nDrawmode);
		m_pDC->MoveTo(m_StartPoint);
		m_pDC->LineTo(point);
		m_LastEndPoint=point;
	}//if line
	/**/
	if(m_bFreehand&&(nFlags&&MK_LBUTTON)){
		lineany(m_StartPoint.x,m_StartPoint.y,point.x,point.y);	
		m_StartPoint=point;
	}//if
	CView::OnMouseMove(nFlags, point);
}

void CGraphicView::OnCancelMode() 
{
	CView::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}
void CGraphicView::circlePlotPoints(int xCenter,int yCenter,int x,int y){
			setPixel(xCenter+x,yCenter+y);
			setPixel(xCenter-x,yCenter+y);
			setPixel(xCenter+x,yCenter-y);
			setPixel(xCenter-x,yCenter-y);
			setPixel(xCenter+y,yCenter+x);
			setPixel(xCenter-y,yCenter+x);
			setPixel(xCenter+y,yCenter-x);
			setPixel(xCenter-y,yCenter-x);
	}//circlePlotPoints

void CGraphicView::OnMenuArea() 
{
	 m_bLine  =false;
	 m_bBezier=false;
	 m_bSircle=false;
	 m_bFreehand=false;
	 m_bArea  =true;

	 while(TemPoint){
		pEnd=TemPoint;
		TemPoint=TemPoint->next;
		delete pEnd;
	 }//while()
	 pEnd=NULL;

}

void CGraphicView::OnMenuBezier() 
{
	 m_bLine  =false;
	 m_bBezier=true;
	 m_bSircle=false;
	 m_bArea  =false;
	 m_bFreehand=false;
	 cnt=0;
	 while(TemPoint){
		dcpt *p=TemPoint;
		TemPoint=TemPoint->next;
		delete p;
	 }//while()
	 pEnd=NULL;

	
}

void CGraphicView::OnMenuSircle() 
{
	 m_bLine  =false;
	 m_bBezier=false;
	 m_bSircle=true;
	 m_bFreehand=false;
     m_bArea  =false;
	 m_ForeColor=RGB(0,250,120);
	
}

bool CGraphicView::lineany()
{	if(!TemPoint)return false;
		dcpt *p=TemPoint;
	while(p->next){
		lineany(p->point.x,p->point.y,p->next->point.x,p->next->point.y);	
		p=p->next;
	}//while
	lineany(p->point.x,p->point.y,TemPoint->point.x,TemPoint->point.y);
	/**/
	return true;
}

void CGraphicView::OnMenuFreehand() 
{
	 m_bLine  =false;
	 m_bBezier=false;
	 m_bSircle=false;
	 m_bArea  =false;
	 m_bFreehand=true;
	 m_ForeColor=RGB(255,200,0);
	
}


void CGraphicView::OnMenuLine() 
{	 m_ForeColor=RGB(80,150,250);
	 m_bLine  =true;
	 m_bBezier=false;
	 m_bSircle=false;
	 m_bArea  =false;
	 m_bFreehand=false;

}

void CGraphicView::circleMidPoint(CPoint sta, CPoint end)
{
	int r=(int)sqrt((sta.x-end.x)*(sta.x-end.x)+(sta.y-end.y)*(sta.y-end.y));
	circleMidPoint(sta.x,sta.y,r);
	
}

void CGraphicView::OnBUTTONBezier() 
{	m_ForeColor=RGB(0,150,250);
	 int i,max;
	CPoint curve[300],pts[4];
	pts[0].x=100;pts[0].y=100;
	pts[1].x=300;pts[1].y=300;
	pts[2].x=400;pts[2].y=200;
	pts[3].x=250;pts[3].y=10;
	for(i=0;i<3;i++){
		max=abs(pts[i].x-pts[i+1].x);
		if(abs(pts[i].y-pts[i+1].y)>max)
		max=abs(pts[i].y-pts[i+1].y);}
		bezier(pts,4,max,curve);
		if(cntB%2==0){
			for(i=0;i<max;i++)
				setPixel(curve[i].x,curve[i].y);
		cntB=1;
	}//if
	else{
		COLORREF color=m_ForeColor;
		m_ForeColor=RGB(250,250,250);
		for(i=0;i<max;i++)
			setPixel(curve[i].x,curve[i].y);
		m_ForeColor=color;
		cntB=0;
	}//else
		
}


void CGraphicView::OnBUTTONClear() 
{	 m_ForeColor=RGB(0,0,0);
 	 m_bLine  =false;	m_bBezier=false; m_bSircle=false;	 
	 m_bFreehand=false; m_bArea=false;	 m_bFill=false;	 bn=false;
	 this->Invalidate();
}//::OnBUTTONClear() 

void CGraphicView::OnBUTTONLine() 
{	bn=false;
	m_ForeColor=RGB(125,250,0);
	if(cntL%2==0){	lineany(450,10,80,250);	cntL=1;	}//if
	else{	COLORREF color=m_ForeColor;	m_ForeColor=RGB(250,250,250);
		lineany(450,10,80,250);	m_ForeColor=color;
		cntL=0;	}//else
}//::OnBUTTONLine() 

void CGraphicView::OnBUTTONSircle() 
{	bn=false;
	m_ForeColor=RGB(255,0,250);
	CPoint sta,end;
	sta.x=250;	sta.y=150;
	end.x=350;	end.y=200;
	if(cntS%2==0){	circleMidPoint(sta,end);  cntS=1;}//if
	else {	COLORREF color=m_ForeColor;
		m_ForeColor=RGB(220,250,250);
		circleMidPoint(sta,end);
		m_ForeColor=color;
		cntS=0;		}//else	
}//::OnBUTTONSircle()

void CGraphicView::OnBUTTONSphere() 
{    bn=false;	 m_bSeeable	=false;	 m_bSphere	=true;	
	 m_bUp	=false;	 m_bDown=false;	 m_bAnydrect=false;
	 if(!cntQ) {m_ForeColor=RGB(100,200,150); cntQ=1;}
	 else {m_ForeColor=RGB(230,220,250); cntQ=0;}
	viewpoint.x=200.0;	viewpoint.y=200.0;	viewpoint.z=100.0;
	dengfen=28;
//	AfxMessageBox(text,MB_OK,0);
	sphereview();	
}//::OnBUTTONSphere() 

void CGraphicView::OnRButtonUp(UINT nFlags, CPoint point) 
{	if(m_bFill){
		dcpt *tem=TemPoint;
		while(tem){ tem=tem->next;	cnt++;	}
		CPoint *pts=new CPoint[cnt];
		tem=TemPoint;
		for(int i=0;i<cnt;i++){
			pts[i].x=tem->point.x;
			pts[i].y=tem->point.y;
			tem=tem->next;
		}//for
		scanFill(cnt,pts);
		delete []pts;
	 cnt=0;
}//if fill

	if(m_bArea){ m_ForeColor=RGB(100,25,250); lineany(); }//if area
	if(m_bBezier){
		m_ForeColor=RGB(0,125,225);
		drawcurve(TemPoint);
		m_ForeColor=RGB(220,250,250);
		lineany();
		cnt=0;
	}//if bezier
	m_ForeColor=RGB(100,150,250);
	while(TemPoint){
			dcpt *p=TemPoint;
			TemPoint=TemPoint->next;
			delete p;	}//while()
	pEnd=NULL;
	/**/
	CView::OnRButtonUp(nFlags, point);
}

void CGraphicView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CView::OnRButtonDown(nFlags, point);
}

void CGraphicView::drawcurve(dcpt *dcpt1)
{		
		CPoint curve[300],pts[4];
		if(!TemPoint) return;
			pts[0]=TemPoint->point;
		if(!TemPoint->next) return;
			pts[1]=TemPoint->next->point;
		if(!TemPoint->next->next) return;
			pts[2]=TemPoint->next->next->point;
		if(!TemPoint->next->next->next) return;
			pts[3]=TemPoint->next->next->next->point;
		int i,max;
		for(i=0;i<3;i++){
			max=abs(pts[i].x-pts[i+1].x);
			if(abs(pts[i].y-pts[i+1].y)>max)
				max=abs(pts[i].y-pts[i+1].y);
		}//for1
		bezier(pts,4,max,curve);
		for(i=0;i<max;i++)	setPixel(curve[i].x,curve[i].y);
	 
}

void CGraphicView::OnButtonSeeable() 
{	 bn=false;
	 m_ForeColor=RGB(0,200,100);
	 m_bSeeable	=true;
	 m_bSphere	=false;
	 m_bUp		=false;
	 m_bDown	=false;
	 m_bAnydrect=false;
	 viewpoint.x=500.0;	
	 viewpoint.y=500.0;	
	 viewpoint.z=0.0;
	 dengfen=28;

	 sphereview();

}

void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	m_StartPoint=point;
	m_LastEndPoint=point;
	SetCapture();
	
	CView::OnLButtonDown(nFlags, point);
}

void CGraphicView::OnButtonSunlight() 
{
	bn=false;	
}


void CGraphicView::clearwindow()
{
	for(int i=200;i<=400;i++)
		{
			for(int j=100;j<=300;j++){m_pDC->SetPixel(i,j,RGB(255,255,255));}
		}//for

}

void CGraphicView::OnButtonRight() 
{	bn=false;
	if(m_bSphere||m_bSeeable){
//		double cos30=0.8660254,sin30=0.5;
		double cos30=0.70710678118654752440084436210485,sin30=0.70710678118654752440084436210485;
		dcpt3 tem;
		tem.x=viewpoint.x;tem.y=viewpoint.y;tem.z=viewpoint.z;
		viewpoint.x=tem.x*cos30-tem.y*sin30;
		viewpoint.y=tem.x*sin30+tem.y*cos30;
		viewpoint.z=500;
		clearwindow();
		sphereview();

	}//if
	else AfxMessageBox("请现按“球”或“消隐”画出球!",MB_OK,0);
}
void CGraphicView::OnButtonUp() 
{
	if(m_bSphere||m_bSeeable){
		cosA=0.8660254;	sinA=0.5;
		sishu2=sqrt(double(viewpoint.x*viewpoint.x+viewpoint.y*viewpoint.y));
		double r ;
		if(viewpoint.y<=0)r=sishu2;
		else r=-sishu2;
		sishu1=cosA-((double)viewpoint.z*sinA/r);
		dcpt3 tem;
		tem.x=viewpoint.x*1.0;
		tem.y=viewpoint.y*1.0;
		tem.z=viewpoint.z*1.0;
		viewpoint.x=tem.x*sishu1;
		viewpoint.y=tem.y*sishu1;
		viewpoint.z=tem.z*cosA+r*sinA;
		clearwindow();
		sphereview();

	}//if
	else AfxMessageBox("请现按“球”或“消隐”画出球!",MB_OK,0);
	
}

void CGraphicView::setPixmask(int x, int y)
{
	m_lineMask=m_fillMask[m_ky];
	 while(TemPoint){
		dcpt *p=TemPoint;
		TemPoint=TemPoint->next;
		delete p;
	 }//while()
	 pEnd=NULL;
	
	if(m_lineMask&(1<<(m_maskwidth-1)-m_k%m_maskwidth)) 
			 m_pDC->SetPixel(x,y,m_ForeColor);
		else m_pDC->SetPixel(x,y,m_BackColor);
		m_k++;

}

void CGraphicView::OnFileFill() 
{	 m_bLine  =false; m_bBezier=false; m_bSircle=false; m_bFreehand=false;
	 m_bArea  =true; m_bFill  =true;
	 while(TemPoint){
		dcpt *p=TemPoint;
		TemPoint=TemPoint->next;
		delete p;
	 }//while()
	pEnd=NULL;
	for(int i=0;i<8;i++){m_fillMask[i]=0xff00;}
	while(i<16){m_fillMask[i]=0x00ff;i++;}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -