The largest vector in an array

I'm working on a game engine in C# at the moment and I've come across a matter I find confusing. I need to know how to get the maximum three component vector in an array.

There is a method in OpenTK that can compare two Vector3s to find out the max, I need this on a larger scale.

I've tried using Array.Max(), but I get the error :

At least one object must implement IComparable

• You can extend the Vector3 class and implement the interface IComparable, this let's you use every generic method which requires IComparable to be implemented, such as Sort() Max(), ... msdn.microsoft.com/en-us/library/… – lyinch May 22 '17 at 7:44
• Are you trying to compare the length of the vectors? It's unclear from your question. Vectors are not normally orderable unless you define a function of their components, such as length. – user1118321 Jul 9 '17 at 2:24
• Another possible interpretation is that they want a single vector whose x component is the maximum of all x components in the array, whose y component is the maximum of all y components in the array, etc. One might use this, for instance, for computing the corners of an axis-aligned bounding box around a set of points. Without more explanation though, it's impossible to tell what the intended result is. – DMGregory Aug 12 '17 at 13:04

Linq method of solving this would be using MaxBy coupled with a method mapping vector to a scalar type, e.g. LengthSquared or ComponentMax which gives float,

var resVec = array.MaxBy(vec => vec.LengthSquared);

• This is the most intuitive answer. – S. Tarık Çetin Sep 15 '17 at 21:03

Having a look at the Vector3 class of monotk gives us a hint why this doesn't work. Classes such as Sort() and Max() require your datastructure to implement the IComparable interface, which requires the CompareTo() method.

With this method you define a new binary relation. You can implement a lexicographical relation (aa > ab (this only works because the characters are ordinal) ) for strings, use distances if your class is a topological graph,...

Comparing two vectors ( dimension > 1) isn't so straight-forward. Which vector is greater: (100,10) or (10,100) or (67,43)? To compare vectors in a metric space, you have to rely on a norm. You might however want to create a different ordering, such as: First compare the Y coordinate, then the X coordinate. This gives us (for points) all the points sorted row by row.

As you can see, it is not trivial to compare vectors, and different use cases need a different definition of "the greatest of two vectors". The trivial case, where you want to know if two vectors are equal, is implemented (overloading the == and != operator).

Now what? You can extend the Vector3 class and implement the IComparable interface to be able to use Array.Max(). Or you can simply create a loop and compare your vectors on your own, and define what you want as "max". You can compare their norms, or use the implemented ComponentMax() functions, which compares the vector by, you guessed it, components.

All in all, once you defined "your maximum vector", the implementation is straight-forward.

You can do this in linear time:

Vector3 best = yourArray[0];

for (int i = 1; i < yourArray.Length; i++)
{
if (yourArray[i] > best)
best = yourArray[i];
}

• Are you sure that the > operator is defined in the Vector3 class in opentk? – lyinch May 22 '17 at 8:51
• @lyinch I don't know, but he said that there's a method doing that – Bálint May 22 '17 at 8:57
• > is not defined. He is referring to the Vector3 ComponentMax(Vector3 a, Vector3 b) method. – lyinch May 22 '17 at 9:07
• @lyinch I didn't give him an exact code to use, I gave him the basic way to solve this problem, if he knows anything about programming, he'll be able to change it according to it – Bálint May 22 '17 at 9:53