Sign in to follow this  
klayAlloy

Writing a vector class

Recommended Posts

klayAlloy    150
If I wanted to code my own vector class, should it include methods such as cross product and dot product or should those functions be in a say a math library / class? CVector u; CVector v; u.Dot(v); vs. DotProduct(u, v);

Share this post


Link to post
Share on other sites
jjd    2140
I don't know if there is a right way or a wrong way to doing this. At work we use operator| for the dot product and operator^ for the cross product. I don't like the u.dot(v) syntax myself, but that is just personal preference.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by jjd
I don't know if there is a right way or a wrong way to doing this. At work we use operator| for the dot product and operator^ for the cross product. I don't like the u.dot(v) syntax myself, but that is just personal preference.
jjd is a pro and I'm not, so you should probably listen to him :-) However, I'd vote against overloading operators for these functions. If you want your code to be immediately and clearly understandable to anyone reading it, using | for the dot product is not going to contribute to that IMO.

As for dot(v1,v2) vs. v1.dot(v2), each has pros and cons. For example, the former can look better with expressions:
d = dot(v1-v2,v3-v4);
// vs.
d = (v1-v2).dot(v3-v4);
And so on. In my last library, I made both options available, with one simply wrapping a call to the other. In the end though, I've ended up just using the dot(v1,v2) syntax, which I prefer.

So my (non-professional) advice is:

1. No non-obvious overloaded operators (^, %, |, !, etc.)
2. Both member and non-member versions of functions like dot() and cross() if you're not sure which you prefer, or...
3. Non-member versions only if you just want to pick one or the other

Share this post


Link to post
Share on other sites
d00fus    328
As the others have said this really comes down to personal preference. However I also vote for not overloading operators to do this; I have seen code written using | and ^ and it is difficult to follow when you aren't used to it, particularly since these operators are basically arbitrary (well not quite arbitrary, they do have the correct precedence).

I use the same approach as jyk, defining both member and non-member versions and using whichever you prefer in a given situation. Ends up very readable for anyone.

Share this post


Link to post
Share on other sites
jjd    2140
Quote:
Original post by jyk
Quote:
Original post by jjd
I don't know if there is a right way or a wrong way to doing this. At work we use operator| for the dot product and operator^ for the cross product. I don't like the u.dot(v) syntax myself, but that is just personal preference.
I'd vote against overloading operators for these functions. If you want your code to be immediately and clearly understandable to anyone reading it, using | for the dot product is not going to contribute to that IMO.


Actually I agree with this entirely. I think using operator| is stupid (and operator^ for that matter) and I shouldn't have suggested it. I remember seeing it for the first some and wondering why the hell someone had performed a bitwise operation on two vectors...


Share this post


Link to post
Share on other sites
deavik    570
It's funny no-one suggested the '*' operator sice it is, after all a 'product'. Sucker that I am, for my first attempt at a vector class, I made '*' the dot-product and '%' the cross product (I somehow thought that if you kind of narrow your eyes and squint at the '%', it looks like a 'X' (for 'cross') [lol]).

But now I too, like jyk has already said, use dot(v1, v2) and cross(v1, v2).

Share this post


Link to post
Share on other sites
oliii    2196
I used to use '*' for dot product, and '^' for cross product, at work, and other purposes. Always be careful about overloading operators and their precedence (for example, '%').

if you want to avoid problems, use function names, or parenthesis, but operators can look neat. You can also implement the two if you feel like it.

Share this post


Link to post
Share on other sites
Dark_Nebula    221
Well, the | operator could make sense, since it's used in some (non-american) math textbooks to denote the dot (scalar) product.

However, I personally think operator overloading for dot and cross products is quite confusing.

Share this post


Link to post
Share on other sites
CTar    1134
I think overloading operators for the dot- and cross product is a very bad idea. Especially since the operators normally have different meanings. If X had been an operator then it might have been a good idea to use * for dot and X for cross, but X isn't an operator. If you use a Dot or DotProduct function everyone will know what you are doing if you use operator* for dot some people might think it's used for the cross product. If you use operator % for cross product some people will be wondering why you are using modulus on vectors, and if you use some of the bitwise operators people will be very confused.

IMO you should use a global function taking two vectors, but a member will be just as good.

And please, don't have more than one function for cross and dot product. It will be very confusing for a user to see this:
a = u*v;
b = u.Dot(v);
c = DotProduct(u,v);
It would be the same thing as providing a color AND a colour class, and maybe also Color, Colour, CColor and CColour.

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