c++ specific (operator overloading) vector+point

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

c++ specific (operator overloading) vector+point

Post 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!
Last edited by short on Mon Mar 29, 2010 6:26 pm, edited 2 times in total.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

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

Post 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?
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

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

Post 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!
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

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

Post 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.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

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

Post 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);
	}
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

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

Post 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.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

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

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

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

Post 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)
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

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

Post by short »

include both in something such as "mymath.h". (My personal preference)
This seems to make the most sense, re-usability!!
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Post Reply