angle maths
BlitzMax Forums/BlitzMax Programming/angle maths
| ||
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. |
| ||
by reverting it ^^ angle = atan(ydif / xdif) radius = sqrt(x^2 + y^2) basic math |
| ||
atan - thats what i was looking for. thanks. |
| ||
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. |
| ||
yes - I had to jiggle the wires a little to get what i wantedGlobal 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] |