Find centermost vector3? Edges?

Given a `List<Vector3>` how can I find the center most vector(s)?

It would also be useful to be able to find the edge vectors.

My lists represent a flat grid (same Y axis all points) and is square.

Thank you

“centermost” could mean a few things.

``````if you're thinking of something like a center of mass, then two steps:
1) compute the center of mass by adding up all the points and dividing by the number of points.
2) iterate through your points and find the one/s closest to the center of mass.

if you're thinking of the points closest to the geometrical center of the square,
you'd probably be fine with these steps:
1) iterate through the points tracking the minimum and maximum X and Z values.
2) call the 'center' just (min + max) / 2 for X and Z.
3) iterate again finding the point/s closest to that center.
``````

I ended up with this

``````void findLevelCenter() { // finds the centermost vector3 of all objects in the level
List<Vector3> allPositions = new List<Vector3>();
GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
foreach (GameObject go in allObjects) {
allPositions.Add(go.transform.position);
};
Vector3 vectorsum = new Vector3();
int vectorcount = 0;
foreach (Vector3 v in allPositions) {
vectorsum += v;
vectorcount += 1;
}
Vector3 centerVector = vectorsum / vectorcount;
Debug.DrawRay(centerVector, new Vector3(0,1,0), Color.red, 1000f);
}
``````