Sign in to follow this  
Endar

Less than, greater than 3d vectors

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 );
}
[/code]

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this