Skip to content
gicking edited this page Feb 10, 2018 · 3 revisions

back to Command Reference / Math

Description

Calculates the arctangent (in radians) of two arguments. The result will be between -PI and PI (see constants. For a single argument (but with ambiguity) use atan() instead.

Note: for conversion radians to degrees, use deg2rad().

Inclusion

  • defined in misc.h
  • auto-loaded in main_general.h
  • no #define required

Syntax

a = atan2(y,x)

Parameters

  • input:

    • y: proportion of the y-coordinate (float)
    • x: proportion of the x-coordinate (float)
  • output:

    • none

Returns

  • The principal arctangent of y/x (float).

Example Code

The below code prints a table for y=atan2(x) between x=-10..10 and y=-10..10 in degrees. Note that the example requires option #define USE_FTOA in file config.h for floating point output.

#include "main_general.h"
#include "uart1.h"
#include "putchar.h"

void setup() {

  float x,y,z;
  char  s1[20], s2[20], s3[20];
  
  // init UART and printf()
  UART1_begin(115200);
  putcharAttach(UART1_write);
  
  // allow the terminal to launch
  sw_delay(1000);
  
  // print math table
  for (y=-10.0; y<=10.0; y+=0.5) {
    for (x=-10.0; x<=10.0; x+=0.5) {
      z = rad2deg(atan2(y,x));
      printf("%s  %s  %s\n", floatToString(s1,x,2), floatToString(s2,y,2), floatToString(s3,z,2));
    }
    printf("\n");
  }
  
}

void loop() {
  // dummy
}

Relevant Tutorial

  • tbd

See also

Clone this wiki locally