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

📄 ddex1.cpp

📁 人工智能遗传算法,经过改进,希望对大家有帮助,常常联系啊.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
						};
	for(int l=0;l<6;l++)
	{	i[l][0]+=i[l][4];
		i[l][2]+=i[l][4];
		i[l][1]+=i[l][5];
		i[l][3]+=i[l][5];
		rectFill(ddsd,i[l][0],i[l][1],i[l][2],i[l][3],color);
		color+=0x39e7;
	}
	for(l = 0;l<6;l++)
	{	if( i[l][0]== 0||i[l][0]==639||i[l][2]==0||i[l][2]==639)
		{	i[l][4] = -i[l][4];
			if(i[l][1] == 0||i[l][1]==479||i[l][3]==0||i[l][3]==479)
				i[l][5]= -i[l][5];
			continue;
		}
		if(i[l][1] == 0||i[l][1]==479||i[l][3]==0||i[l][3]==479)
		{	i[l][5]= -i[l][5];
			continue;
		}
		for(int j=0;j<6;j++)
		{	if(l!=j)
			{	if(i[l][1]<=i[j][3]&&i[l][3]>=i[j][1])
				if(i[l][0]==i[j][2]||i[l][0]+i[l][4]==i[j][2]||i[l][2]==i[j][0]||i[l][2]+i[l][4]==i[j][0])
				{	i[l][4]=-i[l][4];
					break;
				}
			}
		}
		for(j=0;j<6;j++)
		{	if(l!=j)
			{	if(i[l][0]<=i[j][2]&&i[l][2]>=i[j][0])
				if(i[l][1]==i[j][3]||i[l][1]+i[l][5]==i[j][3]||i[l][3]==i[j][1]||i[l][3]+i[l][5]==i[j][1])
				{	i[l][5]=-i[l][5];
					break;
				}
			}
		}
	}
}

void polygonDemo1(DDSURFACEDESC* ddsd)
{	static unsigned char rrr=0,ggg=80,bbb=160;
	WORD color=RGBto16bit(&ddsd->ddpfPixelFormat,rrr,ggg,bbb);
	rrr+=2;ggg+=1;bbb+=2;
	static POINT points[5]={	{340,80},
						{490,180},
						{400,300},
						{250,330},
						{120,200}
					};
	static int range[5][6]={	{300,60,360,120,1,1},
						{440,110,600,210,-1,1},
						{380,270,460,380,-1,1},
						{200,280,320,440,-1,-1},
						{100,150,200,260,1,-1}
					};
	for(int i=0;i<5;i++)
	{	points[i].x += range[i][4];
		points[i].y += range[i][5];
		if(points[i].x <= range[i][0]||points[i].x >= range[i][2])
			range[i][4] = -range[i][4];
		if(points[i].y <= range[i][1]||points[i].y >= range[i][3])
			range[i][5] = -range[i][5];
	}
	drawPolygon(ddsd,points,5,color);
}

void polygonDemo2(DDSURFACEDESC* ddsd)
{	static unsigned char rrr=0,ggg=80,bbb=160;
	WORD color=RGBto16bit(&ddsd->ddpfPixelFormat,rrr,ggg,bbb);
	rrr+=2;ggg+=1;bbb+=2;
	static POINT points[5]={	{320,100},
						{100,250},
						{210,440},
						{500,390},
						{500,150}
					};
	static int range[5][6]={	{210,-50,400,120,1,1},
								{-50,130,210,380,-1,1},
								{150,410,280,540,1,-1},
								{460,360,560,460,-1,-1},
								{480,130,700,260,1,-1}
							};
	for(int i=0;i<5;i++)
	{	points[i].x += range[i][4];
		points[i].y += range[i][5];
		if(points[i].x <= range[i][0]||points[i].x >= range[i][2])
			range[i][4] = -range[i][4];
		if(points[i].y <= range[i][1]||points[i].y >= range[i][3])
			range[i][5] = -range[i][5];
	}
	polygonFill(ddsd,points,5,color);
}

typedef struct { float x,y; }Complex;
inline Complex complexSquare(Complex c)
{	Complex cSq;
	cSq.x = c.x*c.x - c.y*c.y;
	cSq.y = 2*c.x*c.y;
	return cSq;
}

inline int iterate(Complex zInit,int maxIter)
{	Complex z=zInit;
	int cnt = 0;
	while((z.x*z.x+z.y*z.y<=4.0)&&(cnt < maxIter))
	{	z = complexSquare(z);
		z.x += zInit.x;
		z.y += zInit.y;
		cnt++;
	}
	return cnt;
}

void fsDemo1(DDSURFACEDESC* ddsd)
{	int nx,ny,maxIter;
	WORD color;
	static int l=0;
	float realMin,realMax,imagMin,imagMax;
	static int c[6][2]={	{0xf81f,-0x0800},{0x001f,0x0040},{0x07ff,-0x0001},
					{0x07e0,0x0800},{0xffe0,-0x0040},{0xf800,0x0001}
				};
	nx = 640,ny = 480,maxIter = 186;
	realMin = -0.78937,realMax=-0.78928,imagMin=0.16434,imagMax=0.16443;
	//realMin = -0.790,realMax=-0.788,imagMin=0.161,imagMax=0.163;
	float realInc = (realMax - realMin)/nx;
	float imagInc = (imagMax - imagMin)/ny;
	Complex z;
	int x,y;
	int cnt;
	//for(x=l,z.x=realMin+realInc*l;x<nx;x+=201,z.x+=realInc*201)
	x=l,z.x=realMin+realInc*l;
		for(y=0,z.y=imagMin;y<ny;y++,z.y+=imagInc)
		{	cnt = iterate(z,maxIter);
			if(cnt == maxIter)
				writePixel(ddsd,x,y,0x0000);
			else
			{	color = c[cnt/31][0] + c[cnt/31][1]*(cnt%31);
				writePixel(ddsd,x,y,color);
			}
		}
	l++;
	if(l>=640)
		l = 0;
}

void demo3d(DDSURFACEDESC* ddsd)
{	DDSURFACEDESC       ddsdZBuf;
	ddsdZBuf.dwSize = sizeof(ddsdZBuf);
	HRESULT ddrval;
	UINT i;
	p1.rotatePoints(&m4,CONVERTALL);
	face.updataParams(&p1,CONVERTALL);
	p1.clearConvert();
	face.clearConvert();
	face.judgeSide(&p1,&pot);
//	face.calLight(&vLight,groundLight,spotLight,CONVERTSIGNED);
//	face.calLightSpot(&potLight,&p1,groundLight,spotLight-4,CONVERTSIGNED);
	face.calLightMirror(&potLight,&p1,groundLight,spotLight,CONVERTSIGNED);
	p2.copy(&p1);
	p2.convertPoints(&m2,CONVERTSIGNED);
	while ((ddrval=lpDDSZBuffer->Lock(NULL, &ddsdZBuf, 0, NULL)) == DDERR_WASSTILLDRAWING)
				;
	if(ddrval == DDERR_SURFACELOST )
	{	lpDDSZBuffer->Restore();
		return;
	}
	for(i=0;i<face.topPos;i++)
	{	
		if(face.shouldConvert[i])
		{//	fillTriangle3dWithZ(ddsd,&ddsdZBuf,&p2,&face.faces[i],lightLevel[face.faces[i].light]);
			fillTriangle3dWithZLT(ddsd,&ddsdZBuf,&p2,&face.faces[i],face.faces[i].light);
		//	drawTriangle3dWithT1(ddsd,&p2,&face.faces[i],0xffff);
		}
	}
	lpDDSZBuffer->Unlock(NULL);
}

void demo3d2(DDSURFACEDESC* ddsd)
{	DDSURFACEDESC       ddsdZBuf;
	ddsdZBuf.dwSize = sizeof(ddsdZBuf);
	HRESULT ddrval;
	UINT i;
	static UINT ii=0;
	ii++;
	if((ii/110)%2 == 0)
		p3.rotatePoints(&m5,CONVERTALL);
	else
		p3.rotatePoints(&m8,CONVERTALL);
	face1.updataParams(&p3,CONVERTALL);
	p3.clearConvert();
	face1.clearConvert();
	face1.judgeSide(&p3,&pot2);
	face1.calLight(&vLight,groundLight,spotLight,CONVERTSIGNED);
	p4.copy(&p3);
	p4.convertPoints(&m7,CONVERTSIGNED);
//	while ((ddrval=lpDDSZBuffer->Lock(NULL, &ddsdZBuf, 0, NULL)) == DDERR_WASSTILLDRAWING)
//				;
//	if(ddrval == DDERR_SURFACELOST )
//	{	lpDDSZBuffer->Restore();
//		return;
//	}
	//for(i=0;i<p2.topPos;i++)
	for(i=0;i<face1.topPos;i++)
	{	//drawTriangle3d(ddsd,&p2,&face.faces[i],0xffff);
		if(face1.shouldConvert[i])
		{	//fillTriangle3d(ddsd,&p2,&face.faces[i],lightLevel[face.faces[i].light]);
		//	fillTriangle3dWithZT1(ddsd,&ddsdZBuf,&p4,&face1.faces[i],lightLevel[face1.faces[i].light]);
			fillTriangle3dWithT1(ddsd,&p4,&face1.faces[i],lightLevel[face1.faces[i].light]);
		//	fillTriangle3dWithT1(ddsd,&p4,&face1.faces[i],lightLevel[face1.faces[i].light]);
		}
	}
//	lpDDSZBuffer->Unlock(NULL);
}

void demoEnd()
{	static int startPosition=390, startLine = 0;
	HDC         hdc;
	RECT rcRect;
	rcRect.left = 0;
	rcRect.top = 120;
	rcRect.right = 640;
	rcRect.bottom = 390;
	if (lpDDSBack->GetDC(&hdc) == DD_OK)
	{	SetBkColor( hdc, RGB( 0, 0, 255 ) );
		int	oldBkMode = SetBkMode(hdc,TRANSPARENT);
		int pos = startPosition;
		for(int i=startLine;i<lines;i++)
		{	if(pos > 390)
				break;
			SetTextColor( hdc, RGB( 255, 255, 0 ) );
			//TextOut( hdc, 20,pos, txt[i], lstrlen(txt[i]) );
			 ExtTextOut(hdc,20,pos,ETO_CLIPPED,&rcRect,txt[i],lstrlen(txt[i]),0);
			pos += 30;
		}
		SetBkMode(hdc,oldBkMode);
		lpDDSBack->ReleaseDC(hdc);
	}
	if(startPosition > 90)
		startPosition--;
	else
	{	startPosition += 30;
		if(startLine < lines-1)
			startLine++;
		else
		{	startPosition=390;
			startLine = 0;
		}
	}
}

DWORD WINAPI demo(LPVOID lpparm)
{	HDC         hdc;
	HRESULT ddrval;
	DDSURFACEDESC       ddsd;
	ddsd.dwSize = sizeof(ddsd);
	while(1){
		if( bQuit )
			return -1;
			//ExitThread(-1);
		if( bActive )
		{	
			DDBLTFX ddbltfx;
			ddbltfx.dwSize = sizeof(ddbltfx);
			ddbltfx.dwFillColor = 0;
			if(bClear){
			lpDDSBack->Blt(NULL,NULL,NULL,DDBLT_COLORFILL|DDBLT_WAIT, &ddbltfx);
			if(phase == 11)
			{	ddbltfx.dwFillDepth = MAX_ZDEPTH;
				lpDDSZBuffer->Blt(NULL,NULL,NULL,DDBLT_DEPTHFILL|DDBLT_WAIT, &ddbltfx);
			}
			while ((ddrval = lpDDSBack->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
				;
			frames++;
			if( ddrval == DD_OK )
			{	switch (phase)
				{	case 0:
						lineDemo1(&ddsd);
						break;
					case 1:
						lineDemo2(&ddsd);
						break;
					case 2:
						circleDemo1(&ddsd);
						break;
					case 3:
						circleDemo2(&ddsd);
						break;
					case 4:
						ellipseDemo1(&ddsd);
						break;
					case 5:
						ellipseDemo2(&ddsd);
						break;
					case 6:
						rectDemo1(&ddsd);
						break;
					case 7:
						rectDemo2(&ddsd);
						break;
					case 8:
						polygonDemo1(&ddsd);
						break;
					case 9:
						polygonDemo2(&ddsd);
						break;
					case 10:
						fsDemo1(&ddsd);
						break;
					case 11:
						demo3d(&ddsd);
						break;
					case 12:
						demo3d2(&ddsd);
						break;
					case 13:
					{	RECT rcRect;
						rcRect.left = 0;
						rcRect.top = 0;
						rcRect.right = 640;
						rcRect.bottom = 480;
						lpDDSBack->Unlock(NULL);
						lpDDSBack->Blt(&rcRect,lpDDSEndBmp,&rcRect, FALSE, NULL);
						//lpDDSBack->BltFast(0,0,lpDDSEndBmp,&rcRect,FALSE);
						demoEnd();
					}
						break;
					default:
						;
				}
				lpDDSBack->Unlock(NULL);
			}
			if (lpDDSBack->GetDC(&hdc) == DD_OK)
			{
				SetBkColor( hdc, RGB( 0, 0, 255 ) );
				SetTextColor( hdc, RGB( 255, 255, 0 ) );
				TextOut( hdc, 10, 10, szFrontMsg[phase], lstrlen(szFrontMsg[phase]) );
				TextOut( hdc, 330, 420, szMsg, lstrlen(szMsg) );
				TextOut( hdc, 330, 450, szMsg1, lstrlen(szMsg1) );
				if(time(NULL)>tt)
				{	tt = time(NULL);
					rate = frames;
					frames=0;
				}
				char cc[40];
				sprintf(cc,"屏幕刷新率为:%ld幅/秒",rate);
				TextOut( hdc, 120, 10, cc, lstrlen(cc) );
				lpDDSBack->ReleaseDC(hdc);
			}
			while( 1 )
			{
			    ddrval = lpDDSPrimary->Flip( NULL, 0 );
		        if( ddrval == DD_OK )
			    {
				    break;
				}
				if( ddrval == DDERR_SURFACELOST )
				{
					ddrval = lpDDSPrimary->Restore();
					if( ddrval != DD_OK )
					{
						break;
					}
				}
				if( ddrval != DDERR_WASSTILLDRAWING )
				{
					break;
				}
			}
			}
			else
			{	while ((ddrval = lpDDSPrimary->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
				;
				frames++;
				if( ddrval == DD_OK )
				{	switch (phase)
					{	case 10:
							fsDemo1(&ddsd);
							break;
						default:
							;
					}
					lpDDSPrimary->Unlock(NULL);
				}
				if (lpDDSPrimary->GetDC(&hdc) == DD_OK)
				{
					SetBkColor( hdc, RGB( 0, 0, 255 ) );
					SetTextColor( hdc, RGB( 255, 255, 0 ) );
					TextOut( hdc, 10, 10, szFrontMsg[phase], lstrlen(szFrontMsg[phase]) );
					TextOut( hdc, 330, 420, szMsg, lstrlen(szMsg) );
					TextOut( hdc, 330, 450, szMsg1, lstrlen(szMsg1) );
					if(time(NULL)>tt)
					{	tt = time(NULL);
						rate = frames;
						frames=0;
					}
					char cc[40];
					sprintf(cc,"屏幕刷新率为:%ld幅/秒",rate);
					TextOut( hdc, 120, 10, cc, lstrlen(cc) );
					lpDDSPrimary->ReleaseDC(hdc);
				}
			}
		}
	}
}

⌨️ 快捷键说明

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