在执行蚁群期间在 winforms 中动态绘制蚂蚁 [英] Dynamic drawing ants in winforms during execution of ant colony

查看:34
本文介绍了在执行蚁群期间在 winforms 中动态绘制蚂蚁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题之后(显示移动像素的轨迹在 C# WinForm 项目中) 对于我在 C# 中的个人蚁群项目,我正在尝试应用解决方案第二个建议的解决方案:将路径绘制成位图并将新蚂蚁绘制到表面上.>

After this question (Show trail of moving pixel in C# WinForm project) for my personal ant colony project in c#, I'm trying to apply the solution second suggested solution: the one that combines drawing the trail into a bitmap and the new ants onto the surface.

[...]Application.Run(new ShowAnts());[...]

public partial class ShowAnts : Form
{
    Bitmap bmp;
    int j = 0;
    public ShowAnts()
    {
        InitializeAntProgram();
        InitializeComponent();
        bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
        pictureBox1.Image = bmp;
    }

    public void RenderAnts(object sender, PaintEventArgs e)
    {
        using (Graphics G = Graphics.FromImage(pictureBox1.Image))
        {
            while (j < 1000)
            {
                Map.EvaporatesPheromones();
                foreach (Vector2D food in foodSrcs)
                {
                    Map.SetMapPoint(food, 500);
                }
                foreach (Ant a in ants)
                {
                    Brush c;
                    c = Brushes.DarkBlue;
                    if (a.role == AntRole.Scout)
                    {
                        a.Move(j);
                        c = Brushes.Red;
                    }
                    e.Graphics.FillRectangle(Brushes.DarkBlue, a.position.x, a.position.y, 1, 1);
                    G.FillRectangle(Brushes.Gray, a.position.x, a.position.y, 1, 1);
                }
                j++;
            }
        }
    }
}

上面的代码显示了将蚂蚁运动绘制到 Winform 中的图形尝试.它工作得很好,但它只显示了最终结果.我想展示在不重新解析我的地图信息的情况下保留图形路径信息的逐步演变.

The code above shows the graphic attempt to draw the ant movement into a winform. It works perfectly, but it shows only the final result. I would like to show the step by step evolution keeping the graphical trail information without reparsing my map info.

请考虑我开发这个图形界面"的工作控制台项目已经存在,所以:

Please consider that a working console project on which I' developing this "graphic interface" already exists so:

  • 一些变量设置在项目的其他地方(即:食物);
  • `a.Move(j);` 指的是蚂蚁逻辑本身(分析、决策、新单元格移动指的是地图数组);
  • `j` 计数器用于计算步数和设置任意停止,但没有实际用途;
  • 我已经在存储将有关信息素、运动、位置等的所有信息放入地图数组和其他一些变量中.

推荐答案

查看您的代码以及上一个问题的注释,您似乎缺少可以为运动设置动画的部分.相反,您在似乎是 Paint 事件的内部循环.

Looking at your code and also the comments of the previous question, it seems that you are missing the part that would animate the movement. Instead you are looping inside what seems to be the Paint event.

这里有一个快速解决方法.它添加了一个 Timer 来触发 RenderAnts 事件,该事件似乎连接到 pictureBox1.Paint 处理程序..:

Here is a quick fix for that. It adds a Timer that triggers the RenderAnts event, which seems to be hooked up to the pictureBox1.Paint handler..:

一些类级别的变量:

 int counter = 0;
 int limit = 1000;
 Timer antTimer = new Timer(); 

开始代码:

 antTimer.Interval = 50;   // <-- pick your speed !!
 antTimer.Tick += (ss, ee) =>
 { pictureBox1.Invalidate(); counter++; if (counter > limit) antTimer.Stop(); };
 antTimer.Start();

速度为 50ms,即每秒 20 个 Ticks.

The speed is 50ms, which means 20 Ticks per second.

Tick 事件与一个微小的 Lambda 表达式内联,并且只有一个语句加上循环逻辑.通过InvalidatingpictureBox1 控制它的Paint 事件,从而触发RenderAnts 事件.

The Tick event is inlined with a tiny Lambda epression and has only one statement plus the loop logic. By Invalidating the pictureBox1 control its Paint event and thereby the RenderAnts event is triggered.

另请注意,我称其为快速修复".通常你会区分动画的渲染和移动代码;但在这种情况下,这种细微差别并不重要.

Also note that I called it a 'quick fix'. Usually you would discern between the rendering and the moving code of an animation; but in this case this fine difference doesn't matter much.

现在我们改变RenderAnts方法,去掉循环:

Now we change the RenderAnts method, taking out the loop:

public void RenderAnts(object sender, PaintEventArgs e)
{
    using (Graphics G = Graphics.FromImage(pictureBox1.Image))
    {
        Map.EvaporatesPheromones();
        foreach (Vector2D food in foodSrcs)
        {
           Map.SetMapPoint(food, 500);
        }
        foreach (Ant a in ants)
        {
           Brush c = Brushes.DarkBlue;
           if (a.role == AntRole.Scout)
           {
              a.Move(j);
              c = Brushes.Red;
           }
           e.Graphics.FillRectangle(c, a.position.x, a.position.y, 1, 1);
           G.FillRectangle(Brushes.Gray, a.position.x, a.position.y, 1, 1);
        }
    }
}

您可能还想添加一个开始/停止按钮.还有一个 TrackBar 来改变速度..

You also may want to add a Start/Stop Button. Also a TrackBar to change the speed..

现在您应该能够以 20Hz 的频率观察蚂蚁的进度,并留下灰色的痕迹.

Now you should be able to watch the progress of your ants at 20Hz, leaving grey trails.

这篇关于在执行蚁群期间在 winforms 中动态绘制蚂蚁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆