C++ 加 EasyX简易绘图实例

绘制一个圆从左向右移动

#include <graphics.h>
int main()
{
    initgraph(640,480);//绘图环境640*480
    setcolor(WHITE);//绘图前景色为白色
    setfillstyle(BS_SOLID);//填充样式为固实填充
    setfillcolor(RED);//填充颜色为蓝色
    BeginBatchDraw();//开始批量绘图
    for(int i=50; i<600; i++)
    {
        circle(i,100,40);//画圆
        floodfill(i, 100, WHITE);//填充
        FlushBatchDraw();//执行未完成的绘制任务
        Sleep(10);//挂起50毫秒
        cleardevice();//用背景色清空屏幕
    }
    EndBatchDraw();//结束批量绘制
    closegraph();//关闭图形环境
    return 0;
}

力学:弹跳球模拟程序

#include <graphics.h>
#include <conio.h>
int main()
{
    double h = 300;				// 高度
    double v = 0;				// 速度(方向向下)
    double dv = 9.8 / 50;		// 加速度(每 1/50 秒)
    // 初始化绘图窗口
    initgraph(640, 480);
    // 画地平线
    line(100, 421, 540, 421);
    while(!_kbhit())
    {
        v += dv;				// 根据加速度计算速度
        h -= (v - dv / 2);		// 计算高度
        // 如果高度低于地平线,实现反弹,速度方向取反
        if (h <= 0)
        {
            h += (v - dv / 2);
            v = - v * 0.9;		// 反弹时能量损耗 10%
        }
        // 画绿色球
        setcolor(GREEN);
        circle(320, 400 - int(h), 20);
        Sleep(20);		// 延时(每帧延时 1/50 秒)
        // 擦掉球
        setcolor(BLACK);
        circle(320, 400 - int(h), 20);
    }
    // 关闭绘图窗口
    closegraph();
    return 0;
}

钟表模拟程序(表针形式)

#include <graphics.h>
#include <conio.h>
#include <math.h>
 
#define	PI	3.1415926536
 
void DrawHand(int hour, int minute, int second)
{
    double a_hour, a_min, a_sec;					// 时、分、秒针的弧度值
    int x_hour, y_hour, x_min, y_min, x_sec, y_sec;	// 时、分、秒针的末端位置
 
    // 计算时、分、秒针的弧度值
    a_sec = second * 2 * PI / 60;
    a_min = minute * 2 * PI / 60 + a_sec / 60;
    a_hour= hour * 2 * PI / 12 + a_min / 12;
 
    // 计算时、分、秒针的末端位置
    x_sec = int(120 * sin(a_sec));
    y_sec = int(120 * cos(a_sec));
    x_min = int(100 * sin(a_min));
    y_min = int(100 * cos(a_min));
    x_hour= int(70 * sin(a_hour));
    y_hour= int(70 * cos(a_hour));
 
    // 画时针
    setlinestyle(PS_SOLID, 10);
    setcolor(WHITE);
    line(320 + x_hour, 240 - y_hour, 320 - x_hour / 7, 240 + y_hour / 7);
 
    // 画分针
    setlinestyle(PS_SOLID, 6);
    setcolor(LIGHTGRAY);
    line(320 + x_min, 240 - y_min, 320 - x_min / 5, 240 + y_min / 5);
 
    // 画秒针
    setlinestyle(PS_SOLID, 2);
    setcolor(RED);
    line(320 + x_sec, 240 - y_sec, 320 - x_sec / 3, 240 + y_sec / 3);
}
 
void DrawDial()
{
    // 绘制一个简单的表盘
    circle(320, 240, 2);
    circle(320, 240, 60);
    circle(320, 240, 160);
    outtextxy(296, 310, "BestAns");
 
    // 绘制刻度
    int x, y;
    for (int i=0; i<60; i++)
    {
        x = 320 + int(145 * sin(PI * 2 * i / 60));
        y = 240 + int(145 * cos(PI * 2 * i / 60));
 
        if (i % 15 == 0)
            bar(x - 5, y - 5, x + 5, y + 5);
        else if (i % 5 == 0)
            circle(x, y, 3);
        else
            putpixel(x, y, WHITE);
    }
}
 
void main()
{
    initgraph(640, 480);		// 初始化 640 x 480 的绘图窗口
 
    DrawDial();					// 绘制表盘
 
    setwritemode(R2_XORPEN);	// 设置 XOR 绘图模式
 
    // 绘制表针
    SYSTEMTIME ti;				// 定义变量保存当前时间
    while(!kbhit())				// 按任意键退出钟表程序
    {
        GetLocalTime(&ti);		// 获取当前时间
        DrawHand(ti.wHour, ti.wMinute, ti.wSecond);	// 画表针
        Sleep(1000);			// 延时 1 秒
        DrawHand(ti.wHour, ti.wMinute, ti.wSecond);	// 擦表针(擦表针和画表针的过程是一样的)
    }
    closegraph();				// 关闭绘图窗口
}
 

3 辆行驶的小车

#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
 
// 声明需要使用的函数
void carstart(int x, int y, int z);
void drawbus();
void init();
 
// 定义全局变量
IMAGE img;
 
 
 
void main()
{
	init();
	int x=0, y=0, z=0;
	BeginBatchDraw();
 
	while(!kbhit())
	{
		x += 2;
		y++;
		z += 3;
		if (x > 600)	x = -200;
		if (y > 600)	y = -200;
		if (z > 600)	z = -200;
 
		carstart(x, y, z); 
		FlushBatchDraw(); 
		Sleep(5);
	}
 
	EndBatchDraw();
	closegraph();
}
 
 
 
// 初始化函数,初始化窗口大小,获取所画图片
void init()
{
	// 初始化窗口大小
	initgraph(600, 600);
 
	outtextxy(70, 250, "大家好,新手来报到,希望大家多多指教"); 
	outtextxy(70, 270, "下面你们会看到我程序的效果,程序很简单"); 
	outtextxy(70, 290, "希望以后再跟大家的交流中学到更多,希望自己以后能编出更好的程序"); 
	outtextxy(70, 320, "请按任意键进观看程序执行效果"); 
 
	// 等待按键按下
	getch();
 
	cleardevice();						// 清除上面的文字进入运行效果画面
	drawbus();							// 调用绘图函数,绘制 BUS
	getimage(&img, 80, 40, 180, 90);	// 获取 BUS 图片位置,保存在 img 变量中
}
 
 
//
// 车辆行驶程序,通过 putimge 函数,改变移动的像素来达到图片移动
void carstart(int x, int y, int z)
{
	cleardevice();
	putimage(x, 40, &img);
	setlinestyle(PS_SOLID, 10);   //设置画线的大小
	line(0, 135, 600, 135);
	putimage(y, 220, &img);
	line(0, 315, 600, 315);
	putimage(z, 380, &img);
	line(0, 475, 600, 475);
}
 
 
//
// 绘制 BUS 函数,通过画一些线条,组合它们的位置,组合成一辆小车
void drawbus()
{
	setcolor(RED);
	setfillstyle(BLUE);
 
	fillcircle(120, 120, 10); 		// 画车的轮胎
	fillcircle(200, 120, 10); 		// 画车的轮胎
	line(80,  120, 110, 120);		// 画车的底部车板
	line(80,  40,  80,  120); 		// 画车的四周车板
	line(130, 120, 190, 120);		// 画车的底部车板
	line(210, 120, 250, 120);		// 画车的底部车板
	line(250, 40,  250, 120); 		// 画车的四周车板
	line(80,  40,  250, 40);		// 画车的顶部车板
 
	// 画车窗
	for(int x = 90, y = 100; x < 190 && y < 190; x += 15, y += 15)
	{
		rectangle(x, 60, y, 70);
	}
 
	// 画车门
	rectangle(220, 60, 240, 120);
	line(230, 60, 230, 120);
	circle(230, 90, 5); 
}

鼠标操作演示

#include <graphics.h>
#include <conio.h>
 
int main()
{
	// 初始化图形窗口
	initgraph(640, 480);
 
	MOUSEMSG m;		// 定义鼠标消息
 
	while(true)
	{
		// 获取一条鼠标消息
		m = GetMouseMsg();
 
		switch(m.uMsg)
		{
			case WM_MOUSEMOVE:
				// 鼠标移动的时候画红色的小点
				putpixel(m.x, m.y, RED);
				break;
 
			case WM_LBUTTONDOWN:
				// 如果点左键的同时按下了 Ctrl 键
				if (m.mkCtrl)
					// 画一个大方块
					rectangle(m.x-10, m.y-10, m.x+10, m.y+10);
				else
					// 画一个小方块
					rectangle(m.x-5, m.y-5, m.x+5, m.y+5);
				break;
 
			case WM_RBUTTONUP:
				return 0;	// 按鼠标右键退出程序
		}
	}
 
	// 关闭图形窗口
	closegraph();
	return 0
}

** 模拟蒙特卡罗法求π的值**

#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
    long m=0,n=0,i;
    double xi,yi,y;
    initgraph(640, 480);// 初始化 640 x 480 的绘图窗口
    setcolor(YELLOW);
//line(200,50,200,400);line(200,50,205,60); line(200,50,195,60);
//line(50,200,400,200);line(400,200,390,195); line(400,200,390,205);
    rectangle(220,100,420,300);
    circle(320,200,100);
    srand((unsigned)time(NULL));
    for(i=0; i<100000; i++)   /*设置产生随机数的个数*/
    {
        /*---产生-1到1之间的随机数xi和yi-----*/
        xi=(rand()%(1000-0+1)+0)/500.0-1.0;
        yi=(rand()%(1000-0+1)+0)/500.0-1.0;
        putpixel((int)(xi*100+320),(int)(yi*100+200),(int)GREEN);/*在正方形区域内画点*/
        /*printf("xi=%f,yi=%f\n",xi,yi);*/
        if(yi*yi>=(-(1-xi*xi))&&yi*yi<=1-xi*xi) /*判断是否在圆内并计数*/
            m++,n++;
        else m++;
        Sleep(10);
    }
    y=4.0*n/m;
    /*计算PI值*/
    char str[100];
    sprintf(str,"PI=%f",y);
    outtextxy(280,400,str);
    getch();
    closegraph();// 关闭绘图窗口
    return 0;
}
 
  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值