Need help using Arrays ASAP

Some how I need to set up a data array using simple graphics functions for simple object Circles, Boxes, Triangles
That is three numbers for circles, four numbers for rectangles, and six numbers for triangles. I will need one more number to specify a color.
I need a program where I can store the data needed to draw a bunch of objects needed to draw something, like maybe a robot. Some how I need to draw a robot for this.

I really have no idea how to set this up. I understand how arrays work but when comes to drawing thing its pretty complicated.

Here is with what I need to start with.

I really need quick help with some code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
index = 0;
for(object=0;object<NumObjects; object++) {
    objectType = data[index];
    objectColor = data[index+1];
    setBrush(objectColor);
    if(objectType == 1) { // circle
        x = data[index+2];
        y = data[index+3];
        radius = data[index+4];
        drawFilledCircle(x,y,radius);
        index = index + 5;
    }
    ...
}

I think that's an exercise in polymorphism. You clearly can't have an array of different length objects, but you can have an array of pointers to different kinds of objects.
Yes I do know that, that is exactly what I need. But how to code that so it works. That is where I am having problems.

1
2
3
4
5
6
myClass **ptr; //pointer of pointers of classes ;)

ptr = new *myClass[]; //create an array of pointers to myclass

for(i = 0; i < size; i++)
   myClass[i] = new myClass; //create myclass in each element of the array 
The idea is that you define a base class for your Circles, Boxes and Triangles. The actual methods that the base class defines will be things that you need to do to/with the objects.

For example, if you need to calculate area and circumference, you'd define these as methods on the base class. Or if you want to draw them, you'd define a draw methods on the base class.

At any rate, you define the vector to hold pointers to the base class and you pass in pointers to the actual (concrete) derived classes.
I understand that but. But I am just can figure out what the code would be for the circles, boxes, and triangles.

That is where I am having a hard time.
Mmm, looking back at your original post I can see that your shape has:
1. type (circle)
2. position (x, y, radius)
3. colour
4. circle is drawn with drawnFilledCircle

I'll assume you have several shapes and you just draw them. I'll ignore the colour, you can add that. Here's the code declaration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Shape
{
public:
    virtual ~Shape() {}
    virtual void Draw() {}
};

class Circle : publc Shape
{
public:
    Circle(int x, int y, int radius) : x(x), y(y), radius(radius) {}
    virtual void Draw() { drawFilledCircle(x, y, radius); }

private:
    int x, y, radius;
};


Put some shapes in a vector:
1
2
3
4
5
6
typedef std::vector<Shape*> Shapes;
Shapes shapes;

shapes.push_back(new Circle(100, 100, 20));
shapes.push_back(new Square(30, 30));
// ... 


Draw the shapes:
1
2
3
4
5
6
7
void Draw(Shapes &shapes)
{
    for (size_t i = 0; i != shapes.size(); ++i)
    {
        shapes[i]->Draw();
    }
}


... well that's the idea.
void Draw(Shapes &shapes)

I assume you meant void Draw(Shapes* shapes)
if you use it as an array, it must be a pointer, not a reference!
if you use it as an array, it must be a pointer, not a reference!
No, I wrote what I meant.
Last edited on
Topic archived. No new replies allowed.