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

Author Topic: Length method in sf::Vector2/3  (Read 9801 times)

0 Members and 4 Guests are viewing this topic.

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Length method in sf::Vector2/3
« on: August 31, 2016, 09:48:51 pm »
IDEA
Add "length" method to sf::Vector2 and sf::Vector3, length is property of vector, so should be declared as method and can simplify many operations.

Possible implementation
It is the implementation that I use in my projects. Only sf::Vector2, I didn't need sf::Vector3.
template<typename R = T>
R length()
{
    auto R_x = static_cast<R>(x);
    auto R_y = static_cast<R>(y);

    return std::sqrt(R_x * R_x + R_y * R_y);
}
/* //edit: I know that this code is working only on C++11 and above,
but it can be simply changed to compile under C++98 by replacing "auto" with "R" */

 

Example code
#include <SFML/System/Vector2.hpp>
#include <iostream>

struct myStruct
{
    float data;

    template<typename T>
    myStruct(T val)
    {
        data = val;
    }

    myStruct operator *(myStruct m)
    {
        std::cout << "I (" << this->data << ") am being multipled by " << m.data << "!" << std::endl;
        return {this->data * m.data};
    }
    myStruct operator +(myStruct m)
    {
        std::cout << "I (" << this->data << ") am being added to " << m.data << "!" << std::endl;
        return {this->data + m.data};
    }
    operator float() const
    {
        std::cout << "I am being casted to float!" << std::endl;
        return data;
    }
};

int main()
{
    sf::Vector2f vec1 = {2.f, 3.f};
    sf::Vector2u vec2 = {2u, 3u};

    std::cout << "-Vector2f::length()\n";
    std::cout << vec1.length() << std::endl;

    std::cout << "\n-Vector2f::length<int>()\n";
    std::cout << vec1.length<int>() << std::endl;

    std::cout << "\n-Vector2u::length()\n";
    std::cout << vec2.length() << std::endl;

    std::cout << "\n-Vector2u::length<myStruct>()\n";
    std::cout << vec2.length<myStruct>().data << std::endl;

    return 0;
}
 
Output:
-Vector2f::length()
3.60555

-Vector2f::length<int>()
3

-Vector2u::length()
3

-Vector2u::length<myStruct>()
I (3) am being multipled by 3!
I (2) am being multipled by 2!
I (4) am being added to 9!
I am being casted to float!
3.60555
 

Use cases
  • Line implementation. If line is mathematicaly definied as point and vector instead of pair of points many operations are easier, like scaling etc.
  • Square Bezier Curves using backend of lines above. I don't know if it is faster, but I'm sure that my code is shorter and more readable.

What do you think?
« Last Edit: August 31, 2016, 09:52:42 pm by korczurekk »

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Length method in sf::Vector2/3
« Reply #1 on: September 01, 2016, 05:43:12 am »
IDEA
Add "length" method to sf::Vector2 and sf::Vector3, length is property of vector, so should be declared as method and can simplify many operations.

In math, yes, length is a property, in programming, not necessary.
ej: std::vector can hold values different from numbers, and don't have .x or .y members.

If you need the function you could write it.


Acctually, the sf::Vector2<> template is a facility.

SFML is a media library, actually adding this method ( that can be easily written by the user ) can be used as a excuse to add another mathematicals operations. like triangulation, bezier curves, dot products, cross product ...etc...

SFML is not a math library.

You suggestions aren't bad but IMHO i think that turn aside the purpouse of the library.

« Last Edit: September 01, 2016, 05:44:43 am by DarkRoku »
I would like a spanish/latin community...
Problems building for Android? Look here

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Length method in sf::Vector2/3
« Reply #2 on: September 01, 2016, 09:01:05 am »
This has been suggested/discussed before.

While I tend to agree, such helpers might be useful in some situation, they can also be misleading and get people to write less efficient code, e.g. requesting vector length (being calculated using a square root), while the squared value being perfectly sufficient.

WRT Your code:

If your code requires C++11 just due to using the auto keyword, you're using it wrong. You're static casting to R anyway, so why don't you just use the type R directly?

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Length method in sf::Vector2/3
« Reply #3 on: September 01, 2016, 11:10:31 am »
SFML is a media library, actually adding this method ( that can be easily written by the user ) can be used as a excuse to add another mathematicals operations. like triangulation, bezier curves, dot products, cross product

Triangulation, bezier curves and dot/cross products are advanced operations, which don't really fit library and only a few people will use them, length of vector is basic operation and has maaany use cases and many people will use it.

This has been suggested/discussed before.
Sorry, I couldn't find it. But I see that topic you linked is about more mathematical operations (which IMHO really do not fit SFML), also my idea of length() function is quite different and more flexible.

While I tend to agree, such helpers might be useful in some situation, they can also be misleading and get people to write less efficient code, e.g. requesting vector length (being calculated using a square root), while the squared value being perfectly sufficient.
People still write "sqrt(vec,x * vec,x + vec.y * vec.y) > 50.f" instead of "vec,x * vec,x + vec.y * vec.y > 50.f * 50.f", my method won't really change anything. It could be divided into 2 functions: squareLength() and length(), but I don't know if it wouldn't be too complicated.

If your code requires C++11 just due to using the auto keyword, you're using it wrong. You're static casting to R anyway, so why don't you just use the type R directly?
I think the right operand is enough to determine type and prefer using auto instead of writing types by myself, my code is shorter and more readable. Btw isn't that purpose of "auto" in c++11?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Length method in sf::Vector2/3
« Reply #4 on: September 01, 2016, 11:53:21 am »
A (mathematical) vector can be defined/described as both/either cartesian co-ordinates (x and y for 2D) and/or polar co-ordinates (direction/heading/angle and magnitude/length). Since SFML's vector is mostly just a store for the cartesian co-ordinates, if you need the extra logic, you can always calculate it manually.

That said, Thor includes polar vectors (using SFML vectors) and Plinth has cartesian-based vectors (similar to SFML vectors) that include extra logic including getting and setting to and from polar co-ordinates, so you don't even have to "roll your own".
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Length method in sf::Vector2/3
« Reply #5 on: September 01, 2016, 12:35:13 pm »
If your code requires C++11 just due to using the auto keyword, you're using it wrong. You're static casting to R anyway, so why don't you just use the type R directly?
I think the right operand is enough to determine type and prefer using auto instead of writing types by myself, my code is shorter and more readable. Btw isn't that purpose of "auto" in c++11?

Despite this being slightly off-topic, the point I tried to make:

You don't get anything from using auto here. IMO it should be used if you don't want to care about the specific type being used or if this might change. Neither is true in this case. As a good example, think of something like for(auto a : myList).... This code won't require any changes if the type of myList changes. It will just work. In your case that's not true and you'll always have to modify the static cast anyway. Besides that, how is "auto" shorter than "R"? ;) Point is: This addition adds a feature requirement (C++11) that's easily avoidable, so avoid it.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Length method in sf::Vector2/3
« Reply #6 on: September 01, 2016, 02:21:49 pm »
The idea is actually pretty old and has been brought up many times.

2008
2009 1 / 2
2010

And probably many more threads. Maybe read the argumentation in the other threads. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Length method in sf::Vector2/3
« Reply #7 on: September 01, 2016, 03:10:08 pm »
That said, Thor includes polar vectors (using SFML vectors) and Plinth has cartesian-based vectors (similar to SFML vectors) that include extra logic including getting and setting to and from polar co-ordinates, so you don't even have to "roll your own".
Oh, I use Thor in this project (particles <3) so… well, I can use it. Thanks. :)

You don't get anything from using auto here. IMO it should be used if you don't want to care about the specific type being used or if this might change. Neither is true in this case. As a good example, think of something like for(auto a : myList).... This code won't require any changes if the type of myList changes. It will just work. In your case that's not true and you'll always have to modify the static cast anyway. Besides that, how is "auto" shorter than "R"? ;) Point is: This addition adds a feature requirement (C++11) that's easily avoidable, so avoid it.
I meant that it's usally shorter so I just use it where I can. But thanks, I will remember that while writing code that doesn't require c++11 (now I have many other reasons to use cpp11).

2008
2009 1 / 2
2010
I have no idea why I couldn't find any topics about it.  :'(

 

anything