C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-12-2007, 04:56 PM   #1
Registered User
 
Join Date: May 2006
Posts: 894
Vector class

Hey there. I'm pursuing my Matrix class and would like to make it work together with a Vector class I'm developing as well. I have a design question however and a technical question as well.

First, here's the code for the Vector class right now:
Code:
#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED

#include <vector>

template < class T >
class Vector
{
public:
	Vector(int n = 3);
	Vector(const Vector< T >&);

	// Not quite sure about the type it should return...
	float Angle( ) const;
	void Normalize( );
	float Length( ) const;

	Vector< T > VecProduct(const Vector< T >&) const;
	T DotProduct(const Vector< T >&) const;

	bool IsParallel(const Vector< T >&) const;
	bool IsOrthogonal(const Vector< T >&) const;

	bool operator == (const Vector< T >&) const;
	bool operator != (const Vector< T >&) const;
	Vector< T >& operator = (const Vector< T >&);
	Vector< T >& operator += (const Vector< T >&);
	Vector< T >& operator + (const Vector< T >&) const;
	Vector< T >& operator -= (const Vector< T >&);
	Vector< T >& operator - (const Vector< T >&) const;
	Vector< T >& operator *= (const T&);
	Vector< T >& operator * (const T&) const;

private:
	
};

#endif // VECTOR_H_INCLUDED
It isn't as complete as it should, but this is a good start for a beginner's Vector class. I want to be able to use that Vector class with matrices; therefore I need it to let the user use a 2, 3, 5 or even 10 dimension vector. I first thought about using std::auto_ptr< > and allocate enough memory for each dimension, but copying objects would be a mess because of pointer ownership. Then I thought of using a vector, but it seems like overhead to me since vectors don't usually get large enough for vectors to be efficient. I thought then to use a pointer and allocate memory dynamically but this seems a bit hack-ish to me. Do you guys know a good way to solve that problem ?

Also, the technical question is rather simple, in fact. What type should the Angle() function return ? I thought returning a T would be dangerous because if T happens to be 'int' then we lose precision (which is also a problem for Normalize() and Length()). Then I thought returning float would be correct because of its precision but I'm not 100% sure.

Thanks a lot.
Desolation is offline   Reply With Quote
Old 05-12-2007, 05:39 PM   #2
The superheterodyne.
 
twomers's Avatar
 
Join Date: Dec 2005
Location: Ireland
Posts: 2,215
You have a choice in the second option - do you want to return degrees or radians? Either way I'd return a double of some description - I never liked the float type much. Make a method for each option. You might also want to consider returning a COORD, x and y values. There's no point in just working in polar coordinates. The other one, that I can say but not spell, is useful too. For the former question ... could be difficult to have the number of dimensions as a variable. Stick with fixing a static number of dimensions first.
__________________
I blag!
twomers is offline   Reply With Quote
Old 05-12-2007, 05:44 PM   #3
Registered User
 
Join Date: Jan 2005
Posts: 7,246
In general, an std::vector is as efficient as any other similar dynamic memory solution. Since the size of your Vector will be dynamic, you will probably want to use dynamic memory. So, I would use std::vector. If you want to cap the size of your Vector at something small like 10, then a local array of that maximum might be an option to avoid dynamic allocation.
Daved is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 10:34 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22