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

📄 shapes.h

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 H
字号:
#ifndef _SHAPE_H
#define _SHAPE_H


#include "canvas.h"

// Abstract Base class for all shapes
// 


class Shape
{
    public:
      Shape();
      virtual ~Shape() {} 
        
      virtual void draw(AnimatedCanvas& c)     = 0; 
      virtual void setLocation(const Point& p) = 0;
      virtual Shape * clone();
      
      int            id  ()        const {return myCount;}
      virtual Point  getLocation() const = 0; 
      bool           contains(const Point& p) const;
      bool           overlaps(const Shape& s) const;
      
      virtual Box    bbox()        const = 0;
      virtual string tostring()    const;
        
    protected:
  
      static int ourCount;
      int        myCount;
};

class CircleShape : public Shape
{
  public:
    CircleShape(const Point& p, double r, color c);
    CircleShape(const CircleShape& c);
    virtual void draw(AnimatedCanvas& c);
    
    virtual void  setLocation(const Point& p);
    virtual Point getLocation()                const;
    
    virtual Box  bbox() const;
    virtual string tostring() const;
    virtual Shape* clone();
  private:
    
    Point myPoint;
    double myRadius;
    color myColor;
      
      
};

class EllipseShape : public Shape
{
  public:
    EllipseShape(const Point& ul, const Point& lr, color c);
    virtual void draw(AnimatedCanvas& c);
    virtual void setLocation(const Point& p);
    virtual Point getLocation() const;
    virtual Box bbox() const;
    virtual string tostring() const;
    
    virtual Shape* clone();
    
  private:
    Point myPoint;
    Box myBox;
    color myColor;
};

class RectangleShape : public Shape
{
  public:
    RectangleShape(const Point& p, double w, double h, color c);
    virtual void draw(AnimatedCanvas& c);
    virtual Box bbox() const;
    virtual Shape* clone();
    
    virtual void  setLocation(const Point& p);
    virtual Point getLocation()                const;
    virtual string tostring()                  const;
    
  private:
    Point myPoint;
    double myWidth;
    double myHeight;
    color myColor;
    
};

class PolygonShape : public Shape
{
  public:
    PolygonShape(const tvector<Point>& a, int numPoints, color c);
    
    virtual void draw(AnimatedCanvas& c);
    virtual Box  bbox() const;
    virtual Shape* clone();
    virtual void setLocation(const Point& p);
    virtual Point getLocation() const;
    virtual string tostring() const;
    
  private:
   
    tvector<double> myXdeltas;
    tvector<double> myYdeltas;
    Point myMin;
    Point myPoint;
    Box   myBox;
    color myColor;
  
};

class TriangleShape : public Shape
{
  public:
    TriangleShape(const Point& p1, const Point& p2, const Point& p3,
                  color c);
    
    TriangleShape(const TriangleShape& ts);
    const TriangleShape& operator =(const TriangleShape& ts);   
    virtual ~TriangleShape();
                  
    virtual void draw(AnimatedCanvas& c);
    virtual void setLocation(const Point& p);
    virtual Point getLocation() const;
    virtual Box  bbox() const;
    virtual string tostring() const;
    virtual Shape* clone();
    
  private:
    PolygonShape * myShape;
};

class TextShape : public Shape
{
  public:
    TextShape(const Point& p, const string& s, color c);
    
    void changeText(const string& s);
    virtual void draw(AnimatedCanvas& c);
    virtual Box bbox() const;
    virtual Shape* clone();
    
    virtual void  setLocation(const Point& p);
    virtual Point getLocation()                const;
    virtual string tostring() const;
    
  private:
  
    Point myPoint;
    string myString;
    color myColor;
    Box myBox;
    bool myInitialized;
};

class ImageShape : public Shape
{
  public:
    ImageShape(const Point& p, const string& filename, double scale=1.0);
    ImageShape(const ImageShape& is);
    virtual void draw(AnimatedCanvas& c);
    virtual Box  bbox() const;
    virtual Shape* clone();
    
    virtual void  setLocation(const Point& p);
    virtual Point getLocation()                const;
    virtual string tostring() const;
    
  private:
    Point  myPoint;
    image* myImage;
    double myScale;
};

class CompositeShape : public Shape
{
  public:
    CompositeShape();  // construct, then call add
    
    virtual void draw(AnimatedCanvas& c);
    virtual Box  bbox() const;
    
    virtual void add(Shape& sh);
    virtual void add(Shape* sh);
    virtual void setLocation(const Point& p);
    virtual Point getLocation() const;
    virtual Shape* clone();
    virtual string tostring()  const;
    
  protected:
  
    Point myPoint;
    tvector<Shape *> myShapes;
    Box myBox;
};

class Mouseable : public CanvasAble
{
  public:
    virtual ~Mouseable() = 0;
    virtual void processClick(const Point& p, AnimatedCanvas& c) = 0;
};

class Keyable : public CanvasAble
{
  public:
    virtual ~Keyable() = 0;
    virtual void processKey(const Key& p, AnimatedCanvas& c) = 0;
};


class Bouncer : public Shape
{
  public:
  
	Bouncer(Shape *cs, double angle, double v);
    Bouncer(Shape& cs, double angle, double v);
    Bouncer(const Bouncer& b);
    
    
    double getAngle() const;
    double getVelocity() const;
    
    virtual void draw(AnimatedCanvas& c);
    virtual Shape* clone();
    
    virtual Box bbox() const;
  
    virtual void  setLocation(const Point& p);
    virtual Point getLocation()                const;
  
  protected:
  
    virtual void updatetop    (AnimatedCanvas& c, Point& p);
    virtual void updatebottom (AnimatedCanvas& c, Point& p);
    virtual void updateleft   (AnimatedCanvas& c, Point& p);
    virtual void updateright  (AnimatedCanvas& c, Point& p);
    
    virtual void update(AnimatedCanvas& c); // calls top, bottom, left, right
    
    Shape* myShape;
    double myAngle;
    double myVelocity;   // multiplicative factor for delta/slope
};


class Mover : public Shape
{
  public:
  
    Mover(Shape& cs);
    
    virtual void draw(AnimatedCanvas& c);
    virtual Box  bbox() const;
    
    virtual void moveTo(const Point& p);
    virtual void moveBy(double dx, double dy);
    virtual Shape * clone();
    
    virtual void  setLocation(const Point& p);
    virtual Point getLocation()                const;
    
  protected:
  
    Shape* myShape;
};

class EmptyShape : public Shape
{
    public:
      EmptyShape();
      virtual void draw(AnimatedCanvas& c);
      virtual Box bbox() const;
      Shape * clone();
        
      virtual void  setLocation(const Point& p);
      virtual Point getLocation()                const;
};

class MKAdapter : public EmptyShape, public Mouseable, public Keyable
{
  public:
    MKAdapter();
    virtual void processClick(const Point& p, AnimatedCanvas& c);
    virtual void processKey(const Key& key, AnimatedCanvas& c);
};

class PrintWatcher : public EmptyShape, public Keyable
{
  public:
    PrintWatcher(const Key& k);
    virtual void processKey(const Key& key, AnimatedCanvas& c);
  private:
    Key myKey;    
};

class Button : public Shape, public Mouseable
{
  public:
    Button(const string& label);
    Button(const string& label, const color& c);
    
    virtual void draw(AnimatedCanvas& c);
    virtual void setLocation(const Point& p);
    virtual Shape * clone();
    
    virtual Box    bbox() const;
    virtual Point  getLocation() const;
    
    virtual void processClick(const Point& p, AnimatedCanvas& c);
    
    virtual void execute(AnimatedCanvas& c);  // clients override this
    
  protected:    
    
    Shape * myShape;
    string  myLabel;
};

#endif

⌨️ 快捷键说明

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