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);
        }
    }
}
相关推荐
陌上花开࿈3 小时前
调用第三方接口
java
Aileen_0v03 小时前
【玩转OCR | 腾讯云智能结构化OCR在图像增强与发票识别中的应用实践】
android·java·人工智能·云计算·ocr·腾讯云·玩转腾讯云ocr
桂月二二5 小时前
Java与容器化:如何使用Docker和Kubernetes优化Java应用的部署
java·docker·kubernetes
黄金小码农5 小时前
C# 2024/12/26 周四
c#
liuxin334455665 小时前
学籍管理系统:实现教育管理现代化
java·开发语言·前端·数据库·安全
小马爱打代码5 小时前
设计模式详解(建造者模式)
java·设计模式·建造者模式
栗子~~6 小时前
idea 8年使用整理
java·ide·intellij-idea
2301_801483696 小时前
Maven核心概念
java·maven
Q_19284999066 小时前
基于Spring Boot的电影售票系统
java·spring boot·后端
我要学编程(ಥ_ಥ)7 小时前
初始JavaEE篇 —— 网络原理---传输层协议:深入理解UDP/TCP
java·网络·tcp/ip·udp·java-ee