// returns null if unknown static DoublePt bwCentroid2(BWImage img, bool dark) { int w = img.getWidth(), h = img.getHeight(); double sumX = 0, sumY = 0, sum = 0; for y to h: for x to w: { float weight = img.getPixel(x, y); if (dark) weight = 1-weight; sumX += weight*x; sumY += weight*y; sum += weight; } if (sum == 0) null; ret DoublePt(sumX/sum, sumY/sum); }