How to find the Centroid of Set of Points

I just started learning python scripting and trying to work on relatively easy stuff to practice. So, I imported some points from rhino and I want to find the centroid of the points. I can probably do that in grasshopper in less than 10 seconds but that would defeat the purpose of this. So, I think I brought the points into python by adding “ptList = ”, and what would be the next step here? Do I go with something like finding the location of the points first or would I draw a line between the points and find the centroid of that shape? And also what kind of “commands” I would use for that?

Any help is appreciated!

@Cape_Olive,

below is a (hopefully comprehensible) python example. Usually you create the sum of all point coordinates, then divide it by the number of all points to get one average point:

import rhinoscriptsyntax as rs
    
def FindCentroidOfPoints():
    
    ids = rs.GetObjects("Select points", rs.filter.point, True, True, False)
    if not ids: return
    
    # first point
    pt_sum = rs.PointCoordinates(ids[0])
    
    # sum rest of points
    for i in xrange(1, len(ids)):
        pt_sum += rs.PointCoordinates(ids[i])
    
    # divide with amount of points to get average
    pt_sum = pt_sum / len(ids)
    
    rs.AddTextDot("A", pt_sum)
    
if __name__=="__main__":
    FindCentroidOfPoints()

There are various more cryptic ways to do it, but above works ok for a first start.

c.

2 Likes

I’ll try to use this and understand the steps, thank you very much!