Small Collection of Angle Conversion Functions
#1
Back in the day I wrote a whole trigonometry library. Looking on the wiki I see that most of what is in that library are incorporated into QB64. Most...

So what we have here are 2 constants, 3 ordinary functions and 4 conversion functions. The conversation functions deal with converting to and from another standard angle measurement type - Gradians (aka Grade). Conversions between radians and degrees are already covered in QB64. The other 3 functions just make sure that angles lie within specified bounds.

Code: (Select All)
CONST PI = 3.141592653589793#
CONST PITimes2 = 6.283185307179586#

FUNCTION NormaliseRadians#(Radians AS DOUBLE)
    DO WHILE Radians > PI
        Radians = Radians - PITimes2
    LOOP
    DO WHILE Radians < -PI
        Radians = Radians + PITimes2
    LOOP
    NormaliseRadians# = Radians
END FUNCTION

FUNCTION NormaliseDegrees#(Degrees AS DOUBLE)
    DO WHILE Degrees > 180
        Degrees = Degrees - 360
    LOOP
    DO WHILE Degrees < -180
        Degrees = Degrees + 360
    LOOP
    NormaliseDegrees# = Degrees
END FUNCTION

FUNCTION NormaliseGrade#(Grade AS DOUBLE)
    DO WHILE Grade > 200
        Grade = Grade - 400
    LOOP
    DO WHILE Grade < -200
        Grade = Grade + 400
    LOOP
    NormaliseGrade# = Grade
END FUNCTION

FUNCTION RadiansToGrade#(Radians AS DOUBLE)
    RadiansToGrade# = (NormaliseRadians#(Radians) * (200 / PI))
END FUNCTION

FUNCTION DegreesToGrade#(Degrees AS DOUBLE)
    DegreesToGrade# = (NormaliseDegrees#(Degrees) * 1.111111111111111)
END FUNCTION

FUNCTION GradeToRadians#(Grade AS DOUBLE)
    GradeToRadians# = (NormaliseGrade#(Grade) * (PI / 200))
END FUNCTION

FUNCTION GradeToDegrees#(Grade AS DOUBLE)
    GradeToDegrees# = (NormaliseGrade#(Grade) * .9)
END FUNCTION


TR
Reply


Messages In This Thread
Small Collection of Angle Conversion Functions - by TarotRedhand - 05-16-2022, 08:26 AM



Users browsing this thread: 1 Guest(s)