area_poly2.c 898 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*!
  2. * \file lib/gis/area_poly2.c
  3. *
  4. * \brief GIS Library - Planimetric polygon area calculation routines.
  5. *
  6. * (C) 2001-2009 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 Original author CERL
  12. */
  13. #include <grass/gis.h>
  14. /*!
  15. * \brief Calculates planimetric polygon area.
  16. *
  17. * \param x array of x values
  18. * \param y array of y values
  19. * \param n number of x,y pairs
  20. * \return polygon area in map units
  21. */
  22. double G_planimetric_polygon_area(const double *x, const double *y, int n)
  23. {
  24. double x1, y1, x2, y2;
  25. double area;
  26. x2 = x[n - 1];
  27. y2 = y[n - 1];
  28. area = 0;
  29. while (--n >= 0) {
  30. x1 = x2;
  31. y1 = y2;
  32. x2 = *x++;
  33. y2 = *y++;
  34. area += (y2 + y1) * (x2 - x1);
  35. }
  36. if ((area /= 2.0) < 0.0)
  37. area = -area;
  38. return area;
  39. }