# Less than, greater than 3d vectors

This topic is 4874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've got a Vector3d class
class Vector3d
{
float x,
y,
z;
};


So, I was wondering, for a two 3d vectors how would you determine if one was greater than or less than the other (assuming that the vectors are simply 2 positions in 3d space). I was thinking something like this:
bool Vector3d::operator<(const Vector3d& v)
{
return ( (x < v.x) || (y < v.y) || (z < v.z) );
}

bool Vector3d::operator>(const Vector3d& v)
{
return ( (x > v.x) || (y > v.y) || (z > v.z) );
}


I know this was in C++, but it was the easiest way to think of it. Is this right? Or is there no real way to figure this out?

##### Share on other sites
a=(1, 0, 0) and b=(0, 1, 0)

Then a < b and b < a, but a!=b. Sounds weird ?

Better use something else to decide which vector is greater than the other, e.g. by comparing the norm of each vector.

So in example:
bool Vector3d::operator<(const Vector3d& v)
{
return this->length() < v.length();
}

bool Vector3d::operator>(const Vector3d& v)
{
return this->length() > v.length();
}

##### Share on other sites
Vectors are unordered.

You can order them by length but then !(a<b) && !(b<a) doesn't imply a.x==b.x && a.y==b.y && a.z==b.z, which usually is meant by a==b

##### Share on other sites
depends on your sorting criteria. soring by what? X values? Y Values? Random axis? Distance from the origin? Sorting vector doesn't make much sense as it is. The best you can do is an '== operator'.

##### Share on other sites
Sometimes you need just any order (to use them as keys of an std::map, for instance). Then you can use the lexicographical order:
bool Vector3d::operator<(const Vector3d& v){  if ( x < v.x )    return true;  if ( x > v.x )    return false;  if ( y < v.y )    return true;  if ( y > v.y )    return false;  return (z < v.z );}

##### Share on other sites
Another vote for lexicographical order. That's the only meaningful definition of < for vectors that I'm aware of, and it can come in handy for certain algorithms.

##### Share on other sites
in world's coords. what do you think of operator > to be? shorter? longer? heigher? lower? etc...

I think the most required comparison is the length... Yet you don't have to really calculate the length precisly; you can compare the length squared to save some processing for other things.

length_sqr = x^2 + y^2 + z^2

in case you were comparing higher? then I suggest you keep it where you wanna compare it by doing this... a.z > b.z ... wont bother by a non understanable a > b... which to many ppl means length checking...

##### Share on other sites
IMO using < for vector length would be a bit of operator overloading abuse (kind of like % or ^ for cross product). I suppose the same could be said for lexicographical sort, but at least there's some precedent for that (I think).

If you want to use a std::container of some sort, I think many stl functions will take a comparison function object rather than using < (I'd have to check on this). So it might be more clear to have different functions (CompareX(), CompareY(), CompareZ(), CompareLength(), Lexicographical()) and submit one of them as appropriate. Sorting vectors by any one of those criteria could be useful under certain circumstances.

##### Share on other sites
just was thinking "operator overload abuse" when I saw jyk posting.
I totally agree, defining > and < on vectors is too much of it.

##### Share on other sites
It only makes sense if you consider the vectors as packed real values, then it returns packed booleans (3 answers in your case), which is what SIMD (MMX, SSE, 3DNow, Altivec) instructions trivially do. In the case of a 3D vector, term by term comparison just means three comparisons in parallel nothing more. And this makes much more sense in practice than one could imagine. For instance fast computations involving bounding boxes.

• 11
• 20
• 12
• 10
• 38
• ### Forum Statistics

• Total Topics
631400
• Total Posts
2999862
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!