xatan.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include "globals.h"
  6. #include "expression.h"
  7. #include "func_proto.h"
  8. /**********************************************************************
  9. atan(x) range [-90,90]
  10. atan(x,y) = atan(y/x) range[0,360]
  11. if floating point exception occurs during the evaluation of atan(x)
  12. the result is NULL
  13. note: result is in degrees
  14. **********************************************************************/
  15. #define RADIANS_TO_DEGREES (180.0 / M_PI)
  16. int f_atan(int argc, const int *argt, void **args)
  17. {
  18. DCELL *res = args[0];
  19. DCELL *arg1 = args[1];
  20. DCELL *arg2;
  21. int i;
  22. if (argc < 1)
  23. return E_ARG_LO;
  24. if (argc > 2)
  25. return E_ARG_HI;
  26. if (argt[0] != DCELL_TYPE)
  27. return E_RES_TYPE;
  28. if (argt[1] != DCELL_TYPE)
  29. return E_ARG_TYPE;
  30. if (argc > 1 && argt[2] != DCELL_TYPE)
  31. return E_ARG_TYPE;
  32. arg2 = (argc > 1) ? args[2] : NULL;
  33. for (i = 0; i < columns; i++)
  34. if (IS_NULL_D(&arg1[i]))
  35. SET_NULL_D(&res[i]);
  36. else if (argc > 1 && IS_NULL_D(&arg2[i]))
  37. SET_NULL_D(&res[i]);
  38. else {
  39. floating_point_exception = 0;
  40. if (argc == 1)
  41. res[i] = RADIANS_TO_DEGREES * atan(arg1[i]);
  42. else {
  43. res[i] = RADIANS_TO_DEGREES * atan2(arg2[i], arg1[i]);
  44. if (res[i] < 0)
  45. res[i] += 360.0;
  46. }
  47. if (floating_point_exception)
  48. SET_NULL_D(&res[i]);
  49. }
  50. return 0;
  51. }