Page 1 of 1

c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 2:27 pm
by short
Hey guys, quick question:

I wrote this code for adding a vector and a point (the code is in my point class), which returns a point (they are both different classes)

Code: Select all

class Point2D
{
public:
	// member variables
	double x, y;

	// constructors
	Point2D();
	Point2D(double x, double y);

	// operator overloading
	//adding a vector and a point, returns a point
	friend Point2D operator+(const Vector2D &c1, const Point2D &c2)
	{
		// return a new point
		return Point2D(c1.x + c2.x, c1.y + c2.y);
	}

	// member functions
};

#endif
note: I included the entire class definition for clarity, but we are only interested in the operator overloading portion

I need to describe some variables:
"it" and "j" are defined as follows:

Code: Select all

std::vector<Particle>::iterator it; // Iterator
float random = sf::Randomizer::Random(0.0f, (float)M_PI);
	Vector2D j;
	j.x = cos(random);
	j.y = sin(random);
This code compiles fine:

Code: Select all

it->position = j + it->position;
This works.

However, if I try and do the following:

Code: Select all

it->position = it->position + j;
This gives an error. Which is expected, I only told the compiler how to deal with a vector on the left of the +operator and a point on the right of the +operator.

My question is, since adding a point + vector = vector + point in practice, is there a way to tell the compiler not to care which class is on the left and right side? I could add another operator overloading where the point is first and the vector is second, but I'm just rewriting the same thing twice. Is there a more elegant way of handling this?

Thanks!

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 2:52 pm
by RyanPridgeon
short wrote: This code compiles fine:

Code: Select all

it->position = j + it->position;
This works.

However, if I try and do the following:

Code: Select all

it->position = j + it->position;
This gives an error.
What?

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 5:54 pm
by Ginto8
#1: seeing as a point represents a vector from the origin (data-wise), and vice versa, why make them separate classes? Just use a typedef
#2: the two pieces of code you show as one working, the other not, are exactly the same. Please fix this so that we can actually try to solve the problem.

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 6:26 pm
by short
Whoops!

I edited my first post, my bad!

this one works

Code: Select all

it->position = j + it->position;
error:

Code: Select all

it->position = it->position + j;
note: I already understand why I'm getting the error, I am just wondering if it necessary to rewrite the operator again with vector first and point second, if that makes sense.

Perhaps later on I will take your suggestion about using just one class, but this is certainly something that could be applied in other situations!

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 6:34 pm
by Ginto8
short wrote:Whoops!

I edited my first post, my bad!

this one works

Code: Select all

it->position = j + it->position;
error:

Code: Select all

it->position = it->position + j;
note: I already understand why I'm getting the error, I am just wondering if it necessary to rewrite the operator again with vector first and point second, if that makes sense.

Perhaps later on I will take your suggestion about using just one class, but this is certainly something that could be applied in other situations!
yes, it's necessary to have another operator. But doing it like that would bring up some nasty stuff:
-How to you conceptually understand vector+point?
-In vector+point, does it return a vector? or a point? and vice versa?
-Do you really want to rewrite almost the exact same code for two classes (vector & point)?
-Don't these questions really support a point being a vector typedef?

Oh, almost forgot. If you want to use any OBB collision detection, points and vectors really should be synonymous because you are projecting the vectors from the origin to the point (in value, the point itself) onto an axis that was determined by using vector subtraction on two of the vertices (points). You really should reconsider your insistence to make point & vector separate.

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 6:56 pm
by qpHalcy0n
I vehemently disagree.

We actually had a fairly lengthy discussion about this in another post.
There was a post that I'd made a long time ago where I discussed that you CAN VIEW......a point as a vector offset from "some origin" ....wherever that is. Apparently some light bulbs ticked on and it spread like wild fire. This, however, does not mean you SHOULD VIEW.....a point as a vector. Despite their mathematical representation (in affine systems they actually do NOT have the same representation) being similar, they're different ideas conceptually.

Without going into much detail, I'll answer the following:


-How to you conceptually understand vector+point?
The origin is also a point in space (location, no direction), when added to a vector (a direction, no location) yeilds.....


-In vector+point, does it return a vector? or a point? and vice versa?
... a point. (In both cases...with technicalities)


-Do you really want to rewrite almost the exact same code for two classes (vector & point)?
Alot of implementations (including a few of mine) do so there is a DISTINCT difference in concept. There will be FAR more operations you'll do later down the line that are pretty "wack-tastic" that will bring out this VERY distinct difference.
Though you CAN do it however you want. Mooball just typedefs both types as float constants. Then again, I know what the difference in operations on points and vectors are....perhaps some beginners do not. This will prevent undefined mistakes.


-Don't these questions really support a point being a vector typedef?
No...not particularly.


The OBB thing works because a point minus a point is DEFINED....as is a vector minus a vector, a vector plus a vector, a point plus a vector, a point minus a vector and so on and so forth. All of these relationships are very clearly defined.
Represent it how you wish. Again, you will *NOT* and I do stress.....not......be sacrificing any code speed representing it one way or the other. If it assists you in keeping the concepts straight then do so.



Ginto8 wrote:
short wrote:Whoops!

I edited my first post, my bad!

this one works

Code: Select all

it->position = j + it->position;
error:

Code: Select all

it->position = it->position + j;
note: I already understand why I'm getting the error, I am just wondering if it necessary to rewrite the operator again with vector first and point second, if that makes sense.

Perhaps later on I will take your suggestion about using just one class, but this is certainly something that could be applied in other situations!
yes, it's necessary to have another operator. But doing it like that would bring up some nasty stuff:
-How to you conceptually understand vector+point?
-In vector+point, does it return a vector? or a point? and vice versa?
-Do you really want to rewrite almost the exact same code for two classes (vector & point)?
-Don't these questions really support a point being a vector typedef?

Oh, almost forgot. If you want to use any OBB collision detection, points and vectors really should be synonymous because you are projecting the vectors from the origin to the point (in value, the point itself) onto an axis that was determined by using vector subtraction on two of the vertices (points). You really should reconsider your insistence to make point & vector separate.

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 7:28 pm
by short
The difference between vectors and points was the reason why I split them into separate classes in the first place, after a year of physics and vector math classes I do understand how different they are :)

If anyone wants to point anything out about the operator overloading, would love to hear it!

edit:

For clarity's sake, this is what I meant about rewriting it for both ways:

Code: Select all

// operator overloading
	//adding a vector and a point, returns a point
	friend Point2D operator+(const Vector2D &c1, const Point2D &c2)
	{
		// return a new point
		return Point2D(c1.x + c2.x, c1.y + c2.y);
	}

	//adding a vector and a point, returns a point (user doesn't have to worry about which goes first)
	friend Point2D operator+(const Point2D &c2, const Vector2D &c1)
	{
		// return a new point
		return Point2D(c1.x + c2.x, c1.y + c2.y);
	}

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 7:58 pm
by qpHalcy0n
Ah, good man.

As for the operator, no unfortunately not. There must be an explicit lvalue and rvalue. So operator+ would have to be defined for point and vector both.

As that is a binary operator you also have the choice of taking the definition outside of the class.

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 7:59 pm
by short
If I don't have it in either the point or vector2D classes, where else would be appropriate to put it? It is kind of awkward to have it in my point class, as it forces me to #include "Vector2D" in my point class.

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 8:04 pm
by qpHalcy0n
As a programming habit, I keep binary operators that return values outside of the class type definitions. Binary operators that modify the value of the lvalue are then defined within the class definition.

Thusly, your +=, -=, *=, /=, =, and possibly == would reside within the classes.
Then +, -, *, /, and possibly == would reside outside as binary operator overloads.

Either way, you'd have to include point in vector and vector in point outside of the header guards.

OR

include both in something such as "mymath.h". (My personal preference)

Re: c++ specific (operator overloading) vector+point

Posted: Mon Mar 29, 2010 8:10 pm
by short
include both in something such as "mymath.h". (My personal preference)
This seems to make the most sense, re-usability!!