Logo Search packages:      
Sourcecode: inkscape version File versions

unsigned Geom::centroid ( Piecewise< D2< SBasis > > const &  p,
Point centroid,
double &  area 
)

Centroid using sbasis integration.

Parameters:
p the Element.
centroid on return contains the centroid of the shape
area on return contains the signed area of the shape.
This approach uses green's theorem to compute the area and centroid using integrals. For curved shapes this is much faster than converting to polyline. Note that without an uncross operation the output is not the absolute area.

Returned values: 0 for normal execution; 2 if area is zero, meaning centroid is meaningless.

Definition at line 518 of file sbasis-geometric.cpp.

References Geom::D2< T >::at0(), and Geom::D2< T >::at1().

                                                                                      {
    Point centroid_tmp(0,0);
    double atmp = 0;
    for(unsigned i = 0; i < p.size(); i++) {
        SBasis curl = dot(p[i], rot90(derivative(p[i])));
        SBasis A = integral(curl);
        D2<SBasis> C = integral(multiply(curl, p[i]));
        atmp += A.at1() - A.at0();
        centroid_tmp += C.at1()- C.at0(); // first moment.
    }
// join ends
    centroid_tmp *= 2;
    Point final = p[p.size()-1].at1(), initial = p[0].at0();
    const double ai = cross(final, initial);
    atmp += ai;
    centroid_tmp += (final + initial)*ai; // first moment.
    
    area = atmp / 2;
    if (atmp != 0) {
        centroid = centroid_tmp / (3 * atmp);
        return 0;
    }
    return 2;
}


Generated by  Doxygen 1.6.0   Back to index