Discuz! Board

 找回密码
 立即注册
查看: 295|回复: 0

小球碰撞只新冠传播

[复制链接]
回帖奖励 5 金钱 回复本帖可获得 5 金钱奖励! 每人限 1 次

49

主题

70

帖子

23万

积分

论坛元老

Rank: 8Rank: 8

积分
232976

大王燕 Lv:51
发表于 2022-11-11 19:36:36 | 显示全部楼层 |阅读模式
[C++] 纯文本查看 复制代码
#include<bits/stdc++.h>
#include <graphics.h>             //包含EGE的头文件
#include<time.h>
using namespace std;
int width=800;
int hight=600;
int number;
int count1=1,count2;
struct ballon
{
    int r;
    float vx;
    float vy;
    float x;
    float y;
    float g;
    COLORS color;
    int healthy;
    int n95;
}ball[10000];
float inx(int min,int max)
{
    float t=rand()%(max-min)+min;
    return t;
}
void is_ill()
{
    for(int i=0;i<number;i++)
    {
        for(int j=0;j<number;j++)
        {
			if((abs(ball[i].x-ball[j].x)+(abs(ball[i].y-ball[j].y))<(ball[i].r+ball[j].r))&&(ball[i].healthy!=ball[j].healthy))
            {
            	float t=inx(0,100);
            	if(t<=30)
            	{
            		ball[i].healthy=1;
                	ball[j].healthy=1;
                	count1++;
				}
            }
        }
    }
}
int main()
{
    srand(time(0));//随机种子函数   
    cout<<"请输入病人数量(不少于1000):";
    cin>>number;  
    for(int i=0;i<number;i++)
    {
        ball[i].x = inx(ball[i].r*2+1,width-ball[i].r);
        ball[i].y = inx(ball[i].r*2+1,hight-ball[i].r);
        ball[i].vx = rand()%10;
        ball[i].vy = rand()%10;
        ball[i].r = 10+rand()%10;
        ball[i].g=0;
        ball[i].color=GREEN;
        ball[i].healthy=0;
    }
     
    ball[0].color=RED;
    ball[0].healthy=1;
    initgraph(width, hight);                //初始化图形界面
    setcolor(RED);  //设置画图线为红色 
    setbkcolor(WHITE);//设置背景颜色为白色
    setfillcolor(RED);//设置填充颜色为红色
    setfont(25,0,"幼圆");//(字体高度,字体宽度(为0即为自适应),字形)设置字体为25,幼圆字体
    setbkmode(TRANSPARENT); //设置文字背景色为透明(默认为有背景色)
    while(1)
    {
        is_ill();
        for(int i=0;i<number;i++)
        {
            ball[i].vy=ball[i].vy+ball[i].g;
            ball[i].y=ball[i].vy+ball[i].y;
            ball[i].vx=ball[i].vx+ball[i].g;
            ball[i].x=ball[i].vx+ball[i].x;
            if(ball[i].y>=hight-ball[i].r)
            {
                ball[i].y=hight-ball[i].r;
                ball[i].vy=-ball[i].vy;
            }
            if(ball[i].y<=ball[i].r)
            {
                ball[i].y=ball[i].r;
                ball[i].vy=-ball[i].vy;
            }
            if(ball[i].x>=width-ball[i].r)
            {
                ball[i].x=width-ball[i].r;
                ball[i].vx=-ball[i].vx;
            }
            if(ball[i].x<=ball[i].r)
            {
                ball[i].x=ball[i].r;
                ball[i].vx=-ball[i].vx;
            }
            fillellipse(ball[i].x, ball[i].y, ball[i].r, ball[i].r); //画一个实心圆 
            //floodfillsurface(400, 300, RED);
            setfillcolor(ball[i].color);
            if(ball[i].healthy==1)
            {
                ball[i].color=RED;
                
            }
        }
        count2+=10;
		xyprintf(20,20,"病人数量:%d",count1);//格式化输出文字
        xyprintf(20,60,"时间:%d",count2);//格式化输出时间 
        Sleep(10);
        cleardevice();
    }
       
    getch();                            //暂停,等待键盘按键
    
    closegraph();        //关闭图形界面
      
    return 0;
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-2-13 05:00 , Processed in 0.050264 second(s), 30 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表