I have an svg file with multiple levels of hierarchy. <g>
within <g>
, and different elements such as <polygon>
, <polyline>
, etc within each group. My goal is to create a function that will return me all children elements as RShape
s in a simple single array format.
I think I will need to do this recursively by creating a function:
RShape[] getAllChildren(RShape shp) { }
and check whether shp
has any children, and if so, run the function recursively until there’s no child left to find:
int numChildren = shp.countChildren();
if (numChildren != 0) {
// run getAllChildren() on each child
} else {
// return RShape
}
Then, return each shape one level up, store them all in a single array and return it. It sort of makes sense to me conceptually but I’m having difficulty implementing it.
I know that there are some recursion examples like drawing a tree, but I feel just running functions like line()
recursively and having to return some data and track them like I’m trying here are somewhat different.
If you have any example or suggestions, I’d appreciate it.