Logo Search packages:      
Sourcecode: inkscape version File versions

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

centroid using sbasis integration. 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.

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

Copyright Nathan Hurst 2006

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

                                                                                      {
    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