FB II Compiler

PG PRO

Debugging

Memory

System

Mathematics

Resources

Disk I/O

Windows

Controls

Menus

Mouse

Keyboard

Text

Fonts

Drawing

Sound

Clipboard

Printing

Communication

ASM

Made with FB

MATHEMATICS

Calculate the angle of a slope


Use the ATN function (Arc Tangent). The tangent of an angle is its rise (Y) over its run (X) or Y/X. The ArcTan gives you the angle given the rise over the run.

Following is an FN for FB2 that should help. I stuck it into a little test program so you can see how it works.

A few notes:
--- The FN output assumes the regular trig quadrants with 0 degrees pointing right and 90 degrees pointing straight up. If you'd like them to represent screen coordinates where larger Y values are lower rather than higher simply reverse the Y2-Y1 calc to Y1-Y2.
--- Because the only input to ATN is a ratio of rise over run you have to do some additional work to to sort out the four quadrants. All the ATN has to work with are the signs of the x and y differences so -y/+x (LOWER RIGHT quadrant) looks the same as +y/-x (UPPER LEFT quadrant). That's what the little quadrant handler LONG IFs are for.
--- Sometimes results will come out as negative values (such as -45^o) so the last LONG IF wraps them back around (315^o)
--- It should be easy to convert this to single precision if you want
--- Although it would seem more elegant to do the little TestRatio calculation inside the ATN () it won't work that way. I think FB makes some assumptions about integer math that mess this up.
--- This could be changed to work directly from a Rectangle record or a pair of Point records.
'--------------
'GetAngle# from X,Y coordinates.  Russ Pagel 11/26/99
'Returns double precision angle in degrees.

LOCAL FN GetAngle#(X1,Y1,X2,Y2)
  DIM pi#
  DIM AngleRads#
  DIM AngleDegrees#
  DIM TestRatio#
  
  pi#=ATN(1)<<2                                   'get the value of pi
  TestRatio#=(Y2-Y1)/(X2-X1)                      'precalculate this
  AngleRads#=ATN(TestRatio#)                      'get angle in radians
  AngleDegrees#=AngleRads# * 180/pi#              'convert to degrees
  
  LONG IF SGN(X2-X1)=-1 OR SGN(Y2-Y1)=-1          'handle 1st three quadrants
    AngleDegrees#=AngleDegrees#+180
  END IF
  
  LONG IF SGN(X2-X1)<>-1 AND SGN(Y2-Y1)=-1        'handle fourth quadrant
    AngleDegrees#=AngleDegrees#-180
  END IF
  
  LONG IF SGN(AngleDegrees#)=-1                   'Convert to positive values
    AngleDegrees#=AngleDegrees#+360
  END IF
END FN = AngleDegrees#

"Main"
WINDOW 1,"TEST",(35,50)-(500,500)
PRINT "Command . to exit"
CALL TEXTSIZE(9)
DIM X1,X2,Y1,Y2,counter
DIM pi#,angle#,result#

X1=0:Y1=0

pi#=ATN(1)<<2

FOR counter=0 TO 360 STEP 10
  angle#=counter/180*pi#
  Y2=SIN(angle#)*32000
  X2=COS(angle#)*32000
  result#=FN GetAngle#(X1,Y1,X2,Y2)
  PRINT counter,SGN(X2),SGN(Y2),result#
  
NEXT
DO
  HANDLEEVENTS
UNTIL progends
Russ Pagel