Simplevector.normalize(v)

Started by Irony, March 24, 2015, 12:30:59 PM

Previous topic - Next topic

Irony

Sorry for being so anal, but I somehow don't like to see vec.normalize(vec) in my code ;)
Any specific reason why it's not a static method?

EgonOlsen

It's not static, because there's no point in making it static. Maybe the method signature isn't clear...what this actually is, is:


b=a.normalize();


with a remaining untouched and the returned b being a new instance of SimpleVector. The method that you are using in a special variant for Android that does the same thing but avoids the object creation by letting you feed it with a SimpleVector instance to fill with the result.
So this basically is:


b=a.normalize(b);


In your case, a==b...but that's just a special case. I agree that this looks strange, but...

There are several of these methods to compensate for the Android's weak garbage collection. They are meant to be fast, not to be nice.

Irony

 I meant specifically the version that takes the simplevector as an argument (I always use these methods wherever available).
I see the reasoning behind something as v1.add(v2), but as normalize(v) only operates on a single object for input and output, I thought it could/should be made static. But who cares, as long as it works and has no disadvantage other than looking strange :)



EgonOlsen

Quote from: Irony on March 24, 2015, 02:29:36 PM
...but as normalize(v) only operates on a single object for input and output...
But that's not what it does. It operates on one instance and writes the result into another, which it returns in addition. In your case, these two instances are the same, but they don't have to be.

Irony

#4
Ok, thanks for the explanation!
One last question, do you see a problem with calling a.normalize(a)? Should I use two different vectors?

EgonOlsen

No, that's fine. There are methods of that kind that don't support this, but they should raise an error if you try that.