0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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/… \$\endgroup\$ – lyinch May 22 '17 at 7:44
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – user1118321 Jul 9 '17 at 2:24
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Aug 12 '17 at 13:04
1
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ This is the most intuitive answer. \$\endgroup\$ – S. Tarık Çetin Sep 15 '17 at 21:03
0
\$\begingroup\$

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.

\$\endgroup\$
-1
\$\begingroup\$

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];
}
\$\endgroup\$
  • \$\begingroup\$ Are you sure that the > operator is defined in the Vector3 class in opentk? \$\endgroup\$ – lyinch May 22 '17 at 8:51
  • \$\begingroup\$ @lyinch I don't know, but he said that there's a method doing that \$\endgroup\$ – Bálint May 22 '17 at 8:57
  • \$\begingroup\$ > is not defined. He is referring to the Vector3 ComponentMax(Vector3 a, Vector3 b) method. \$\endgroup\$ – lyinch May 22 '17 at 9:07
  • \$\begingroup\$ @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 \$\endgroup\$ – Bálint May 22 '17 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy