xgraph.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include "globals.h"
  5. #include "expression.h"
  6. #include "func_proto.h"
  7. /****************************************************************
  8. graph(x, x1,y1, x2,y2, ... xn,yn) returns y value based on graph
  9. described by the x,y pairs.
  10. ****************************************************************/
  11. int c_graph(int argc, int *argt)
  12. {
  13. int i;
  14. if (argc < 3)
  15. return E_ARG_LO;
  16. if (argc % 2 == 0)
  17. return E_ARG_NUM;
  18. for (i = 0; i <= argc; i++)
  19. argt[i] = DCELL_TYPE;
  20. return 0;
  21. }
  22. int f_graph(int argc, const int *argt, void **args)
  23. {
  24. DCELL **argz = (DCELL **) args;
  25. DCELL *res = argz[0];
  26. int n = (argc - 1) / 2;
  27. int i, j;
  28. if (argc < 3)
  29. return E_ARG_LO;
  30. if (argc % 2 == 0)
  31. return E_ARG_NUM;
  32. if (argt[0] != DCELL_TYPE)
  33. return E_RES_TYPE;
  34. for (i = 1; i <= argc; i++)
  35. if (argt[i] != DCELL_TYPE)
  36. return E_ARG_TYPE;
  37. for (i = 0; i < columns; i++) {
  38. #define X(j) (argz[2 + 2 * (j) + 0][i])
  39. #define Y(j) (argz[2 + 2 * (j) + 1][i])
  40. #define x (argz[1][i])
  41. if (IS_NULL_D(&x))
  42. goto null;
  43. for (j = 0; j < n; j++)
  44. if (IS_NULL_D(&X(j)))
  45. goto null;
  46. for (j = 0; j < n - 1; j++)
  47. if (X(j + 1) <= X(j))
  48. goto null;
  49. if (x <= X(0)) {
  50. if (IS_NULL_D(&Y(0)))
  51. goto null;
  52. res[i] = Y(0);
  53. continue;
  54. }
  55. if (x >= X(n - 1)) {
  56. if (IS_NULL_D(&Y(n - 1)))
  57. goto null;
  58. res[i] = Y(n - 1);
  59. continue;
  60. }
  61. for (j = 0; j < n - 1; j++) {
  62. if (x > X(j + 1))
  63. continue;
  64. if (IS_NULL_D(&Y(j)) || IS_NULL_D(&Y(j + 1)))
  65. goto null;
  66. res[i] =
  67. Y(j) + (x - X(j)) * (Y(j + 1) - Y(j)) / (X(j + 1) - X(j));
  68. break;
  69. }
  70. continue;
  71. null:
  72. SET_NULL_D(&res[i]);
  73. }
  74. return 0;
  75. }