Search code examples
integertrigonometryangleinteger-divisionnextion

Nextion: Calculate inverse tan (arctan) without trig functions or floating point


I am programming an interface on this Nextion display. Specifically, I am trying to program a digital dial that rotates around a centre point.

These screens are great, but they don’t have any trig functions, they don’t have floating point and can’t rotate images. Just sharing these specs for background on my constraints, don’t worry about the screen.

I need a way to convert X,Y coordinates into a 0-359 output.

Does anyone know any hacks or tricks using integer math to approximate an angle?

All I have right now is gradients adjusted for each quartile, but it’s not linear because I’m just doing rise/run.


Solution

  • Thankyou @jhole for pointing me in the direction of the arctan approximation formula. I ended up modifying it a bit to fit what I needed for my Nextion screen, including inputs for X & Y coordinates, outputting in degrees, and not making the variables too large to overflow (opting for a x10 multiplier to allow 1 digit for rounding).

    I was able to get it up and running with this formula that outputs degrees (screenshot from Desmos where 'a' = 380 and 'b' = 400):

    enter image description here

    'a' is Y coordinate and 'b' is the X coordinate. I've included in the picture the benchmark trig function and you can see it's pretty close. I wanted the result to be 10x multiplied to allow for rounding since everything below the decimal point is lost on the Nextion.

    I really wanted to remove those top two brackets to reduce everything into one big fraction that has one divide, but I found that the variables get way too large, so I found this to be a great sweet spot of speed and accuracy.

    Crucial things to note with the implementation of this formula is that it only works where RISE <= RUN, and 'a' and 'b' are both positive. To get a measurement in the top and bottom four 45 degree segments I had to flip around the RISE/RUN variables. From there I was able to add/subtract from the 0,180,270,360 values depending on what quadrant you are touching.