如何计算点的arylylist的质心

use*_*101 4 java arraylist points centroid

我试图分别从ArrayList的点添加所有x和y坐标.

public static ArrayList knots = new ArrayList<Point>();



public Point centroid()  {
        Point center = new Point();
            for(int i=0; i<knots.size(); i++) {
            ????????????????????
        return center;
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能找到质心?

Phi*_*der 20

public Point centroid()  {
    double centroidX = 0, centroidY = 0;

        for(Point knot : knots) {
            centroidX += knot.getX();
            centroidY += knot.getY();
        }
    return new Point(centroidX / knots.size(), centroidY / knots.size());
}
Run Code Online (Sandbox Code Playgroud)

  • 好吧,我必须同意mvn. (3认同)