process_rite.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /****************************************************************************
  2. *
  3. * MODULE: r.buffer
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. *
  7. * PURPOSE: This program creates distance zones from non-zero
  8. * cells in a grid layer. Distances are specified in
  9. * meters (on the command-line). Window does not have to
  10. * have square cells. Works both for planimetric
  11. * (UTM, State Plane) and lat-long.
  12. *
  13. * COPYRIGHT: (C) 2005 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (>=v2). Read the file COPYING that comes with GRASS
  17. * for details.
  18. *
  19. ****************************************************************************/
  20. #include "distance.h"
  21. int process_right(int from_row, int to_row, int start_col, int first_zone)
  22. {
  23. register int i, col, cur_zone;
  24. register MAPTYPE *to_ptr, *from_ptr;
  25. register int ncols;
  26. register int xcol;
  27. register int incr;
  28. register int farthest;
  29. /* find cells to the right
  30. * stop at right edge, or when ncols is bigger than the last zone,
  31. * or when we see a 1 in the map
  32. */
  33. xcol = col = start_col;
  34. from_ptr = map + MAPINDEX(from_row, col);
  35. to_ptr = map + MAPINDEX(to_row, col);
  36. farthest = distances[ndist - 1].ncols;
  37. /* planimetric grids will look for ncols^2
  38. * and can use fact that (n+1)^2 = n^2 + 2n + 1
  39. */
  40. if (window.proj != PROJECTION_LL)
  41. incr = 1;
  42. else
  43. incr = 0;
  44. ncols = 0;
  45. while (1) {
  46. if (col >= window.cols - 1) { /* global wrap-around */
  47. if (!wrap_ncols)
  48. return window.cols;
  49. col = -1;
  50. ncols += wrap_ncols - 1;
  51. from_ptr = map + MAPINDEX(from_row, -1);
  52. to_ptr = map + MAPINDEX(to_row, -1);
  53. }
  54. col++;
  55. xcol++;
  56. if (*++from_ptr == 1)
  57. break;
  58. if (incr) {
  59. ncols += incr;
  60. incr += 2;
  61. }
  62. else
  63. ncols++;
  64. if (ncols > farthest)
  65. break;
  66. /* convert 1,2,3,4 to -1,0,1,2 etc. 0 becomes ndist */
  67. if ((cur_zone = *++to_ptr))
  68. cur_zone -= ZONE_INCR;
  69. else
  70. cur_zone = ndist;
  71. /* find the first zone that is closer than the current value */
  72. for (i = first_zone; i < cur_zone; i++) {
  73. if (distances[i].ncols >= ncols) {
  74. *to_ptr = (first_zone = i) + ZONE_INCR;
  75. break;
  76. }
  77. }
  78. }
  79. while (xcol <= maxcol && *from_ptr++ != 1)
  80. xcol++;
  81. return xcol;
  82. }