⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swarm.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <iostream>
#include "canvas.h"
#include "utils.h"
#include "randgen.h"

#include <string>

using namespace std;

const double PI = 3.14159265;

class Leader : public Bouncer
{
  public:
      Leader(Shape& s, double velocity)
        : Bouncer(s,PI/4,velocity)
      { }
      
  protected:
      void update(AnimatedCanvas& c)
      {
          if (myGen.RandInt(0,1000) < 10)
          {
              myAngle = myGen.RandReal(0,2*PI);
          }
          Bouncer::update(c);
      }
      RandGen myGen;
};


class Follower : public Bouncer
{
  public:
    Follower(Shape& s, Leader * leader,double velocity)
      : Bouncer(s,PI/4,velocity),
        myLeader(leader)
    { }
    
  protected:
  
      void update(AnimatedCanvas& c)
      {
          Point p = myLeader->getLocation();
          Point me = getLocation();
          myAngle = atan2(p.y-me.y,p.x-me.x);
          Bouncer::update(c);
      }
    
  private:
     
     Leader * myLeader;
};


int main()
{
    int width=400;
    int height=400;
    RandGen gen;
    AnimatedCanvas canvas(width,height,100,20);
    
    int limit = 8;
    double vel=8;
    color col[] = {RED,YELLOW,GREEN,BLACK};
    color col2[] = {BLUE,MAGENTA,KHAKI,TURQUOISE};
    int j,k;
    
    for(k=0; k < limit; k++)
    {
        CircleShape c(Point(20,20),8,col[k%4]);
        Leader * lead = new Leader(c,vel+4);
        canvas.addShape(lead);
        for(j=0; j < 6; j++)
        {
            CircleShape c2(Point(gen.RandInt(10,width),
                                 gen.RandInt(10,height)),8,col2[k%4]);
        
            canvas.addShape(new Follower(c2,lead,(vel-j)));
        }
    }
    
    canvas.runUntilEscape();
    
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -