GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
xatan.c
Go to the documentation of this file.
1 
2 #include <stdlib.h>
3 #include <math.h>
4 
5 #include <grass/gis.h>
6 #include <grass/raster.h>
7 #include <grass/calc.h>
8 
9 /**********************************************************************
10 atan(x) range [-90,90]
11 atan(x,y) = atan(y/x) range[0,360]
12 
13  if floating point exception occurs during the evaluation of atan(x)
14  the result is NULL
15 
16  note: result is in degrees
17 **********************************************************************/
18 
19 #define RADIANS_TO_DEGREES (180.0 / M_PI)
20 
21 int f_atan(int argc, const int *argt, void **args)
22 {
23  DCELL *res = args[0];
24  DCELL *arg1 = args[1];
25  DCELL *arg2;
26  int i;
27 
28  if (argc < 1)
29  return E_ARG_LO;
30  if (argc > 2)
31  return E_ARG_HI;
32 
33  if (argt[0] != DCELL_TYPE)
34  return E_RES_TYPE;
35 
36  if (argt[1] != DCELL_TYPE)
37  return E_ARG_TYPE;
38 
39  if (argc > 1 && argt[2] != DCELL_TYPE)
40  return E_ARG_TYPE;
41 
42  arg2 = (argc > 1) ? args[2] : NULL;
43 
44  for (i = 0; i < columns; i++)
45  if (IS_NULL_D(&arg1[i]))
46  SET_NULL_D(&res[i]);
47  else if (argc > 1 && IS_NULL_D(&arg2[i]))
48  SET_NULL_D(&res[i]);
49 
50  else {
52  if (argc == 1)
53  res[i] = RADIANS_TO_DEGREES * atan(arg1[i]);
54  else {
55  res[i] = RADIANS_TO_DEGREES * atan2(arg2[i], arg1[i]);
56  if (res[i] < 0)
57  res[i] += 360.0;
58  }
60  SET_NULL_D(&res[i]);
61  }
62 
63  return 0;
64 }
double DCELL
Definition: gis.h:614
#define NULL
Definition: ccmath.h:32
Definition: calc.h:13
int columns
Definition: calc.c:12
volatile int floating_point_exception
Definition: calc.c:9
#define DCELL_TYPE
Definition: raster.h:13
#define RADIANS_TO_DEGREES
Definition: xatan.c:19
Definition: calc.h:12
#define IS_NULL_D(x)
Definition: calc.h:30
int f_atan(int argc, const int *argt, void **args)
Definition: xatan.c:21
#define SET_NULL_D(x)
Definition: calc.h:34