5

我正在尝试分别从 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;
}

我如何找到质心?

4

2 回答 2

22
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());
}
于 2013-09-03T12:11:41.807 回答
0
public Point centroid()  {
    Point center = new Point();
        int sumofx=0,sumofy=0;
        for(int i=0; i<knots.size(); i++) {
        sumofx= sumofx+knot[i].x;
        sumofy=sumofy+knot[i].y;
        }
    center.x=sumofx/knots.size();
    center.y=sumofy/knots.size();
    return center;

}

于 2017-09-19T06:07:50.433 回答