#include <stdio.h>
#include <stdlib.h>
#include <time.h>

enum { rand_limit = 10 };

class GraphObject {
protected:
    double x, y;
    int color;
public:
    GraphObject(double ax, double ay, int acolor)
        : x(ax), y(ay), color(acolor) {}
    virtual ~GraphObject() {}
    virtual void Show() = 0;
    virtual void Hide() = 0;
    void Move(double nx, double ny);
};

void GraphObject::Move(double nx, double ny)
{
    Hide();
    printf("move graph object\n");
    x = nx;
    y = ny;
    Show();
}

class Pixel : public GraphObject {
public:
    Pixel(double x, double y, int color)
        : GraphObject(x, y, color) {}
    virtual ~Pixel() {}
    virtual void Show() { printf("hide pixel\n"); }
    virtual void Hide() { printf("show pixel\n"); }
};

class Circle : public GraphObject {
    double radius;
public:
    Circle(double x, double y, double rad, int color)
        : GraphObject(x, y, color), radius(rad) {}
    virtual ~Circle() {}
    virtual void Show() { printf("hide circle\n"); }
    virtual void Hide() { printf("show circle\n"); }
};


class PolygonalChain : public GraphObject {
    struct Vertex {
        double dx, dy;
        Vertex *next;
    };
    Vertex *first;
public:
    PolygonalChain(double x, double y, int color)
        : GraphObject(x, y, color), first(0) {}
    virtual ~PolygonalChain();
    void AddVertex(double adx, double ady);
    virtual void Show() { printf("hide polygonal chain\n"); }
    virtual void Hide() { printf("show polygonal chain\n"); }
};

void PolygonalChain::AddVertex(double adx, double ady)
{
    Vertex *tmp = new Vertex;
    tmp->dx = adx;
    tmp->dy = ady;
    tmp->next = first;
    first = tmp;
}

PolygonalChain::~PolygonalChain()
{
    while (first) {
        Vertex *tmp = first;
        first = first->next;
        delete tmp;
    }

}

class Square : public PolygonalChain {
public:
    Square(double x, double y, int color, double a)
        : PolygonalChain(x, y, color)
    {
        AddVertex(0, 0);
        AddVertex(0, a);
        AddVertex(a, a);
        AddVertex(a, 0);
        AddVertex(0, 0);
    }
};

int random_number(int limit)
{
    return 1.0 * limit * rand() / (RAND_MAX + 1.0);
}

int main()
{
    int i;
    srand(time(NULL));

    int scene_length = 3 + random_number(rand_limit);
    GraphObject **scene;

    scene = new GraphObject*[scene_length];

    scene[0] = new Pixel(1.25, 15.75, 0xff0000);
    scene[1] = new Circle(20.9, 7.25, 0x005500, 3.5);
    scene[2] = new Square(55.0, 30.5, 0x008080, 10.0);
    for (i = 3; i < scene_length; i++)
        scene[i] = new Circle(i, i, 0x0000ff, 1);

    // Square *sp = (Square*) scene[3];
    Square *sp = static_cast<Square*>(scene[3]);

    for (i = 0; i < scene_length; i++)
        delete scene[i];
    delete[] scene;

    return 0;
}
