how to calculate centroid of an arraylist of points

给你一囗甜甜゛ 提交于 2019-12-01 04:11:21

问题


I am trying to add up all the x and y coordiantes respectively from points of ArrayList.

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



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

How do I find the centroid ??


回答1:


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());
}



回答2:


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;

}



来源:https://stackoverflow.com/questions/18591964/how-to-calculate-centroid-of-an-arraylist-of-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!