Min max of Vector component

I hav an array of 2d vectors. I am trying to find a way to return the largest or smallest value from a dimension of the array.

For example I might want the smallest x value in the array or the largest y value. I don’t think the math functions can do this. Any ideas?

Thanks

Vector3 smallestVector = new Vector3(float.MaxValue, 0, 0); //or MinValue for largest value

foreach(Vector3 vector in YourVectorList)
{
	if (vector.x < smallestVector.x) //or > for largest value
	{
		smallestVector = vector;
	}
}

Rinse, repeat for Y/Z or swap the </> and MaxValue/MinValue.

EDIT: forgot you’re using 2D vectors. But you get the drift.

1 Like

Thanks, I’ll try this. It would be cool is there was a built in function like myvector.smallest(x); Oh well.

Andrew

You can always roll your own method and/or Math class!

Often times, frameworks (such as Unity and .NET/Mono) supply the most commonly used methods and leave special cases (such as these) up to the developer to implement.