# How do I find the farthest verts of a mesh relative to its object position and store them in an array

Hello , I am writing an editor script that finds all the verts in a mesh, calculate their distance from the object the mesh is on then determine which verts on the x, y and z axis are the farthest away from the objects transform.positions x, y and z axis respectively.

In the code below ,Right after I have calculated the distance of all the verts of the mesh from the object with the mesh I am having trouble finding the farthest verts on the x ,y and z axis and assigning them to an array.

what would be the best way to get this done?

any help is appreciated .

`````` private Vector3 MeshPosition;
private Vector3[] Verts, Farthest;
private  Vector3 VertPosition()
{
Verts = Object_With_Mesh.GetComponent<MeshFilter>().sharedMesh.vertices;
if (MeshPosition == new Vector3())
{
try
{
MeshPosition = Object_With_Mesh.GetComponent<Transform>().transform.position;
}
catch
{

}
}
int VertCountPosition = 0;

foreach (Vector3 Vert in Verts)
{
float distance = Mathf.Infinity;
Vector3 diff = Vert - MeshPosition;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{

Farthest[VertCountPosition] = Vert;
VertCountPosition += 1;
Debug.Log(Farthest[VertCountPosition]);
distance = curDistance;
}
}
return Farthest[VertCountPosition];

}
``````

Just copied my function for sorting an array of Vector3’s by their distance from a specified origin. You can adapt it to your situation pretty easily.

``````public static void SortDistances( ref Vector3[] positions, Vector3 origin ) {
float[] distances = new float[ positions.Length ];
for (int i = 0; i < positions.Length; i++) {
distances _= (positions *- origin).sqrMagnitude;*_
``````

* } *
* System.Array.Sort( distances, positions ); *
* } *