poly.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Original author CERL, probably Dave Gerdes.
  7. * Update to GRASS 5.7 Radim Blazek.
  8. *
  9. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <math.h>
  19. #include <grass/Vect.h>
  20. #ifndef HUGE_VAL
  21. #define HUGE_VAL 9999999999999.0
  22. #endif
  23. /*
  24. ** fills BPoints (must be inited previously) by points from imput
  25. ** array LPoints. Each imput points must have at least 2 points.
  26. **
  27. ** returns number of points or -1 on error
  28. */
  29. int
  30. dig_get_poly_points ( int n_lines,
  31. struct line_pnts **LPoints,
  32. int *direction, /* line direction: > 0 or < 0 */
  33. struct line_pnts *BPoints
  34. )
  35. {
  36. register int i, j, point, start, end, inc;
  37. struct line_pnts *Points;
  38. int n_points;
  39. BPoints->n_points = 0;
  40. if ( n_lines < 1 ) { return 0; }
  41. /* Calc required space */
  42. n_points = 0;
  43. for (i = 0; i < n_lines; i++) {
  44. Points = LPoints[i];
  45. n_points += Points->n_points - 1; /* each line from first to last - 1 */
  46. }
  47. n_points++; /* last point */
  48. if (0 > dig_alloc_points (BPoints, n_points))
  49. return (-1);
  50. point = 0; j = 0;
  51. for (i = 0; i < n_lines; i++) {
  52. Points = LPoints[i];
  53. if (direction[i] > 0) {
  54. start = 0;
  55. end = Points->n_points - 1;
  56. inc = 1;
  57. } else {
  58. start = Points->n_points - 1;
  59. end = 0;
  60. inc = -1;
  61. }
  62. for ( j = start; j != end; j += inc) {
  63. BPoints->x[point] = Points->x[j];
  64. BPoints->y[point] = Points->y[j];
  65. }
  66. point++;
  67. }
  68. /* last point */
  69. BPoints->x[point] = Points->x[j];
  70. BPoints->y[point] = Points->y[j];
  71. BPoints->n_points = n_points;
  72. return (BPoints->n_points);
  73. }
  74. /*
  75. ** Calculate area size for polygon.
  76. **
  77. ** Total area is positive for clockwise and negative for counter clockwise ?
  78. */
  79. int
  80. dig_find_area_poly (
  81. struct line_pnts *Points,
  82. double *totalarea)
  83. {
  84. int i;
  85. double *x, *y;
  86. double tot_area, sum_area;
  87. *totalarea = 0.;
  88. tot_area = 0.0;
  89. x = Points->x;
  90. y = Points->y;
  91. sum_area = 0.0;
  92. for (i = 1; i < Points->n_points; i++)
  93. {
  94. sum_area += (x[i] - x[i - 1]) * (y[i] + y[i - 1]);
  95. }
  96. tot_area += sum_area;
  97. *totalarea = 0.5 * tot_area;
  98. return (0);
  99. }