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

Author Topic: Basic arithmetic expressions for vector types  (Read 7118 times)

0 Members and 2 Guests are viewing this topic.

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Basic arithmetic expressions for vector types
« on: March 23, 2013, 12:06:49 pm »
Please implement some operators for SFML's vector types. For example I want to divide a "Vector2i" by a float. I often run into these problems and have to write small helper functions every time. Implementing these operators isn't much effort and would be much more intentional.

The only misunderstanding for my part I can think of is that we aren't supposed to use SFML's vector types ourselves. You you please specify they are only for the interface to SFML or for general usage?

Do you plan to implement these operators?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Basic arithmetic expressions for vector types
« Reply #1 on: March 23, 2013, 12:27:33 pm »
For example I want to divide a "Vector2i" by a float.
What do you expect to be the result of Vector2i / float, Vector2i or Vector2f?

I don't see the point of providing such operators. While they seem to be convenient, implicit conversions add a lot of error sources. Dividing integer vectors by a float isn't a frequent use case. If you cast, you make more expressive what you want to achieve.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Basic arithmetic expressions for vector types
« Reply #2 on: March 23, 2013, 04:03:23 pm »
Let Vector2i / float = Vector2f like int / float = float in standard C and C++.

You could store the result in an Vector2i again anyway, for something like this Vector2i /= float, since the copy constructor from Vector2i to Vector2f and vice versa already exists in SFML.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Basic arithmetic expressions for vector types
« Reply #3 on: March 23, 2013, 04:09:28 pm »
since the copy constructor from Vector2i to Vector2f and vice versa already exists in SFML.
It is an explicit conversion constructor, so this code does not work:
sf::Vector2f f;
sf::Vector2i i = f;

I think it is a good decision to keep some type safety, it can really prevent a lot of mistakes.

Why do you need implicit conversions? Vector2i is mostly used for pixel coordinates, whereas Vector2f is used for world units. In the rare cases you need the conversion, why not make it explicit?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Basic arithmetic expressions for vector types
« Reply #4 on: March 23, 2013, 07:30:57 pm »
There are many cases where you need to multiply integer vectors with floats.

Let's say there are framebuffers and for each of them there is a float representing their scale. 1.0 would mean window size, 4.0 would mean four times supersampling and 0.5 would mean sampling at half resolution. To set the right render area, we must multiply the integer vector from window.getSize() by the relative framebuffer size which is a float.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Basic arithmetic expressions for vector types
« Reply #5 on: March 23, 2013, 07:51:28 pm »
Let's say there are framebuffers and for each of them there is a float representing their scale. 1.0 would mean window size, 4.0 would mean four times supersampling and 0.5 would mean sampling at half resolution. To set the right render area, we must multiply the integer vector from window.getSize() by the relative framebuffer size which is a float.
sf::Vector2f result = static_cast<sf::Vector2f>(window.getSize())*framebuffer.size();

It's not much effort, doesn't require any special self-written functions and doesn't introduce implicit conversions.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Basic arithmetic expressions for vector types
« Reply #6 on: March 23, 2013, 08:41:17 pm »
In my personal opinion the first line is much less intentional then the second line. With all these explicit casts the code looses expressiveness and clarity.

Vector2i result = static_cast<Vector2i>(static_cast<Vector2f>(windowsize) * factor);
Vector2i result = windowsize * factor;

If you want to store the result in the vector itself, it becomes even worse because you can't use the *= operator.

windowsize = static_cast<Vector2i>(static_cast<Vector2f>(windowsize) * factor);
windowsize *= factor;

But that's just my sense of neat coding. You have reasons for these operators to not exist, and that's understandable.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Basic arithmetic expressions for vector types
« Reply #7 on: March 23, 2013, 09:40:33 pm »
As mentioned, these are conversion constructors. Therefore, you can use the constructor syntax:
Vector2i result(Vector2f(windowsize) * factor);

The problem is: To implement the requested operators, one needs to provide implicit conversions between vectors of different types. This reduces the type safety drastically. Implicit conversions do not express in code that there is a conversion happening, and can easily lead to mistaken types which cause runtime bugs (instead of compiletime messages).

It's always a trade-off between syntax and safety. In general, I prefer to type slightly more when I have less bugs in turn.
« Last Edit: March 23, 2013, 09:42:55 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Basic arithmetic expressions for vector types
« Reply #8 on: March 23, 2013, 11:55:26 pm »
The problem is: To implement the requested operators, one needs to provide implicit conversions between vectors of different types.

What about returning the given vector type then?

sf::Vector2i operator/ (const sf::Vector2i dividend, float divisor)
{
    return sf::Vector2i((int)(dividend.x / divisor), (int)(dividend.y / divisor));
}

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: Basic arithmetic expressions for vector types
« Reply #9 on: March 24, 2013, 03:43:33 am »
Vector2i and Vector2f are aliases for Vector2<int> and Vector2<float>, Vector2 is a template.
JSFML - The Java binding to SFML.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Basic arithmetic expressions for vector types
« Reply #10 on: March 24, 2013, 08:52:53 am »
Quote
What about returning the given vector type then?
That would be incorrect. And choosing the return type is not really the problem here ;)
Laurent Gomila - SFML developer