Search Unity

Vector{2,3}.Normalize() should return vector length

Discussion in 'Scripting' started by s-sixteen, Jan 29, 2018.

?

Shall Normalize() return the vector's length?

Poll closed Mar 29, 2018.
  1. Yes

    1 vote(s)
    25.0%
  2. No

    3 vote(s)
    75.0%
  1. s-sixteen

    s-sixteen

    Joined:
    Aug 22, 2015
    Posts:
    7
    Yay, first post! ^_^
    Currently, both Vector2 and Vector3's Normalize() returns void. Often, I write code like this:

    Code (CSharp):
    1. Vector2 foo = ...; // load foo here
    2. float magnitude = Vector2.magnitude; // Get foo's magnitude
    3. foo /= magnitude; // Normalize foo
    It would be so awesome if I could just do

    Code (CSharp):
    1. Vector2 foo = ...; // load foo here
    2. float magnitude = foo.Normalize(); // Normalizes foo, returns magnitude
    I think it could work as the magnitude is probably already calculated inside Normalize(). Also, since Normalize() currently returns void, the change would not break any existing code
     
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    There's nothing stopping you from writing your own normalize function that provides the behavior you want.

    But no, I would argue against having the function return the pre-normalized length. It's not intuitive.
     
    Suddoha likes this.
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I'm a bit confused...

    You can argue about whether this makes sense or not.
    From my experience as a programmer, I'd then expect the magnitude of the modified vector, but that's always 1f (approx. - due to imprecisions).

    If it instead returned the magnitude of the original vector, i.e. prior to normalization, that would be some value you wouldn't associate with the name of the function. It also lacks some sort of integrity and relevance with regards to the new value.

    For instance, imagine you had a method that removes items from a collection based on some predicate. Things I could imagine that'll be returned: either nothing, a colllection of affected items or number of removed items.
    I would never associate this functionality with a return value that tells me the number of items prior to removal...

    Anyway, there are neat ways to accomplish your desires. For instance, extension methods.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    I ain't going to lie, I on a number of occasions had this come up. Where I needed the magnitude, and then to normalize. And my code is pretty much what OP has.

    And I have thought about it would be 'nice' to have a more convenient method to do this with.

    But I agree with @Suddoha... though I might find it convenient, I find it counter to what 'Normalize' is supposed to do.

    I also agree with an extension method. Something like a 'GetLengthThenNormalize(this Vector3 v)' method or something.
     
  5. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Use VectorX.normalized.magnitud instead. Normalize actually changes the vector, so no, it shouldn't return the magnitud.

    Just read Suddoha comment...
    He is right the request to return the magnitud is useless, it will always be 1f
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    This would just return 1.
     
    whileBreak likes this.
  7. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    He asked for it man
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    He wants the magnitude BEFORE it's normalized.
     
  9. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Oh so, instead of saving things you can just use: vector.normalized, and vector.magnitud instead of having to save the magnitud somewhere else