angle maths

BlitzMax Forums/BlitzMax Programming/angle maths

nino(Posted 2008) [#1]
Can anyone tell me how you would go about solving for an angle and a distance given an x and y offset?

I know how to do the opposite (solving for an x and y offset given an angle and a distance):

xoffset=distance*Sin(angle)
yoffset=-distance*Cos(angle)


..but I cant remember how to do the reverse.


Dreamora(Posted 2008) [#2]
by reverting it ^^
angle = atan(ydif / xdif)
radius = sqrt(x^2 + y^2)

basic math


nino(Posted 2008) [#3]
atan - thats what i was looking for. thanks.


Floyd(Posted 2008) [#4]
That would be ATan2( ydif, xdif ).

ATan will give the same result for ATan( ydif / xdif ) and ATan( -ydif / -xdif ). Thus it is good only for a 180 degree range.


nino(Posted 2008) [#5]
yes - I had to jiggle the wires a little to get what i wanted

Global x=14
Global y=14

Function ADtoXY:Float[](a:Float,d:Float)
	Local r:Float[2]
	r[0]=d*Sin(a)'x
	r[1]=-d*Cos(a)'y
	Return r
EndFunction

Function XYtoAD:Float[](x:Float,y:Float)
	Local r:Float[2]
	r[0]=ATan(y/x)'a
	If x<0 r[0]:+180
	r[0]:+90
	r[1]=Sqr(x^2 + y^2)'d
	Return r
EndFunction

Local solution1:Float[2]
solution1  =  XYtoAD(x,y)

Local solution2:Float[2]
solution2  =  ADtoXY(solution1[0],solution1[1])

Print "should be "+x+" and "+y
Print solution2[0]
Print solution2[1]