Get Number of Path Coordinates


#1

Hi all,

Couldn't find this question after a quick search, so apologies if it's already been answered.

Is there a way to get the number of Path elements that a given Path uses? Specifically, I'm wondering if there's a function that returns the argument needed for Path::preallocateSpace(). Something like:

Path mypath;

mypath.preallocateSpace (anotherPath.getNumElements() );

I'm working on making special Path transforms (transforming each point in a Path differently depending on some context) for which the AffineTransform class is a bit clunky.

 

Any thoughts are appreciated. Thanks,

Elliot


#2

numElements is a private member… perhaps you can use Path::Iterator to calculate the number of elements.

Rail


#3

Thanks for your response, Rail. That does work; here's the code I put together for finding the number of elements in a path using Path::Iterator.

int getSizeOfPathType (Path::Iterator::PathElementType t)
{
    switch (t)
    {
        case (Path::Iterator::startNewSubPath):
            return 3;
        case (Path::Iterator::lineTo):
            return 3;
        case (Path::Iterator::quadraticTo):
            return 5;
        case (Path::Iterator::cubicTo):
            return 7;
        case (Path::Iterator::closePath):
            return 1;
        default:
            return 0;
    }
}

int getNumElements (Path p)
{
    Path::Iterator i (p);
    int numElements = 0;
    while (i.next() )
    {
        numElements += getSizeOfPathType (i.elementType);
    }
    
    return numElements;
}

Still, a Path function that simply returns Path::numElements could be helpful and much faster than the code above. Just a suggestion.

 

Happy coding,

Elliot