Jump to content
  • Advertisement
Sign in to follow this  
induster

C++ Vector Class

This topic is 3893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a question about the following code:
#ifndef _VECTOR
#define _VECTOR
#include <math.h>

class Vector3D
{
private:
	float x, y, z;
public:
	//default constructor
	Vector3D(float X = 0, float Y = 0, float Z = 0)
	{
		x = X;
		y = Y;
		z = Z;
	}
	~Vector3D(){};

	//calculate and return the magnitude of this vector
	float GetMagnitude()
	{
		return sqrtf(x * x + y * y + z * z);
	}

	//multiply this vector by a scalar
	Vector3D operator*(float num) const
	{
		return Vector3D(x * num, y * num, z * num);
	}

	//pass in a vector, pass in a scalar, return the product
	friend Vector3D operator*(float num, Vector3D const &vec)
	{
		return Vector3D(vec.x * num, vec.y * num, vec.z * num);
	}

	//add two vectors
	Vector3D operator+(const Vector3D &vec) const
	{
		return Vector3D(x + vec.x, y + vec.y, z + vec.z);
	}

	//subtract two vectors
	Vector3D operator-(const Vector3D &vec) const
	{
		return Vector3D(x - vec.x, y - vec.y, z - vec.z);
	}

	//normalize this vector
	void normalizeVector3D()
	{
		float magnitude = sqrtf(x * x + y * y + z * z);
		x /= magnitude;
		y /= magnitude;
		z /= magnitude;
	}
	
	//calculate and return dot product
	float dotVector3D(const Vector3D &vec) const
	{
		return x * vec.x + y * vec.y + z * vec.z;
	}

	//calculate and return cross product
	Vector3D crossVector3D(const Vector3D &vec) const
	{
		return Vector3D(y * vec.z - z * vec.y,
				z * vec.x - x * vec.z,
				x * vec.y - y * vec.x);
	}
};

#endif
1. I normally see the implementation split into a .cpp file. What are the implications of writing a class this way? More efficient? Bad code? No difference?

Share this post


Link to post
Share on other sites
Advertisement
It's largely an organization vs. (potential) performance tradeoff. If you put the implementation in a .cpp file, then client code doesn't get to see the implementation. I'm not talking about security, just about organization. If you're changing the implementation of some of the functions, this is nice, because the header doesn't change, so you don't have to recompile every file that includes it.

On the other hand, putting the code into the header makes it more likely that the compiler can inline the code. For very short functions (like the ones you've shown), the compiler probably will choose to inline them, given the chance. Note that some compilers can inline even if your code is in the .cpp file (using link-time-code-generation), but if you want to ensure that any compiler (gcc, etc...) could do so, you need to leave the code in the header.

Share this post


Link to post
Share on other sites
Functions defined inside a class body is implicitly inline. Inline doesn't however mean anything in and of itself, the compiler is free to ignore it. It does decrease code maintainability, you have to recompile all files including it if you change it. If the code were in a source file, only the source file need be recompiled. However, this isn't a huge problem for a vector class, because they are infrequently changed.

There are some issues with your class:
0) Don't put a empty destructor. The complier will generate one for you
1) As an optimisation, you can provide a "SquaredMagnitude" function, so that comparing the relative lengths of two vectors can avoid costly sqrt calls.
2) Personally, I think "crossVector3D" and "dotCrossVector3D" are overly verbose.

Consider the usage:

Vector3 one, two;

float dot = one.dot(two);

Vector3 cross = two.cross(one);



Is as readable (or more so, IMO) than:

Vector3 one, two;

float dot = one.dotVector3(two);

Vector3 cross = two.crossVector3(one);



In my code, I have dot and cross as free functions. But that is just how I choose to do it.

Share this post


Link to post
Share on other sites
ty for the advice. what is a free function? I don't have any experience using these functions but I'm adding the squaredMagnitude() for the day I do. I'll take your word for it.

Share this post


Link to post
Share on other sites
One not in a class. For example, I have:


float dot(const Vector &one, const Vector &two )
{
return /* ... */;
}

Vector cross(const Vector &one, const Vector &two )
{
return /* ... */;
}

// usage:
void example()
{
Vector3 one, two;

float d = dot(one,two);

Vector3 c = cross(two,one);
}

Share this post


Link to post
Share on other sites
Yes. I define all the operators outside the class, because none of them access private data. The only things in the class body itself are the constructors and the length() and normalise() functions.

But that is just a personal choice I made. Keeping functions inside the class body is fine.

edit:

Looking again, I see that you have made your x, y and z members private. For a simple type like a vector, it is arguable that this is unnecessary (encapsulation adds nothing). I have these variables public.

Share this post


Link to post
Share on other sites
I have a question about normalizing vectors. The function I have changes the actual x, y, z. I would have to copy it's coordinates to another vector before normalizing it (which would overwrite it's position).

In the real world, you need to keep track of not only the normal of an object but also it's coordinates right? Or, would you have some other class keep track of where objects are in the world?

Share this post


Link to post
Share on other sites
I think you have "normalisation" mixed up with "normal". In this context, normalising a vector means making it of unit length. A normal, however, is a direction vector that's perpendicular to a plane. Normals are often normalised, meaning that they are of unit length (unit length means length of 1).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!