Author Topic: Simplevector.normalize(v)  (Read 2470 times)

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Simplevector.normalize(v)
« on: March 24, 2015, 12:30:59 pm »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Simplevector.normalize(v)
« Reply #1 on: March 24, 2015, 01:53:39 pm »
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:

Code: [Select]
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:

Code: [Select]
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.

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Re: Simplevector.normalize(v)
« Reply #2 on: March 24, 2015, 02:29:36 pm »
 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 :)



Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Simplevector.normalize(v)
« Reply #3 on: March 24, 2015, 02:33: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.

Offline Irony

  • long
  • ***
  • Posts: 151
    • View Profile
Re: Simplevector.normalize(v)
« Reply #4 on: March 24, 2015, 02:37:55 pm »
Ok, thanks for the explanation!
One last question, do you see a problem with calling a.normalize(a)? Should I use two different vectors?
« Last Edit: March 24, 2015, 02:47:01 pm by Irony »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Simplevector.normalize(v)
« Reply #5 on: March 24, 2015, 05:52:01 pm »
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.