What's the best way to calculate a 3D (or n-D) centroid?

后端 未结 8 476
萌比男神i
萌比男神i 2021-02-04 00:35

As part of a project at work I have to calculate the centroid of a set of points in 3D space. Right now I\'m doing it in a way that seems simple but naive -- by taking the avera

8条回答
  •  孤独总比滥情好
    2021-02-04 00:55

    Potentially more efficient: if you're calculating this multiple times, you can speed this up quite a bit by keeping two standing variables

    N  # number of points
    sums = dict(x=0,y=0,z=0)  # sums of the locations for each point
    

    then changing N and sums whenever points are created or destroyed. This changes things from O(N) to O(1) for calculations at the cost of more work every time a point is created, moves, or is destroyed.

提交回复
热议问题