Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate centroid of an arraylist of points

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

like image 222
user2398101 Avatar asked Sep 03 '13 12:09

user2398101


People also ask

How do you find the centroid of N points?

To find the centroid, follow these steps: Step 1: Identify the coordinates of each vertex. Step 2: Add all the x values from the three vertices coordinates and divide by 3. Step 3: Add all the y values from the three vertices coordinates and divide by 3.

What is centroid point in graph?

In Mathematics, the centroid defines the geometric centre of a two-dimensional plane surface. It is a point that is located from the arithmetic mean position of all the points on the plane surface. Otherwise, it is defined as the average of all the points in the plane figure.


1 Answers

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());
}
like image 145
Philipp Sander Avatar answered Oct 08 '22 23:10

Philipp Sander