Rotating Vectors

BlitzMax Forums/BlitzMax Programming/Rotating Vectors

mothmanbr(Posted 2007) [#1]
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?


Grey Alien(Posted 2007) [#2]
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 ;-))


big10p(Posted 2007) [#3]
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.


Grey Alien(Posted 2007) [#4]
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.


big10p(Posted 2007) [#5]
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. :)


tonyg(Posted 2007) [#6]
There is this to compare against.


big10p(Posted 2007) [#7]
That uses the exact same method. :)


tonyg(Posted 2007) [#8]
Indeed it does.
I actually posted after GA's first response.


big10p(Posted 2007) [#9]
Ah.