area_poly2.c 913 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * \file area_poly2.c
  3. *
  4. * \brief GIS Library - Planimetric polygon area calculation routines.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <grass/gis.h>
  16. /**
  17. * \brief Calculates planimetric polygon area.
  18. *
  19. * \param[in] x array of x values
  20. * \param[in] y array of y values
  21. * \param[in] n number of x,y pairs
  22. * \return double
  23. */
  24. double G_planimetric_polygon_area(const double *x, const double *y, int n)
  25. {
  26. double x1, y1, x2, y2;
  27. double area;
  28. x2 = x[n - 1];
  29. y2 = y[n - 1];
  30. area = 0;
  31. while (--n >= 0) {
  32. x1 = x2;
  33. y1 = y2;
  34. x2 = *x++;
  35. y2 = *y++;
  36. area += (y2 + y1) * (x2 - x1);
  37. }
  38. if ((area /= 2.0) < 0.0)
  39. area = -area;
  40. return area;
  41. }