// shape.h // Definition of classes Point, Shape and Polyline. #ifndef SHAPE_H #define SHAPE_H #include using std::string; using std::stringstream; using std::ostream; #include // sqrt // class Point struct Point { double x, y; Point( double xx = 0.0, double yy = 0.0 ) : x( xx ), y( yy ) { } double distance( const Point& p2 ) const { double dx = x - p2.x, dy = y - p2.y; return sqrt( dx * dx + dy * dy ); } string toString() const { stringstream sstream; sstream << '(' << x << ", " << y << ')'; return sstream.str(); } Point& operator*=( double c ) { x *= c; y *= c; return *this; } }; inline Point operator+( const Point& p1, const Point& p2 ) { return Point( p1.x + p2.x, p1.y + p2.y ); } inline Point operator-( const Point& p1, const Point& p2 ) { return Point( p1.x - p2.x, p1.y - p2.y ); } inline bool operator==( const Point& p1, const Point& p2 ) { return p1.x == p2.x && p1.y == p2.y; } inline bool operator!=( const Point& p1, const Point& p2 ) { return !( p1 == p2 ); } inline ostream& operator<<( ostream& os, const Point& p ) { os << p.toString(); return os; } // class Shape class Shape { protected: Point anchor; // anchor: point of reference for position of figure public: Shape( const Point& a = Point() ) : anchor( a ) { } virtual ~Shape() {} Point getAnchor() const { return anchor; } void setAnchor( Point a ) { anchor = a; } void move( double dx, double dy ) // displacement { anchor.x += dx; anchor.y += dy; } virtual void scale( double scalingFactor ) = 0; virtual void draw() const = 0; virtual string toString() const { string str( "Shape-Anker: " ); return str += anchor.toString(); } }; // class Polyline class Polyline : public Shape { protected: Point* arrPoints; // end-points of lines, relativ to anchor int nLines; // number of line-segments == number of end-points public: Polyline( const Point& a = Point() ) // only one point : Shape( a ), nLines( 0 ), arrPoints( NULL ) { } Polyline( const Point& p1, const Point& p2 ) // one line : Shape( p1 ), nLines( 1 ) { arrPoints = new Point( p2 - p1 ); } Polyline( Point arr[], int n ); Polyline( const Polyline& src ); // copy constructor ~Polyline() { delete [] arrPoints; } // destructor Polyline& operator=( const Polyline& src ); // assignment int getNumberOfLines() const { return nLines; } double getLength() const; void scale( double scalingFactor ); // scaling virtual void draw() const { } Polyline& operator+=( const Point& p ); // appends a new point Polyline& operator+=( const Polyline& pl ); // appends a new line string toString() const; }; #endif