Rotating Vectors
BlitzMax Forums/BlitzMax Programming/Rotating Vectors
| ||
I'm documenting an old 2d vector module I made and I noticed that in the Rotate method I use square root, sine and cosine:Local Length:Double = GetLength() 'uses square root X = Cos(Angle) * Length Y = -Sin(Angle) * Length Is there a better way to rotate a vector? |
| ||
That's how I'd do it too. I'm going out on a limb here and saying there isn't a beter way...(now watch people disprove me ;-)) |
| ||
I use: cos_rot# = Cos(ang#) sin_rot# = Sin(ang#) new_x# = (x * cos_rot) - (y * sin_rot) new_y# = (y * cos_rot) + (x * sin_rot) ;) That rotates the existing vector by ang, though. It doesn't create a vector with an absolute angle of ang. |
| ||
Ah yes a good ole Matrix. In fact I lied as my TVector type has this method:Method Rotate(angle!) 'clockwise Local tx# = (x * Cos(angle)) - (y * Sin(angle)) Local ty# = (x * Sin(angle)) + (y * Cos(angle)) x=tx y=ty End Method But I like the way Big10p is storing the cos and sin before the main calc to reduce the number of calls to cos and sin. Wonder if it's faster creating those two extra local variables. I guess a test is in order. |
| ||
Wonder if it's faster creating those two extra local variables. I guess a test is in order. I've never tested, either. Old habits die hard, is all. :) |
| ||
There is this to compare against. |
| ||
That uses the exact same method. :) |
| ||
Indeed it does. I actually posted after GA's first response. |
| ||
Ah. |