SFML community forums

General => Feature requests => Topic started by: phear- on August 01, 2009, 05:24:35 pm

Title: Normalization of Vectors
Post by: phear- on August 01, 2009, 05:24:35 pm
It would be cool if we could have a member function within sf::Vector2f that takes a Vector2 parameter and normalizes the member data x/y and the paramaters x/y. Here's what i'm using, it can be adapted to sf::Vector2f pretty easily.

Vector2.h
Code: [Select]

namespace gtp
{
struct Vector2
{
Vector2();
Vector2(float x, float y);
Vector2& operator=(const Vector2& param);
float x;
float y;
};

Vector2 normalize(Vector2 Target, Vector2 Current);
}


Vector2.cpp
Code: [Select]

namespace gtp
{
Vector2 gtp::normalize(Vector2 Target, Vector2 Current)
{
Vector2 _Normal;
float SlopeX = Target.x - Current.x;
float SlopeY = Target.y - Current.y;
if ((SlopeY == 1) || (SlopeY == -1))
{
_Normal.y = SlopeY;
_Normal.x = SlopeX;
}
else
{
_Normal.y = (SlopeY / SlopeY);
_Normal.x = (SlopeX / SlopeY);
}
return _Normal;
}
}


Normalization is basically finding the delta between 2 coords and making it a unit of one (for unit moving).
Title: Normalization of Vectors
Post by: K-Bal on August 01, 2009, 05:54:39 pm
You know that there can be other things in an Vector2 than floats? :D
Title: Normalization of Vectors
Post by: Laurent on August 01, 2009, 06:08:25 pm
This is not vector normalization (google/wikipedia it if you don't know what is it exactly).

This function is a very specific one that may be useful to you, but not a good candidate for being generic and integrated into SFML.
Title: Normalization of Vectors
Post by: phear- on August 02, 2009, 02:20:53 am
It's used within the XNA framework in their Vector2 class and I thought it might be used by other people who have to find the delta of 2 coords in a 1 unit distance. (I was told its called normalization and thats what it is called in that framework so my apologies if thats not what its called)

Edit: I just checked Wiki and it is called normalization:
Quote
A unit vector is any vector with a length of one; normally unit vectors are used simply to indicate direction. A vector of arbitrary length can be divided by its length to create a unit vector. This is known as normalizing a vector. A unit vector is often indicated with a hat as in รข.
Title: Normalization of Vectors
Post by: Laurent on August 02, 2009, 01:31:14 pm
This definition is right, but it doesn't match your function ;)

Normalizing is dividing the vector's components by its length so that its new length is 1.
Code: [Select]
Vector2 normalize(const Vector2& source)
{
    float length = sqrt((source.x * source.x) + (source.y * source.y));
    if (length != 0)
        return Vector2(source.x / length, source.y / length);
    else
        return source;
}


Your function is doing something totally different: it computes the delta between 2 points, and then scale it so that its Y component is 1.
Title: Normalization of Vectors
Post by: Nexus on August 13, 2009, 03:21:05 pm
We have already had a discussion about vector functions (-> Link (http://www.sfml-dev.org/forum/viewtopic.php?t=1232))...