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

📄 backandforth.cpp

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

class BackForthBouncer : public Bouncer
{
  public:
    BackForthBouncer(Shape * left, Shape * right,
                     double angle, double velocity);
    virtual void updateright(AnimatedCanvas& c, Point& p);
    virtual void updateleft(AnimatedCanvas& c, Point& p);  
    virtual Shape* clone();
  protected:
    Shape * myLeft;
    Shape * myRight;
};


BackForthBouncer::BackForthBouncer(Shape * left, Shape * right,
                                   double angle, double velocity)
  : Bouncer(left, angle, velocity)
{
    myLeft = left;
    myRight = right;
    if (PI/2 <= angle && angle <= 3*PI/2)
    {   myShape = myLeft;
    }
    else
    {   myShape = myRight;
    }
}

void BackForthBouncer::updateright(AnimatedCanvas& c, Point& p)
{
    myShape = myLeft;
    myShape->setLocation(p);
    Bouncer::updateright(c,p);
}

void BackForthBouncer::updateleft(AnimatedCanvas& c, Point& p)
{
   myShape = myRight;
   myShape->setLocation(p);
   Bouncer::updateleft(c,p);
}
 
Shape* BackForthBouncer::clone()
{
    BackForthBouncer * fb = 
        new BackForthBouncer(myLeft,myRight,myAngle,myVelocity);
    return fb;
}

int main()
{
    const int WIDTH=  400;
    const int HEIGHT= 400;
    AnimatedCanvas display(WIDTH,HEIGHT,20,20);
    RandGen rgen;
    const string IMAGE_BASE = "c:\\data\\pics\\";
    ImageShape ola(Point(WIDTH/2, HEIGHT/2), IMAGE_BASE + "olabouncesmall.jpg");
    ImageShape mjs(Point(WIDTH/2, HEIGHT/2), IMAGE_BASE + "mjsbouncesmall.jpg");
    
    display.SetTitle("back and forth bouncer demo");
    
    int numFish = PromptRange("how many bouncers: ",1,100);
    int k;
    for(k=0; k < numFish; k++)
    {   
        Point p(WIDTH/2, HEIGHT/2);
        double angle = deg2rad(rgen.RandInt(-90,90));
        
        Shape * left = ola.clone();
        left->setLocation(p);
        Shape * right = mjs.clone();
        right->setLocation(p);  
        BackForthBouncer bfb(left, right, angle, rgen.RandInt(1,2)); 
        display.addShape(bfb);
        
        angle = deg2rad(rgen.RandInt(90,270));
        BackForthBouncer bfb2(left,right, angle, rgen.RandInt(1,2));
        display.addShape(bfb2);
    }
    display.runUntilEscape(10);
    
    return 0;
    
    
}

⌨️ 快捷键说明

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