SFML community forums

General => Feature requests => Topic started by: IngloriousTom on September 01, 2014, 06:01:39 pm

Title: Overload of * operator for Vector2<T>
Post by: IngloriousTom on September 01, 2014, 06:01:39 pm
It's a really little feature but it could be nice to have a new multiply overload for Vector2 class.
Currently you can multiply a Vector2<T> only with a T, but not with another Vector2<T>.
Thus creating relatively verbose code like this:

sf::View view;
view.setSize(scale.x * size.x, scale.y * size.y);

when this could be enough, without losing any readability:
view.setSize(scale*size);

I don't see why it has not be done since it already exists for + and - operators.
Title: Re: Overload of * operator for Vector2<T>
Post by: Nexus on September 01, 2014, 06:16:22 pm
I don't see why it has not be done since it already exists for + and - operators.
Because multiplying vectors component-wise is -- in contrast to vector addition or subtraction -- not a geometrically meaningful operation. The * operator may either denote component-wise product, dot product or cross product (or even more, depending on the context). That's why named functions like those in Thor (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html) are a more expressive way of providing the same functionality.

This has been discussed many times, by the way; please use the search function.
Title: Re: Overload of * operator for Vector2<T>
Post by: IngloriousTom on September 01, 2014, 06:25:30 pm
Ok, generally speaking I thought dot and cross product were used with functions, not operator overloads for this exact reason.

This has been discussed many times, by the way; please use the search function.

The only discussion I found about it was your own post about overloading "<" operator. Which was indeed not meaningful enough, and I thought otherwise for the * operator. Sorry if I did not found other topics about it and thanks for your answer.

edit: misspelling
Title: Re: Overload of * operator for Vector2<T>
Post by: Nexus on September 01, 2014, 06:50:29 pm
Ok, generally speaking I thought dot and cross product where used with functions, not operator overloads for this exact reason.
It's the same reason why * is not overloaded: it's semantics are not clear. Multiplication of two vectors is mathematically not defined; people reading the code may expect dot/cross product or the component-wise operator (often referred to as .* in mathematics).

I know that other libraries provide it for the sake of convenience, but they also define < as component-wise less-than combined with logical and, which leads to very tedious bugs when you try to insert vectors into a sorted container. Sometimes clarity is better than convenience.

Concerning previous discussions, there is this thread (http://en.sfml-dev.org/forums/index.php?topic=10631.0) for example.