C语言绘图

C语言图形编程

本c语言代码是调用图形库写出来的。

1、看你晕不晕

C语言绘图_第1张图片

#include
#include
#define N 130

void main()
{
	initgraph(600,600);
	int i,j;

	setfillcolor(RGB(N,N,N));
	fillrectangle(0,0,600,600);
		

	for(i=0;i<600;i+=50)
	{
		for(j=0;j<600;j+=50)
		{
			setlinecolor(BLACK);
        	setfillcolor(BLACK);
			fillrectangle(j,i,j+40,i+40);

			setlinecolor(WHITE);
	        setfillcolor(WHITE);
			fillcircle(j+45,i+45,5);
		}
	}

	getch();
	closegraph();
}

2、还不晕?(代码运行后是动图)

C语言绘图_第2张图片

#include
#include
#include
#define N 130

void main()
{
	initgraph(640,480);
	setorigin(320,240);
	int i=0,j;

	setfillcolor(RGB(N,N,N));
	setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 3);

	BeginBatchDraw();
	while(1)
	{
		setlinecolor(BLUE);
		
		for(j=0;j<50;j++)
		circle(-120,0,j*20+i);

		setlinecolor(RED);
		for(j=0;j<50;j++)
		circle(120,0,fabs(j*20-i));

		i++;
		if(i==20)
			i=0;
		
		Sleep(100);
		
		FlushBatchDraw();
		cleardevice();

	}
	EndBatchDraw();
	getch();
	closegraph();
}

3、两矩形一样长吗?

C语言绘图_第3张图片

#include
#include
#include
#define N 130

void main()
{
	initgraph(640,480);
	setorigin(320,240);
	int i;
	
	
	for(i=-240;i<240;i+=20)
		line(320,0,-320,i);

	setfillcolor(RED);

	fillrectangle(-250,-100,-200,100);
	fillrectangle(150,-100,100,100);

	getch();
	closegraph();
}

4、消失的红点?(代码运行后是动图)

C语言绘图_第4张图片

#include
#include
#include
#define N 130
#define PI 3.1415926

void main()
{
	initgraph(640,480);
	setorigin(320,240);

	double a,b,x,y;
	b=0;

	setfillcolor(RGB(N,N,N));
	fillrectangle(-320,-240,320,240);

	while(1)
	{
		setfillcolor(BLACK);
		setlinecolor(BLACK);
		fillrectangle(-20,-3,20,3);
		fillrectangle(-3,-20,3,20);

		setfillcolor(RGB(200,50,0));
		setlinecolor(RGB(200,50,0));
		for(a=0;a<=2*PI;a+=PI/6)
		{
			x=200*cos(a);
			y=200*sin(a);
			fillcircle(x,y,20);
		}

			setfillcolor(RGB(N,N,N));
			setlinecolor(RGB(N,N,N));
			x=200*cos(b);
			y=200*sin(b);
		    fillcircle(x,y,30);
			b+=PI/6;

		Sleep(300);


	}
	getch();
	closegraph();
}

5、走迷宫图形版(这下清楚多了)

C语言绘图_第5张图片

#include
#include
#include
#include
using namespace std;
int i,j,x,y;
char c,n[27][52];

void map()
{
	char m[27][52]=
	{
		"#o#################################################",
		"# ###          ## #   ###########                 #",
		"#     # ###### #  # # ##        # ######### ##### #",
		"####### #    # # ## # #  # #### #         # #     #",
		"# #   # # ## # #  # # # #  #    ### ##### # ##### #",
		"# # # #   ## # ##   #   # ## ##         # #     # #",
		"# # # ###### # ########## #  ########## # ##### # #",
		"# # #        # #          # # #   #     # #     # #",
		"# # ########## ### ######## # # # ####### # ##### #",
		"#     ##       ### #      #   # #         # #     #",
		"#####  # ######### # ####   # # ########### # #####",
		"#    # # #         #    ### # #         #         #",
		"# ## # # #### ###### ####   # ######### #### ######",
		"#  #   #    # ###############      #  # #  # #   ##",
		"## ######## #              ####### ## # # ## # # ##",
		"## #      # ##### ## ##### #   # #    # #    # # ##",
		"## # #### # ############## # # # ###### ###### # ##",
		"## # #### # #              # #   #    #        # ##",
		"#  # ##   # # # ############ ##### ##  ######### ##",
		"# ## ## ### # #     ####   # # #   ### ##        ##",
		"# ## ##     #  # ####  # #   #   ### #   # ########",
		"#  # ######### #     # # ####### #   ##  #        #",
		"## #           # ### # #         ####    ######## #",
		"## ############### # # ##### # ### #######    ### #",
		"##                 #         #             ##   # #",
		"#################################################$#"
	};
	for(i=0;i<=27;i++)
	strcpy(n[i],m[i]);
	x=0;
    y=1;
}

void putmap()
{
	for(i=0;i<27;i++)
	{
		for(j=0;j<52;j++)
		{
			if(n[i][j]=='#')
			{
				setfillcolor(RED);
				fillrectangle(j*10,i*10,j*10+10,i*10+10);
			}
			if(n[i][j]=='o')
			{
				setfillcolor(BLUE);
				fillcircle(j*10+5,i*10+5,5);
			}
		}
	}
}

void control()
{
		while(1)
	{
		c=getch();
		if(c=='a'&&n[x][y-1]!='#')
		{
			n[x][y]=' ';
			y--;
			n[x][y]='o';
		}
		if(c=='d'&&n[x][y+1]!='#')
		{
			n[x][y]=' ';
			y++;
			n[x][y]='o';
		}
		if(c=='w'&&n[x-1][y]!='#')
		{
			n[x][y]=' ';
			x--;
			n[x][y]='o';
		}
		if(c=='s'&&n[x+1][y]!='#')
		{
			n[x][y]=' ';
			x++;
			n[x][y]='o';
		}
		cleardevice();
	    putmap();
	}
}
void main()
{
	map();
	initgraph(640,480);
	setorigin(20,20);
    putmap();
	control();
	getch();
	closegraph();

}

你可能感兴趣的:(数字IC,c语言,算法)