Home
 | 
Programs
 | 
Tips & Tricks
 | 
Subs & Functions
 | 
Links
 | 
Laz's Liberty Basic Site
GetAngle Functions
Call the function like this:
Angle = GetAngle(X,Y,Xend,Yend)
' Demo to show that the GetAngle() Function works
' The GetAngle() Function will get the angle of a line.
' GetAngle(X start point, Y start point, X end point, Y end point)
' Instructions:
' Positions the mouse in the center of the screen
' Left Click and move the mouse around
' Right Click to erase the screen and start over
'Created by: laz on May 7, 2002
'Some functions by Tom Nally
nomainwin
open "GetAngle()" for graphics_nsb_fs as #main
print #main, "trapclose [quit]"
print #main, "down; color blue; place 100 100"
print #main, "size 1"
print #main, "when leftButtonUp [checkpos]"
print #main, "when rightButtonUp [cls]"
wait
[quit]
close #main
end
[cls]
print #main, "when leftButtonUp [checkpos]"
print #main, "when mouseMove"
print #main, "cls"
wait
[checkpos]
xs = MouseX
ys = MouseY
print #main, "when mouseMove [mouse]"
wait
[mouse]
'draw line from first point to curent mouse position
print #main, "color white" ' erase old line
print #main, "line "; str$(xs); " "; str$(ys); " "; _
str$(xe); " "; str$(ye)
xe = MouseX
ye = MouseY
print #main, "color blue" ' draw new line
print #main, "line "; str$(xs); " "; str$(ys); " "; _
str$(xe); " "; str$(ye)
Angle = GetAngle(xs,ys,xe,ye)
print #main, "place 20 20"
print #main, "\ "
print #main, "place 20 20"
print #main, "\";Angle
print #main, "discard"
wait
'>>>>>>> FUNCTIONS AND SUBS <<<<<<<<<
function GetAngle(xs,ys,xe,ye)
'xs = start point x
'ys = start point y
'xe = end point x
'ye = end point y
'0 degrees is to the right
if xe > xs and ye > ys then ' above 270
c = GetDistance(xs,ys,xe,ye)
a = ye-ys
An = Degrees(asn(a/c))
Ang = 90-An
Angle = 270+Ang
end if
if xe < xs and ye > ys then ' above 180; less than 270
c = GetDistance(xs,ys,xe,ye)
a = ye-ys
An = Degrees(asn(a/c))
Ang = 90-An
Angle = 270-Ang
end if
if xe < xs and ye < ys then ' above 90; less than 180
c = GetDistance(xs,ys,xe,ye)
a = abs(ye-ys)
An = Degrees(asn(a/c))
Ang = 90-An
Angle = 90+Ang
end if
if xe > xs and ye < ys then ' above 0; less than 90
c = GetDistance(xs,ys,xe,ye)
a = abs(ye-ys)
An = Degrees(asn(a/c))
Ang = 90-An
Angle = 90-Ang
end if
'if Angle = 0 then angle is on x or y plane
if Angle > 0 then goto [GetAngleEnd]
if xe > xs then Angle = 0
if ye < ys then Angle = 90
if xe < xs then Angle = 180
if ye > ys then Angle = 270
[GetAngleEnd]
GetAngle = Angle
end function
function GetDistance(Ax,Ay,Bx,By)
Distance = sqr(((Ax-Bx)^2)+((Ay-By)^2))
end function
function ChangeSign(x)
ChangeSign = x - (x*2)
end function
'These Functions by Tom Nally
function Degrees(radian)
'convert Radians to Degrees
pi = 3.1415926535897932384626433832795
Degrees = ((radian*360)/(pi*2))
end function
function Radian(Degree)
'convert Degrees to Radians
pi = 3.1415926535897932384626433832795
Radian = ((Degree/360)*(pi*2))
end function