计算android.graphics.path值的质心,并找到质心的相对位置w.r.t路径

时间:2012-03-17 17:50:34

标签: android centroid

如果我有ArrayListPath值,如何计算由创建的Path值生成的数字的质心?然后,我如何找到质心的相对位置w.r.t由Path生成的图形。需要指导或帮助。谢谢! : - )

1 个答案:

答案 0 :(得分:2)

也许这会对你有所帮助(来自Android的来源) C:\adt-bundle-windows\sdk\sources\android-17\android\gesture\GestureUtils.java):

/**
 * Calculates the centroid of a set of points.
 * 
 * @param points the points in the form of [x1, y1, x2, y2, ..., xn, yn]
 * @return the centroid
 */
static float[] computeCentroid(float[] points) {
    float centerX = 0;
    float centerY = 0;
    int count = points.length;
    for (int i = 0; i < count; i++) {
        centerX += points[i];
        i++;
        centerY += points[i];
    }
    float[] center = new float[2];
    center[0] = 2 * centerX / count;
    center[1] = 2 * centerY / count;

    return center;
}