I would like to get the center point(x,y) of a figure created by a set of points.
How do I do this?
Mathwords: Centroid Formula. The coordinates of the centroid of a triangle are found by averaging the x- and y-coordinates of the vertices. This method will also find the centroid (center of mass) of any set of points on the x-y plane.
I assume that a point is a tuple like (x,y), so you can use zip to join the x's and y's. Then using the min and max of x and y's, you can determine the center point. x,y=zip(*points) center=(max(x)+min(x))/2., (max(y)+min(y))/2.
The training data is split into groups by class label, then the centroid for each group of data is calculated. Each centroid is simply the mean value of each of the input variables. If there are two classes, then two centroids or points are calculated; three classes give three centroids, and so on.
p1-> left top corner, p2-> left bottom corner, p3-> right bottom corner, p4-> right top corner as I believe is in Your case, You can treat the center of the bounding box as the intersection of the diagonals. You need to find equations for lines that diagonals are lying on: Y1 = a1*X1+b1; Y2 = a2*X2+b2 .
If you mean centroid, you just get the average of all the points.
x = [p[0] for p in points] y = [p[1] for p in points] centroid = (sum(x) / len(points), sum(y) / len(points))
If the set of points is a numpy array positions
of sizes N x 2, then the centroid is simply given by:
centroid = positions.mean(axis=0)
It will directly give you the 2 coordinates a a numpy array.
In general, numpy arrays can be used for all these measures in a vectorized way, which is compact and very quick compared to for
loops.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With