Question

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 ??

Was it helpful?

Solution

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

OTHER TIPS

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;

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top