- Home /
Question by
eleonoraseu · Nov 22, 2017 at 01:42 PM ·
arrayvector3maximumminimum
Max and min Vector3 in array
Hi guys, I have a Vector3 array and I need to find the vectors with max and min magnitude. What is the easiest way to do it? Thanks,Hi guys, I have a Vector3 array and I need to find the Vectors with min and max magnitude. What is the easiest way? Thanks
Comment
Answer by Dragate · Nov 22, 2017 at 02:09 PM
Initialise your max vector so it has lowest magnitude possible and your min vector to have the largest magnitude possible. Iterate through some array containing your vector3s. For each vector3 compare their magnitudes. If it is smaller than previous min, it will assign the new vector3, otherwise it will assign the previous min vector.
Vector3 minVector = Vector3.positiveInfinity;
Vector3 maxVector = Vector3.zero;
for(int i = 0; i < someArray.Length; i++){
minVector = (someArray[i].magnitude < minVector.magnitude) ? someArray[i] : minVector;
maxVector = (someArray[i].magnitude > maxVector.magnitude) ? someArray[i] : maxVector;
}