Vector Template

Started by
3 comments, last by EbonySeraph 21 years, 6 months ago
I have a question about a vector template class I want to create. I don''t want to make anything too complicated, but I just want a vector class that can handle ints, doubles, and floats, and also be 2D or 3D. Should I just make a normal template class for a vector and use a constructor''s parameter to set wether or not its 2D or 3D or is there another way to pass a parameter to specify how many dimensions?
"Ogun's Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
Advertisement
Hm... it could get pretty complicated if you wanted. Take a look at these two

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-VectQuat&forum=cotd&id=-1

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=Tip-UnrollingLoopsMeta&forum=totd&id=-1

The first link shows how to make n-dimensional vector space with quite a lot functionality too. The latter link shows how to unroll the loops with the compiler so that runtime speed is optimal. And it's still not as fast as a hand-written version because it needs to use arrays.

In practice, I suggest you just make 2 separate vector classes, Vector2d and Vector3d, and write them manually. The only template parameter would be the type. And use boost/operators.hpp if you're really lazy . I don't know why you'd want to combine 2d and 3d vectors in to a single class anyway..

[edited by - civguy on September 30, 2002 11:50:59 AM]
Of course, you do realise that it takes n-1 n-dimensional vectors to do a cross-product.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by civguy
The latter link shows how to unroll the loops with the compiler so that runtime speed is optimal. And it''s still not as fast as a hand-written version because it needs to use arrays.

And yet it''s possible to write template vector operations that are faster - with a little assembly help... There''s an article here on GameDev about it.

quote:
I don''t know why you''d want to combine 2d and 3d vectors in to a single class anyway...

It''s not a bad idea. You have less code to write, and only need to specialize certain functions (most of which should not be member functions anyway, like cross and dot product, largely because they obfuscate intent).
Thanks for the insight.

I do realize that a cross product can''t be calculated without certain conditions.

"Ogun''s Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.
"Ogun's Laughter Is No Joke!!!" - Ogun Kills On The Right, A Nigerian Poem.

This topic is closed to new replies.

Advertisement