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

Author Topic: No element-wise Vector multiplication/division?  (Read 15560 times)

0 Members and 1 Guest are viewing this topic.

dmitry_t

  • Newbie
  • *
  • Posts: 20
    • View Profile
No element-wise Vector multiplication/division?
« on: September 27, 2025, 09:56:30 pm »
Looked through topics on the forums and PRs/issues in the github project and haven't succeeded (maybe just inattentive).

In most of libraries vector classes come along with multiple operation overloads.
I think sf::Vector* family would be good to have element-wise multiplication and division as well.
It's easy to find a suitable example. Let's say...
sf::RenderTarget target = ...;
sf::FloatRect cropArea = ...;

sf::View view;
view.setCenter(cropArea.size / 2.f); // <===== We have such overload
view.setSize(cropArea.size);
view.setViewport({
    cropArea.position / sf::Vector2f(target.getSize()), // <===== No such overload
    cropArea.size / sf::Vector2f(target.getSize()), // <===== No such overload
});
target.setView(view);
 

One could argue that SFML isn't a math library, there's GLM and so on.
However I doubt it's fun to make multiple type conversions for a example like one above.

I'd gladly contribute the feature if it's fine.
« Last Edit: September 27, 2025, 11:16:01 pm by dmitry_t »

kimci86

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Re: No element-wise Vector multiplication/division?
« Reply #1 on: September 28, 2025, 09:06:37 am »
Component-wise multiplication and division are already available as methods: sf::Vector2<T>::componentWiseMul and  sf::Vector2<T>::componentWiseDiv.

The reason why operator* is not overloaded for vector multiplication is that there are several definitions of product between vectors (dot product, cross product, component-wise multiplication), so choosing a specific one would be debatable.
Not sure if there would be the same problem with operator/ but at least it is consistent.


Hapax

  • Hero Member
  • *****
  • Posts: 3462
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: No element-wise Vector multiplication/division?
« Reply #3 on: October 19, 2025, 12:44:01 am »
It's worth noting the SFML's vectors do have "dot" and "cross" methods for those product functions in the same way as those other languages/formats.
Component-wise multiplication is neither "dot product" nor "cross product" so it's there in addition to those.

Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dmitry_t

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: No element-wise Vector multiplication/division?
« Reply #4 on: October 27, 2025, 07:34:45 pm »
I don't see whether you understand me correctly, at least because I didn't say anything about methods.
Anyway, thanks for your attention and answers!

Hapax

  • Hero Member
  • *****
  • Posts: 3462
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: No element-wise Vector multiplication/division?
« Reply #5 on: October 28, 2025, 09:08:52 pm »
No worries.

I was mainly responding to your post about other libraries and was just pointing out the fact that they all name those functions "dot" and "cross" and so it is with SFML as well as component-wise multiplication is named as such. Overloading * operator 'could' be any of these particular functions so it often seems wise to not overload it and give out only explicitly named functions.

I hope that clears up what I was referring to and maybe helps a little bit.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything