Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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
#2
QB64 has a built-in _PI function:
a _FLOAT value with an optional multiplier parameter.
see HELP or Wiki for more information
Reply
#3
Who uses Gradians?

1/100th of 90 degrees, I had to look it up. So a circle in Grades is 400.
b = b + ...
Reply
#4
@BSpinoza That may well be but functions have an overhead no matter how small when compared to a CONST. Given enough calls it could become noticeable to someone on a Raspberry Pi.

@bplus Hey that's just for completeness. Anyway, on the trig library that I am not posting each of the functions can be called with either Radians, Degrees or Grade. But now that most of the functions I created for my trig library are already implemented in QB64 I see no point in posting it. So I just posted what I thought others might be interested in.

TR
Reply
#5
To me, everyone has missed the mark as far as measures of an angle.

We should use FOC's = Fraction Of a Circle, range from 0 to 1, thus a semi-circle is .5, a 90 degree angle is .25, 45 degrees, .125. 2 would be 2 circles...

Way more intuitive than the crap we use now, 360 degrees is a bit arbitrary and should go the way of the Babylonians from whence it came.

Divide a Radian measure by 2*Pi and you have the FOC, divide a Degree angle by 360 and you have the FOC.
b = b + ...
Reply




Users browsing this thread: 2 Guest(s)