xatan.c 1.3 KB

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