TITLE: Finding the Centre of an Abritary Set of Points in Two Dimensions
QUESTION [10 upvotes]: I am currently working on a program that needs to transform one set of coordinates by shifting them to the center of the screen. The points are offset from the middle of the screen - either to the left or to the right. The coordinates of the points are first presented within a rectangular bound. In the next step I need to figure out how to move them to the center of the screen. The number of points vary from $2$ to $5$ points.
How can I...

Find the center of a finite set of points?
Translate the positions to a new set of coordinates centered around a new center?

REPLY [10 votes]: You can use the centroid of the points. Imagine you have a set $S$ of $n$ points (in blue in the picture below)
$$ S=\{(x_1,y_1),(x_2,y_2),\dots (x_n,y_n)\}$$
Their centroid is given by:
$$(\bar x,\bar y) = \left(\frac{1}{n}\sum_{i=0}^n x_i, \frac{1}{n}\sum_{i=0}^n y_i\right).$$
Then, substracting those coordinates to your points
$$ S_{(0,0)}=\{(x_1-\bar x,y_1-\bar y),(x_2-\bar x,y_2-\bar y),\dots (x_n-\bar x,y_n-\bar y)\}$$
you will move them towards the origin (in red in the picture below). If you want to translate them to a different point (say, towards point $(a,b)$), then you just need to sum those coordinates to the ones above (in green in the picture below):
$$ S_{(a,b)}=\{(x_1-\bar x+a,y_1-\bar y+b),(x_2-\bar x+a,y_2-\bar y+b),\dots (x_n-\bar x+a,y_n-\bar y+b)\}$$

Of course, your program does not need to do both steps separately. You can just apply the last one directly.