# SFML community forums

## Help => System => Topic started by: OniLinkPlus on January 23, 2010, 10:31:41 pm

Title: The Vector Functions
Post by: OniLinkPlus on January 23, 2010, 10:31:41 pm
I see they haven't been implemented. Will they be added soon?
Some Vector functions you probably should do(and code for some of them) are:
Vector2
Code: [Select]
`#include <cmath>T Direction(){// May want to use double or float instead    return atan2(y, x);}T Dot(Vector2<T>& Other){//May want to use double or float instead    return x*Other.x + y*Other.y;}T Length(){    return sqrt(x*x + y*y);}void Normalize(){    T Length = Length();    x /= Length;    y /= Length;}`

Vector3
Code: [Select]
`#include <cmath>Vector3<T>& Cross(Vector3<T>& Other){    return Vector3<T>(y*Other.z - Other.y*z, z*Other.x - x*Other.z, x*Other.y - y*Other.x);}T Dot(Vector3<T>& Other){    return x*Other.x + y*Other.y + z*Other.z;}T Length(){    return sqrt(x*x + y*y + z*z);}void Normalize(){    T Length = Length();    x /= Length;    y /= Length;    z /= Length;}`

I'll add more functions and examples as I think of them.
Title: The Vector Functions
Post by: K-Bal on January 23, 2010, 11:07:03 pm
Why are you doing a normalization in your dot-calculation? ;) That should be a user-side decision.
Title: The Vector Functions
Post by: OniLinkPlus on January 23, 2010, 11:09:34 pm
Quote from: "K-Bal"
Why are you doing a normalization in your dot-calculation? ;) That should be a user-side decision.
Dot product requires that the lengths are equal, so I needed a way to make them equal. I'm probably going to rewrite it a little to normalize B to the length of A.

EDIT: Done, but I normalize A to the length of B instead.
EDIT: Ah, I misread the Wikipedia article, the lengths don't have to be equal. Fixing...
Title: The Vector Functions
Post by: OniLinkPlus on January 27, 2010, 05:41:27 am
Title: The Vector Functions
Post by: Laurent on January 27, 2010, 08:45:17 am
Dunno :wink:
Title: The Vector Functions
Post by: Nexus on January 28, 2010, 01:59:03 pm
This discussion isn't new. ;)
http://www.sfml-dev.org/forum/viewtopic.php?t=1937
http://www.sfml-dev.org/forum/viewtopic.php?t=1488
http://www.sfml-dev.org/forum/viewtopic.php?t=1232
http://www.sfml-dev.org/forum/viewtopic.php?t=651

I think it's better to wait with the integration of vector algebra functions into SFML. Several discussions in the past showed that it's not easy to implement corresponding functionality, which is intuitive to use, powerful and yet generic. Especially the last point is an important one, in my opinion – most of the proposals didn't take genericity and extensibility into account. Yours is one of them.

By the way, your code isn't correct C++ (for example you shouldn't return references to local objects, templates don't work like this and variables must be declared).