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

📄 图形学实验dlg.cpp

📁 按作业效益非增序输入作业的截止期限
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				CircleDDA(mypoint2.x,mypoint2.y,r,RGB(0,0,255));
				break;			
			}
		default:break;
		}
	}
	
	CDialog::OnMouseMove(nFlags, point);
}


void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	//设置光标形状
	::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR2));
	
	CClientDC dc(this);
		switch(state)
		{
		case 1:
			{
				//画点
				dc.SetPixelV(point,RGB(0,0,255));
				break;
			}
		case 2:
			{
				//画小方块
				int adjusty = point.y-10;
				if (point.y-10<30)
				{
					adjusty = 30;
				}
				CRect myrect(point.x-10,adjusty,point.x+10,point.y+10);
				CBrush mybrush;
				mybrush.CreateSolidBrush(RGB(0,0,255));
				dc.FillRect(myrect,&mybrush);
				break;
			}
		case 3:
			{
				//画大方块
				int adjusty = point.y-20;
				if (point.y-20<30)
				{
					adjusty = 30;
				}
				CRect myrect(point.x-20,adjusty,point.x+20,point.y+20);
				CBrush mybrush;
				mybrush.CreateSolidBrush(RGB(0,0,255));
				dc.FillRect(myrect,&mybrush);
				break;
			}
		case 4:
			{
				//画圆
				int adjusty = point.y-10;
				if (point.y-10<30)
				{
					adjusty = 30;
				}
				CBrush mybrush;
				mybrush.CreateSolidBrush(RGB(0,0,255));
				CRgn myrgn;
				myrgn.CreateEllipticRgn(point.x-10,adjusty,point.x+10,point.y+10);
				dc.FillRgn(&myrgn,&mybrush);
				break;
			}
		case 5:
			{
				//初始化线的起点,终点
				pointBegin = point;
				pointEnd = point;
				break;
			}
		case 6:
			{
				//初始化圆心位置
				CircleBeginPoint = point;
				CircleEndPoint = point;
				break;
			}
		default:break;
		}
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CMyDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	//设置光标形状
	::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR2));

	CClientDC dc(this);
		switch(state)
		{
		case 1:
			{
				//画点
				dc.SetPixelV(point,RGB(0,0,255));
				break;
			}
		case 2:
			{
				//画小方块
				int adjusty = point.y-10;
				if (point.y-10<30)
				{
					adjusty = 30;
				}
				
				CRect myrect(point.x-10,adjusty,point.x+10,point.y+10);
				CBrush mybrush;
				mybrush.CreateSolidBrush(RGB(0,0,255));
				dc.FillRect(myrect,&mybrush);
				break;
			}
		case 3:
			{
				//画大方块
				int adjusty = point.y-20;
				if (point.y-20<30)
				{
					adjusty = 30;
				}
				CRect myrect(point.x-20,adjusty,point.x+20,point.y+20);
				CBrush mybrush;
				mybrush.CreateSolidBrush(RGB(0,0,255));
				dc.FillRect(myrect,&mybrush);
				break;
			}
		case 4:
			{
				//画圆
				int adjusty = point.y-10;
				if (point.y-10<30)
				{
					adjusty = 30;
				}
				CBrush mybrush;
				mybrush.CreateSolidBrush(RGB(0,0,255));
				CRgn myrgn;
				myrgn.CreateEllipticRgn(point.x-10,adjusty,point.x+10,point.y+10);
				dc.FillRgn(&myrgn,&mybrush);
				break;
			}
		case 5:
			{
				//确定线的终点,以其与左键按下时获得的起点为两端DDA法画线
				pointEnd = point;
				lineDDA(pointBegin.x,pointBegin.y,pointEnd.x,pointEnd.y,RGB(0,0,255));

				//下面是保存当前屏幕点的信息,供mousemove消息处理函数用来恢复屏幕
				int dx = pointEnd.x - pointBegin.x;
				int dy = pointEnd.y - pointBegin.y;
				int steps,k;
				float xInCrement,yIncrement,x = pointBegin.x,y = pointBegin.y-30;
				
				if (abs(dx) > abs(dy))
				{
					steps = abs(dx);
				}
				else
				{
					steps = abs(dy);
				}
				xInCrement = float(dx)/float(steps);
				yIncrement = float(dy)/float(steps);
				pointdata[round(x)][round(y)] = 1;
				for (k = 0; k < steps; k++)
				{
					x += xInCrement;
					y += yIncrement;
					pointdata[round(x)][round(y)] = 1;
				}
				break;				
			}
		case 6:
			{
				//与5类似,DDA法画圆
				CircleEndPoint = point;
				CPoint mypoint((CircleBeginPoint.x+CircleEndPoint.x)/2,(CircleBeginPoint.y+CircleEndPoint.y)/2);
				int r;
				r=sqrt((point.x-mypoint.x)*(point.x-mypoint.x)+(point.y-mypoint.y)*(point.y-mypoint.y));
				CircleDDA(mypoint.x,mypoint.y,r,RGB(0,0,255));
				
				CPoint temppoint(mypoint.x,mypoint.y-30);

				int i;
				int steps = (sqrt(2)*r/2+0.999);
				float angle = PI / (4 * steps);
				float x, y;
				for (i = 0; i <= steps; i++)
				{
					x = r * cos(i * angle);
					y = r * sin(i * angle);
					if(temppoint.y + y >= 0 && temppoint.y +y <= 500)
					{
						if (temppoint.x-x >= 0 && temppoint.x - x <= 500)
						pointdata[round(temppoint.x - x)][round(temppoint.y + y)] = 1;
						if (temppoint.x+x >= 0 && temppoint.x+x<= 500)
						pointdata[round(temppoint.x + x)][round(temppoint.y + y)] = 1;
					}
					if (temppoint.y-y >= 0 && temppoint.y-y <= 500)
					{
						if (temppoint.x+x >= 0 && temppoint.x+x <= 500)
						pointdata[round(temppoint.x + x)][round(temppoint.y - y)] = 1;
						if (temppoint.x-x >= 0 && temppoint.x-x <= 500)
						pointdata[round(temppoint.x - x)][round(temppoint.y - y)] = 1;
						
					}
					if(temppoint.y+x >= 0 && temppoint.y+x <= 500)
					{
						if (temppoint.x+y >= 0 && temppoint.x+y <= 500)
						pointdata[round(temppoint.x + y)][round(temppoint.y + x)] = 1;
						if (temppoint.x-y >=0 && temppoint.x-y <= 500 )
						pointdata[round(temppoint.x - y)][round(temppoint.y + x)] = 1;						
					
					}
					if (temppoint.y-x >= 0 && temppoint.y-x <= 500)
					{	
						if (temppoint.x-y >=0 && temppoint.x-y <=500)
						pointdata[round(temppoint.x - y)][round(temppoint.y -x)] = 1;
						if (temppoint.x+y >= 0 && temppoint.x+y <= 500)
						pointdata[round(temppoint.x + y)][round(temppoint.y -x)] = 1;
					}  
				}

				

				break;
			}
		default:break;
		}
		

	CDialog::OnLButtonUp(nFlags, point);
}



/*
//DDA 画线算法
*/
int CMyDlg::round(const float a)
{
	return int(a+0.5);
}

void CMyDlg::lineDDA(int x0,int y0,int xEnd,int yEnd,COLORREF cr)
{
	int dx = xEnd - x0;
	int dy = yEnd - y0;
	int steps,k;
	float xInCrement,yIncrement,x = x0,y = y0;

	if (abs(dx) > abs(dy))
	{
		steps = abs(dx);
	}
	else
	{
		steps = abs(dy);
	}
	xInCrement = float(dx)/float(steps);
	yIncrement = float(dy)/float(steps);
	CClientDC dc(this);

	dc.SetPixelV(round(x),round(y),cr);
	for (k = 0; k < steps; k++)
	{
		x += xInCrement;
		y += yIncrement;
		dc.SetPixelV(round(x),round(y),cr);
	}

}

/*
//DDA 画圆算法
*/

void CMyDlg::CircleDDA(int x0,int y0,int r,COLORREF cr)
{   
	CClientDC dc(this);
	int i;
	int steps = (sqrt(2)*r/2+0.999);
	float angle = PI / (4 * steps);
	float x, y;
	for (i = 0; i <= steps; i++)
	{
		x = r * cos(i * angle);
         y = r * sin(i * angle);
			  if(y0 + y >= 30 && y0+y <= 530)
			  {
				  if (x0 >= x && x0-x <= 500)
				  dc.SetPixelV(round(x0 - x),round(y0 + y),cr);
				  if (x0+x >= 0 && x0+x <=500)
				  dc.SetPixelV(round(x0 + x),round(y0 + y),cr);
			  }
			  if (y0-y >= 30 && y0-y <= 530)
			  {
				  if (x0+x >= 0 && x0+x <=500)
				  dc.SetPixelV(round(x0 + x),round(y0 - y),cr);
				  if (x0 >= x && x0-x <= 500)
				  dc.SetPixelV(round(x0 - x),round(y0 - y),cr);
			  }
			  if(y0+x >= 30 && y0 + x <= 530)
			  {		
				  if (x0+y>=0 && x0+y <=500)
				dc.SetPixelV(round(x0 + y),round(y0 + x),cr);
				if (x0>=y && x0-y <= 500)
				dc.SetPixelV(round(x0 - y),round(y0 + x),cr);
			  }
			  if (y0-x>=30 && y0-x <=530)
			  {
				  if (x0>=y && x0-y <= 500)
				dc.SetPixelV(round(x0 - y),round(y0 - x),cr);
				if (x0+y>0 && x0+y <= 500)
				  dc.SetPixelV(round(x0 + y),round(y0 - x),cr);
			  }  
	}                 

}


BOOL CMyDlg::OnEraseBkgnd(CDC* pDC) 
{
	//不然程序刷新背景色,自己动手刷新,避免闪烁
	//	return CDialog::OnEraseBkgnd(pDC);

	return TRUE;
}





void CMyDlg::OnTimer(UINT nIDEvent) 
{
	//计时器消息响应函数
	switch(fangxiang)
	{
	case 1:
		{
			//方向向上
			for (int i = 0; i < 5; i++)
			{
				zhou = 'x';
				changelitipoint(A[i],zhou,PI/-90);
			}
			break;
		}
	case 2:
		{
			//向下
			for (int i = 0; i < 5; i++)
			{
				zhou = 'x';
				changelitipoint(A[i],zhou,PI/90);
			}
			break;
		}
	case 3:
		{
			//向左
			for (int i = 0; i < 5; i++)
			{
				zhou = 'y';
				changelitipoint(A[i],zhou,PI/90);
			}
			break;
		}
	case 4:
		{
			//向右
			for (int i = 0; i < 5; i++)
			{
				zhou = 'y';
				changelitipoint(A[i],zhou,PI/-90);
			}
			break;
		}		
	default:break;
	}
	CClientDC dc(this);

	CDC mdc;
	mdc.CreateCompatibleDC(NULL);
	mybitmap.CreateCompatibleBitmap(GetDC(),500,500);
	mdc.SelectObject(&mybitmap);
	mdc.FillSolidRect(0,0,500,500,RGB(255,255,255));
	mdc.TextOut(30,30,"按住键盘方向键可以");
	mdc.TextOut(30,50,"任意角度旋转");	
	mdc.TextOut(30,70,"按一下 W,S,A,D 可以");
	mdc.TextOut(30,90,"自动旋转");
	mdc.TextOut(30,110,"按 T 可以停止旋转");
	if (PanduanKejianmian(A[0],A[4],A[1]))
	{

		//判断可见面,可见则显示它
		CRgn rgn0;
		CPoint psz0[3];
		psz0[0] = ChangePoint(CPoint(A[0].x,A[0].y));
		psz0[1] = ChangePoint(CPoint(A[1].x,A[1].y));
		psz0[2] = ChangePoint(CPoint(A[4].x,A[4].y));
		rgn0.CreatePolygonRgn(psz0,3,WINDING);
		mdc.FillRgn(&rgn0,&mybrush[0]);
		
		mdc.MoveTo(ChangePoint(CPoint(A[0].x,A[0].y)));
		mdc.LineTo(ChangePoint(CPoint(A[4].x,A[4].y)));
		mdc.LineTo(ChangePoint(CPoint(A[1].x,A[1].y)));
		mdc.LineTo(ChangePoint(CPoint(A[0].x,A[0].y)));
	}
	if (PanduanKejianmian(A[3],A[2],A[1]))
	{
		CRgn rgn0;
		CPoint psz0[3];
		psz0[0] = ChangePoint(CPoint(A[2].x,A[2].y));
		psz0[1] = ChangePoint(CPoint(A[1].x,A[1].y));
		psz0[2] = ChangePoint(CPoint(A[3].x,A[3].y));
		rgn0.CreatePolygonRgn(psz0,3,WINDING);
		mdc.FillRgn(&rgn0,&mybrush[5]);

		mdc.MoveTo(ChangePoint(CPoint(A[3].x,A[3].y)));
		mdc.LineTo(ChangePoint(CPoint(A[2].x,A[2].y)));
		mdc.LineTo(ChangePoint(CPoint(A[1].x,A[1].y)));
		mdc.LineTo(ChangePoint(CPoint(A[3].x,A[3].y)));
	}
	if (PanduanKejianmian(A[0],A[2],A[3]))
	{
		CRgn rgn0;
		CPoint psz0[3];
		psz0[0] = ChangePoint(CPoint(A[0].x,A[0].y));
		psz0[1] = ChangePoint(CPoint(A[2].x,A[2].y));
		psz0[2] = ChangePoint(CPoint(A[3].x,A[3].y));
		rgn0.CreatePolygonRgn(psz0,3,WINDING);
		mdc.FillRgn(&rgn0,&mybrush[2]);

		mdc.MoveTo(ChangePoint(CPoint(A[0].x,A[0].y)));
		mdc.LineTo(ChangePoint(CPoint(A[2].x,A[2].y)));
		mdc.LineTo(ChangePoint(CPoint(A[3].x,A[3].y)));
		mdc.LineTo(ChangePoint(CPoint(A[0].x,A[0].y)));
	}
	if (PanduanKejianmian(A[0],A[1],A[2]))
	{
		CRgn rgn0;
		CPoint psz0[3];
		psz0[0] = ChangePoint(CPoint(A[0].x,A[0].y));
		psz0[1] = ChangePoint(CPoint(A[1].x,A[1].y));
		psz0[2] = ChangePoint(CPoint(A[2].x,A[2].y));
		rgn0.CreatePolygonRgn(psz0,3,WINDING);
		mdc.FillRgn(&rgn0,&mybrush[4]);

		mdc.MoveTo(ChangePoint(CPoint(A[0].x,A[0].y)));
		mdc.LineTo(ChangePoint(CPoint(A[1].x,A[1].y)));
		mdc.LineTo(ChangePoint(CPoint(A[2].x,A[2].y)));
		mdc.LineTo(ChangePoint(CPoint(A[0].x,A[0].y)));
	}
	if (PanduanKejianmian(A[0],A[3],A[4]))
	{
		CRgn rgn0;
		CPoint psz0[3];
		psz0[0] = ChangePoint(CPoint(A[0].x,A[0].y));
		psz0[1] = ChangePoint(CPoint(A[3].x,A[3].y));
		psz0[2] = ChangePoint(CPoint(A[4].x,A[4].y));
		rgn0.CreatePolygonRgn(psz0,3,WINDING);
		mdc.FillRgn(&rgn0,&mybrush[1]);


		mdc.MoveTo(ChangePoint(CPoint(A[0].x,A[0].y)));
		mdc.LineTo(ChangePoint(CPoint(A[3].x,A[3].y)));
		mdc.LineTo(ChangePoint(CPoint(A[4].x,A[4].y)));
		mdc.LineTo(ChangePoint(CPoint(A[0].x,A[0].y)));
	}
	if (PanduanKejianmian(A[1],A[4],A[3]))
	{
		CRgn rgn0;
		CPoint psz0[3];
		psz0[0] = ChangePoint(CPoint(A[3].x,A[3].y));
		psz0[1] = ChangePoint(CPoint(A[1].x,A[1].y));
		psz0[2] = ChangePoint(CPoint(A[4].x,A[4].y));
		rgn0.CreatePolygonRgn(psz0,3,WINDING);
		mdc.FillRgn(&rgn0,&mybrush[3]);

		mdc.MoveTo(ChangePoint(CPoint(A[1].x,A[1].y)));
		mdc.LineTo(ChangePoint(CPoint(A[3].x,A[3].y)));
		mdc.LineTo(ChangePoint(CPoint(A[4].x,A[4].y)));
		mdc.LineTo(ChangePoint(CPoint(A[1].x,A[1].y)));
	}
	dc.BitBlt(0,30,500,500,&mdc,0,0,SRCCOPY);
	mybitmap.DeleteObject();
	mdc.DeleteDC();	
	CDialog::OnTimer(nIDEvent);
}

void CMyDlg::OnZ30() 
{
	//绕Z轴旋转30度
	if (state == 7)
	{
		zhou = 'z';
		for (int i = 0 ; i < 5; i++)
		{
			changelitipoint(A[i],zhou,PI/6.0);
		}
		Invalidate();
	}
}

void CMyDlg::OnZ60() 
{
	//绕Z轴旋转60度
	if (state == 7)
	{
		zhou = 'z';
		for (int i = 0 ; i < 5; i++)
		{
			changelitipoint(A[i],zhou,PI/3.0);
		}
		Invalidate();
	}
}

void CMyDlg::OnZ90() 
{
	// 绕Z轴旋转90度
	if (state == 7)
	{
		zhou = 'z';
		for (int i = 0 ; i < 5; i++)
		{
			changelitipoint(A[i],zhou,PI/2.0);
		}
		Invalidate();
	}
	
}

void CMyDlg::OnFuyuan() 
{
	//多面体复原,将A[]数据赋值为初始数值
	A[0].x=-100; A[0].y=-57.735; A[0].z=0;
	A[1].x=100; A[1].y=-57.735; A[1].z=0;
	A[2].x=0; A[2].y=0; A[2].z=-141.4034;
	A[3].x=0; A[3].y=115.47; A[3].z=0;
	A[4].x=0; A[4].y=0; A[4].z=141.4034;
	 //刷新显示
	state = 7;
	Invalidate();

	
}

void CMyDlg::OnHuajia() 
{
	//演示画家算法原理
	if (isTimerOn)
	{
		KillTimer(1);
		isTimerOn = false;
	}
	if (isfirstview)
	{
		for (int i = 0; i < 5; i++)
		{
			char t = 'Y';
			changelitipoint( LastA[i],t,PI/3);
		}
		isfirstview = false;
	}
	
	state = 8;
	Invalidate();

}

⌨️ 快捷键说明

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