// test_shape4.cpp all in one file, should really be split up #include "vt100_plot.h" // other plot libraries may be used // shape4.h #include using namespace std; class Shape; typedef Shape * Shape_ptr; typedef list Shape_list; class Component { friend Shape; public: Component(float X, float Y, Shape_ptr Ashape): TheX(X), TheY(Y), TheShape(Ashape){} private: float TheX; float TheY; Shape_ptr TheShape; }; typedef list Component_list; class Shape // modified for shape made up of lists of shapes { public: Shape(); // constructor ~Shape(); // destructor, mainly deletes linked list void SetCenter(float XCenter, float YCenter); float XCenter(); float YCenter(); void Move(float dx, float dy); // may also want MoveTo(X,Y) void AddComponent(float X, float Y, Shape_ptr Ashape); void AddSibling(Shape_ptr Ashape); virtual void Draw(float Xoffs, float Yoffs); // shapes implement protected: float TheXCenter; float TheYCenter; private: Component_list list_of_component_shapes; // used by recursive Draw Shape_list list_of_sibling_shapes; // used by recursive Move }; // shape4.cpp //#include "shape4.h" Shape::Shape() // body for constructor, default initialization { TheXCenter = 0; // make it legal the first time, keep it legal TheYCenter = 0; } Shape::~Shape() // body for destructor { } void Shape::SetCenter(float XCenter, float YCenter) { TheXCenter = XCenter; TheYCenter = YCenter; } float Shape::XCenter() { return TheXCenter; } float Shape::YCenter() { return TheYCenter; } void Shape::Move(float dx, float dy) { TheXCenter += dx; TheYCenter += dy; Shape_list::const_iterator p; for(p=list_of_sibling_shapes.begin(); p!=list_of_sibling_shapes.end(); p++) { (*p)->Move(dx, dy); } } void Shape::AddComponent(float X, float Y, Shape_ptr Ashape) { list_of_component_shapes.push_back( Component(X, Y, Ashape)); } void Shape::AddSibling(Shape_ptr Ashape) { list_of_sibling_shapes.push_back(Ashape); } void Shape::Draw(float Xoffs, float Yoffs) // draws sub-shapes { Component_list::const_iterator p; for(p=list_of_component_shapes.begin(); p!=list_of_component_shapes.end(); p++) { (p->TheShape)->Draw(TheXCenter+Xoffs+p->TheX, TheYCenter+Yoffs+p->TheY); } } // circle4.h //#include "shape4.h" class Circle : public Shape { public: Circle(float X, float Y, float R); void SetRadius(float Aradius); void Draw(float Xoffs, float Yoffs); float Radius(); private: float TheRadius; }; // circle4.cpp //#include "circle4.h" Circle::Circle(float X, float Y, float R) { SetCenter(X, Y); TheRadius = R; } void Circle::SetRadius(float Aradius) { TheRadius = Aradius; } void Circle::Draw(float Xoffs, float Yoffs) { DRAW_CIRCLE(TheXCenter+Xoffs, TheYCenter+Yoffs, TheRadius); } float Circle::Radius() { return TheRadius; } // rectangle4.h //#include "shape4.h" class Rectangle : public Shape { public: Rectangle(float X, float Y, float W, float H); void SetSize(float Awidth, float Aheight); void Draw(float Xoffs, float Yoffs); float Width(); float Height(); private: float TheWidth; float TheHeight; }; // rectangle4.cpp //#include "rectangle4.h" Rectangle::Rectangle(float X, float Y, float W, float H) { TheXCenter = X; TheYCenter = Y; TheWidth = W; TheHeight = H; } void Rectangle::SetSize(float Awidth, float Aheight) { TheWidth = Awidth; TheHeight = Aheight; } void Rectangle::Draw(float Xoffs, float Yoffs) { DRAW_RECTANGLE(TheXCenter+Xoffs, TheYCenter+Yoffs, TheWidth, TheHeight); } float Rectangle::Height() { return TheHeight; } float Rectangle::Width() { return TheWidth; } // line4.h //#include "shape4.h" class Line : public Shape { public: Line(float X1, float Y1, float X2, float Y2); void SetEnd(float X2, float Y2); void Draw(float Xoffs, float Yoffs); float X2(); float Y2(); private: float TheY2; float TheX2; }; // line4.cpp //#include "line4.h" Line::Line(float X1, float Y1, float X2, float Y2) { TheXCenter = X1; TheYCenter = Y1; TheX2 = X2; TheY2 = Y2; } void Line::SetEnd(float X2, float Y2) { TheX2 = X2; TheY2 = Y2; } void Line::Draw(float Xoffs, float Yoffs) { DRAW_LINE(TheXCenter+Xoffs, TheYCenter+Yoffs, TheX2+Xoffs, TheY2+Yoffs); } float Line::X2() { return TheX2; } float Line::Y2() { return TheY2; } // text4.h //#include "shape4.h" class Text : public Shape { public: Text(float X, float Y, char * msg); void Draw(float Xoffs, float Yoffs); private: char * Themsg; }; // text4.cpp //#include "text4.h" Text::Text(float X, float Y, char * msg) { TheXCenter = X; TheYCenter = Y; Themsg = msg; } void Text::Draw(float Xoffs, float Yoffs) { WRITE_TEXT(TheXCenter+Xoffs, TheYCenter+Yoffs, Themsg); } // test_shape4.cpp //#include "circle4.h" //#include "rectangle4.h" //#include "text4.h" int main() { Shape clock; Shape bunch; INITIALIZE(); Text label(-30,-8,"Just some crude clocks"); label.Draw(0,0); clock.SetCenter(0, 0); // redundant for 0,0 clock.AddComponent(0,0,new Circle(0,0,4)); clock.AddComponent(-1,3,new Text(0,0,"12")); clock.AddComponent(0,-3,new Text(0,0,"6")); clock.AddComponent(-3,0,new Text(0,0,"9")); clock.AddComponent(3,0,new Text(0,0,"3")); clock.Move(0,4); clock.Draw(0,0); clock.Draw(10,-4); bunch.AddComponent(-10,0,&clock); bunch.AddComponent(-10,0,new Line(0,4,2,6)); bunch.AddComponent(-20,-1,&clock); bunch.AddComponent(-20,-1,new Line(0,4,-2,6)); bunch.AddComponent(-30,-2,&clock); bunch.AddComponent(-30,-2,new Line(0,4,2,2)); bunch.Draw(0,-4); PRINT(); return 0; }