Alright here is the subroutine with an example of using the return values the routine can be called as follows - getang(posx,posy, tarx,tary)
the angles returned are in radians within the range of 0 - 2*pi
angy - direction of travel and it is linked to cosy,siny (cos is horizontal, sin is vertical)
angx - right angles and it is linked to cosx,sinx
Here it is
// This version returns two angles as well as the cos/sin for each
// angy is the actual direction and corrisponds to y axis for drawing
// angx is at right angles and corrisponds to x axis for drawing
// cosx is the cosine of angx
// sinx is the sine of angx
// cosy is the cosine of angy
// siny is the sine of angy
sub getang(x1,y1,x2,y2)
angx = atan(x2-x1,y2-y1)
angy = angx - pi/2
if (angx < 0) angx = angx + 2*pi
if (angy < 0) angy = angy + 2*pi
cosx = cos(-angx)
sinx = sin(-angx)
cosy = cos(-angy)
siny = sin(-angy)
end sub
And this is an example of how to use it
sub getang(x1,y1,x2,y2)
angx = atan(x2-x1,y2-y1)
angy = angx - pi/2
if (angx < 0) angx = angx + 2*pi
if (angy < 0) angy = angy + 2*pi
cosx = cos(-angx)
sinx = sin(-angx)
cosy = cos(-angy)
siny = sin(-angy)
end sub
open window 640,512
posx = 320
posy = 256
desx = 640
desy = 256
repeat
setdispbuf draw
draw = 1 - draw
setdrawbuf draw
clear window
desx = desx + and(peek("port1"),32)/32 - and(peek("port1"),128)/128
desy = desy + and(peek("port1"),64)/64 - and(peek("port1"),16)/16
line posx,posy to desx,desy
getang(posx,posy,desx,desy)
text 20,20,"Angle Y - " + str$(angy/pi*180) // x/pi*180 convert radians to degrees
text 20,40,"Angle X - " + str$(angx/pi*180) // x/pi*180 convert radians to degrees
// example of drawing something using the output values
cy = desx
sy = desy
for y = 0 to 32
cx = cy
sx = sy
for x = 0 to 32
dot cx,sx
cx = cx + cosx
sx = sx + sinx
next
cy = cy + cosy
sy = sy + siny
next
until (0=1)