PDA

View Full Version : hierachies



doofus
04-15-2004, 02:36 AM
I'm trying to implement an object hierachy. I have no problem in creating it but then I need to draw it.

So i have my parent node that will point to its children and them children will point to their own children and so and so forth.

So I draw the parent and then the children, i thought of then going through the children making them the parent and then draw their children so the algorithm is recursive. But im having such a brain block and cant think of how to do that second stage, can anyone possible help??

I'm not the greatest programmer and so the answer is probably very simple. eugh head pain.

Cheers,

jebus
04-15-2004, 03:37 AM
let's say you have an object that is (loosely) defined like this:
class Object
{
Object *parent;
list<Object *> children;
};now you can recursively render (or do anything else) to every Object in the tree like this:
void recurse(Object *obj)
{
obj->render();
if (obj->children.size())
{
for (list<Object *>::itereator i = children.begin(); i != children.end(); i++)
{
recurse(*i);
}
}
}

doofus
04-15-2004, 04:48 AM
could this be translated into c by any chance??

jebus
04-15-2004, 05:23 AM
to make this work in C, just change the class to a struct, and implement your own linked list data structure.