Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to translate a C tree structure to ruby tree objects

Reply
Thread Tools

How to translate a C tree structure to ruby tree objects

 
 
anne001
Guest
Posts: n/a
 
      02-18-2006
I want to draw a figure with opengl. The key seems to start from a tree
structure.
The top of the tree is the trunk, there are several childrens, head,
upper arms upper legs, each of these have children of their own...

I found a tutorial which explains

Information is stored in nodes which include
pointer to a function that draws the object
matrix which positions, scales and orient this node relative to parent
(including children)
pointer to children of node

typedef struct treenode {
GLfloat m[16]; //transformation - e.g. translation
void *f(); //figure - e.g. Left leg
struct treenode *child;
struct treenode *parent;
} treenode;

What is the OOP ruby way of building a tree? Can it be something like
this? (per Brian Schröder)

class Node
attr_reader :children

def initialize
@children = []
end
end

what goes in @children? the transformation and drawing would be Node
methods I think?
And how do you refer to parent (You need information on the parent to
know where to draw the node)
What do I do with my class Node (called traversing a tree? )

I saw http://raa.ruby-lang.org/search.rhtml?search=tree
that trees are something people think about in ruby. Do any of those
reference have a bearing on my question?

 
Reply With Quote
 
 
 
 
matt.smillie@gmail.com
Guest
Posts: n/a
 
      02-18-2006
The children of a node go in @children. They're probably other nodes.

The transformation matrix and function would likely be data members
holding, respectively, a matrix and a proc object. An alternate
approach for the drawing function would be to make an instance method
that receives a block (where the block would contain the drawing
function).

So, potentially something like this:

class Node
attr_accessor :children, arent, :transform
def initialize(parent = nil, transform = some_sort_of _matrix_object)
@children = []
@transform = transform
if parent then parent.add_child(self) end
end

def add_child(node)
@children.push(node)
node.parent = self
end

def draw(&block)
yield
end
end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 2 10-31-2007 02:58 AM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 1 10-30-2007 11:01 PM
Hi, I want to implement a General Tree Data structure (Not Binary Tree ) which have more than 2 sub nodes? sharan C Programming 4 10-30-2007 08:21 PM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM



Advertisments