Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Overload of * operator for Vector2<T>  (Read 3590 times)

0 Members and 1 Guest are viewing this topic.

IngloriousTom

  • Newbie
  • *
  • Posts: 17
    • View Profile
Overload of * operator for Vector2<T>
« 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Overload of * operator for Vector2<T>
« Reply #1 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 are a more expressive way of providing the same functionality.

This has been discussed many times, by the way; please use the search function.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

IngloriousTom

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Overload of * operator for Vector2<T>
« Reply #2 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
« Last Edit: September 01, 2014, 06:58:08 pm by IngloriousTom »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Overload of * operator for Vector2<T>
« Reply #3 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 for example.
« Last Edit: September 01, 2014, 06:52:10 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: