Hi, How do i write a vector class to manipulate vectors in 3D; manipulate vector components in Cartesian, cylindrical, and spherical coordinate systems; add vectors; multiply by scalars; compute dot and cross products?

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by jonsca
``````class vector {
manipulateInCartesian (&vectorType);
manipulateInSpherical (&vectorType);
manipulateInCylindrical (&vectorType);
vectorType add (vectorType, vectorType);
vectorType multiplyByScalar (scalarVal, vectorType);
vectorType dotProduct (vectorType, vectorType);
vectorType crossProduct (vectorType, vectorType);
};``````

Don't forget to implement the functions and define a vectorType.

Edited by Moschops: n/a

The obvious approach most Vector3D classes use it to represent the data in one form and convert from input in another form into it (e.g. leave the vector in cartesian for and convert from spherical and cylindrical input). Depending on the problem that is a viable solution [not always].

Then it is a matter of doing one thing at a time. I would actually recommend writing the output first, that greatly helps with debugging, so you have a class like this:

``````class Vector3D
{
private:

double x,y,z;    // This can be done in many ways,but I have chosen discrete
// values. [you can use an array, etc]
public:

Vector3D();         // Default constructor [e.g. set x,y,z to zero]
Vector3D(const Vector3D&);      // ALWAYS write a copy constructor
Vector3D& operator=(const Vector3D&);    // ALWAYS write an assignment operator
~Vector3D()  {}            // Destructor

void write(std::ostream&) const;        // Write should be constant
};

// This is implemented in the standard way
std::ostream& operator<<(std::ostream& OutStream,const Vector3D& A)
{
A.write(OutStream);
return OutStream;
}``````

Then all you have to write is a simple write method and the constructors/copy constructors and you can star doing stuff with your class. Initially, the vector 0,0,0 is a bit boring, so you start to increase the scope, e.g. an constructor that takes three values, test that and build up the class.

After a bit you add this:

``````class Vector3D
{
// stuff....
public:
// stuff...
Vector3D& operator+=(const Vector3D&);
Vector3D operator+(const Vector3D&,const Vector3D&) const;

// then to multiply by a scalar
Vector3D& operator*=(const double);
// etc...
};``````

If you actually want to change the manipulation set e.g. for some problems use spherical coordinates and others to use something else, then the best way to do that
is to have a base Vector3D and then inherit them into a SphericalVec etc.

When you have some more code and get confused/stuck etc. post your code and we can see if we can help.

Edited by StuXYZ: n/a

Do you know how to do all of the transformations from a mathematical standpoint (i.e., pencil and paper)? If not, first start with a good reference book or Wikipedia article and figure out the mechanics of it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.