Library contents

C atan

In C, atan returns the inverse tangent of a slope as an angle in radians. A slope of 1 gives π/4, or 45 degrees.

A simple example

atan

  1. Start with Slope 0
  2. Do this Find the angle of a flat line
  3. Result 0 radians
A horizontal line has slope zero. Its angle relative to the horizontal is zero radians.
lesson.c
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.15g\n", (double) (atan(0.0)));
}
Output
0

Syntax

C syntax
atan(slope)
Run these examples in C

Save one example at a time as lesson.c. Use C17 and a Unix-style shell.

cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lesson

Turn a ramp slope into an angle

A surveyor describes a platform or ramp using its rise and horizontal run. The drawing tool must convert between slope and angle, including the level case where the rise is zero.

From the programdouble slope = 1.0
Result0.785
lesson.c
#include <math.h>
#include <stdio.h>

int main(void) {
    double slope = 1.0;

    printf("%.3f\n", atan(slope));
    return 0;
}
Output
0.785

Equal rise and run give a slope of one and an angle of about 0.785 radians.

Advertisements

Point a map label up a street

On a square-grid map a stop is 4 blocks east and 2 north of the depot. A designer wants the label to follow the route’s angle above the east axis.

GivenEast 4 · north 2
Worked result0.464 rad

Positive horizontal distance allows either the rise/run form or the two-coordinate form.

Aim along a horizontal corridor

A floor-plan arrow points 6 m right and never changes height. The drawing program stores the angle measured upward from the rightward axis.

GivenRun 6 m · rise 0 m
Worked result0.000 rad

A point directly to the right has a direction of zero radians.

What to watch for

For a direction that must distinguish opposite quadrants, keep both coordinates and use atan2.

Documentation for atan