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

Author Topic: sf::Vector comparison operator (< and >)  (Read 5819 times)

0 Members and 1 Guest are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
sf::Vector comparison operator (< and >)
« on: April 11, 2016, 01:33:50 pm »
Hi,
I want to create a element at the mouse position. I order to keep it within the window I want to clamp the mouse position to the windows dimensions. Maybe I am just being stupid here, but I can't get it to work...
Here is the code I am using:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <algorithm>

template <typename T>
bool operator <(const sf::Vector2<T>& left, const sf::Vector2<T>& right)
{
    return (left.x < right.x) && (left.y < right.y);
}


template <typename T>
bool operator >(const sf::Vector2<T>& left, const sf::Vector2<T>& right)
{
    return (left.x > right.x) && (left.y > right.y);
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Example");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the windows events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();

            else if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
            {
                auto mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
                const auto windowSize = sf::Vector2f(window.getSize());

                // clamp to window size
                mousePosition = std::min(windowSize, std::max(sf::Vector2f(0.f, 0.f), mousePosition));

                std::cout << mousePosition.x << "x" << mousePosition.y << std::endl;
            }
        }

        // clear the window to black
        window.clear();

        // display the windows content
        window.display();
    }
    return 0;
}

I am getting these compilation errors for the std::max and std::min templates: no match for 'operator<' (operand types are 'const sf::Vector2<float>' and 'const sf::Vector2<float>')
and operator> respectively. I don't really know whats wrong since I do define these operators above. Maybe somebody can tell me whats wrong.

This also got me wondering: Is there a reason why these operators are not defined by SFML?

Thanks in advance,
Foaly

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: sf::Vector comparison operator (< and >)
« Reply #1 on: April 11, 2016, 01:41:35 pm »
Operator need no space between operator and < so change it this way :

operator<

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Vector comparison operator (< and >)
« Reply #2 on: April 11, 2016, 01:43:24 pm »
Your operators are flawed. There are pairs (x1 > x2 but y1 < y2) that are neither lesser, greater nor equal. And all kind of bad things can happen with a flawed ordering, because it is mathematically inconsistent.

Generally speaking, there's not a unique way to define < and > for pairs of numbers. You have to decide of an attribute which has higher priority over the other, or any other custom rule that suits your usage -- which explains why these operators are not defined in SFML.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: sf::Vector comparison operator (< and >)
« Reply #3 on: April 11, 2016, 02:56:01 pm »
I see that makes sense. I didn't think of that. Thanks for the answer!
I use this code to clamp to the window size now:
mousePosition.x = std::min(windowSize.x, std::max(0.f, mousePosition.x));
mousePosition.y = std::min(windowSize.y, std::max(0.f, mousePosition.y));

I still don't quiet understand, why the compiler didn't recognize the operator overload. (removing the space didn't change anything.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Vector comparison operator (< and >)
« Reply #4 on: April 11, 2016, 03:00:31 pm »
I don't know either. Must be something stupid, like the const or the typedefs on sf::Vector2.
Laurent Gomila - SFML developer

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: sf::Vector comparison operator (< and >)
« Reply #5 on: April 11, 2016, 03:54:28 pm »
Actually, it's a bit more tricky than that. Those operators should be defined in the sf namespace due to ADL -- I think.
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Vector comparison operator (< and >)
« Reply #6 on: April 11, 2016, 08:29:20 pm »
In general, when you define a custom ordering relation that is not intuitively clear (that is, operator< and operator> don't make sense mathematically), you should go for a binary predicate, i.e. a function of signature
bool(const Operand& lhs, const Operand& rhs)
that defines your ordering.

This needn't be an actual function, it can be any callable. STL algorithms and associative containers are able to deal with such predicates.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: