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