C# WPF 自己写的一个模拟病毒传播的程序,有可视化

源代码: https://github.com/t39q/VirusSpread

主要代码

csharp 复制代码
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VirusSpread.BussnessLogic
{
    public class VirusSpread
    {
        public static ConcurrentBag<Model.PeopleModel.People> peoples;

        public static void Init()
        {
            peoples = new ConcurrentBag<Model.PeopleModel.People>();
            Random random = new Random(DateTime.Now.Millisecond * DateTime.Now.Second);
            for (int i = 0; i < 100; i++)
            {
                Model.PeopleModel.People people = new Model.PeopleModel.People();
                people.ID = i;
                people.X = random.NextDouble() * 400;
                people.Y = random.NextDouble() * 400;
                people.Disease = false;
                peoples.Add(people);
            }
            for (int i = 100; i < 103; i++)
            {
                Model.PeopleModel.People people = new Model.PeopleModel.People();
                people.ID = i;
                people.X = random.NextDouble() * 400;
                people.Y = random.NextDouble() * 400;
                people.Disease = true;
                peoples.Add(people);
            }
        }
        public static void Run(int spreadCount,int moveDistance)
        {
            int count = 0;
            Random random = new Random(DateTime.Now.Millisecond * DateTime.Now.Second);
            while (true)
            {
                if (count > spreadCount)
                {
                    break;
                }
                foreach (var item in peoples)
                {
                    int parity = random.Next(0,200);
                    double tempX, tempY;
                    if (parity % 2 == 0)
                    {
                        tempX = item.X + random.NextDouble() * moveDistance;
                        tempY = item.Y + random.NextDouble() * moveDistance;
                    }
                    else
                    {
                        tempX = item.X - random.NextDouble() * moveDistance;
                        tempY = item.Y - random.NextDouble() * moveDistance;
                    }
                    
                    if (tempX>0 && tempX < 400 && tempY>0 && tempY < 400)
                    {
                        item.X = tempX;
                        item.Y = tempY;
                    }
                    
                }
                Thread.Sleep(1000);
                count++;
            }
        }
        public static void Spread(int spreadCount,int infectious)
        {
            int count = 0;
            while (true)
            {
                if (count > spreadCount)
                {
                    break;
                }
                var healthy = peoples.Where(item => item.Disease == false).ToList();
                var infected = peoples.Where(item => item.Disease == true).ToList();


                var pairsJoin = (from healthyItem in healthy
                                 from infectedItem in infected
                                 select new { healthyItem, infectedItem }).ToList();

                foreach (var item in pairsJoin)
                {
                    CalcDistance(item.healthyItem, item.infectedItem, out double distance);
                    if (distance < infectious)
                    {
                        var infectedLabel = peoples.Where(find => find.ID == item.healthyItem.ID).FirstOrDefault();
                        infectedLabel.Disease = true;
                    }
                }
                Thread.Sleep(1000);
                count++;
            }
        }
        public static void Display(out string statistics)
        {
            statistics = peoples.Where(item => item.Disease == true).Count().ToString();
        }
        public static void CalcDistance(Model.PeopleModel.People A, Model.PeopleModel.People B, out double distance)
        {
            double X = Math.Abs(A.X - B.X);
            double Y = Math.Abs(A.Y - B.Y);
            distance = Math.Sqrt(X * X + Y * Y);
        }
    }
}
相关推荐
张张张31210 分钟前
4.2学习总结 Java:list系列集合
java·学习
KATA~13 分钟前
解决MyBatis-Plus枚举映射错误:No enum constant问题
java·数据库·mybatis
xyliiiiiL29 分钟前
一文总结常见项目排查
java·服务器·数据库
shaoing30 分钟前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库
源之缘-OFD先行者1 小时前
GMap.NET + WPF:构建高性能 ADS-B 航空器追踪平台
.net·wpf·ads-b
腥臭腐朽的日子熠熠生辉1 小时前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven
ejinxian1 小时前
Spring AI Alibaba 快速开发生成式 Java AI 应用
java·人工智能·spring
杉之1 小时前
SpringBlade 数据库字段的自动填充
java·笔记·学习·spring·tomcat
勘察加熊人2 小时前
forms实现俄罗斯方块
c#
圈圈编码2 小时前
Spring Task 定时任务
java·前端·spring