GRASS 8 Programmer's Manual 8.5.0(2026)-8d6ceba290
Loading...
Searching...
No Matches
area_poly2.c
Go to the documentation of this file.
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
14#include <grass/gis.h>
15
16/*!
17 * \brief Calculates planimetric polygon area.
18 *
19 * \param x array of x values
20 * \param y array of y values
21 * \param n number of x,y pairs
22
23 * \return polygon area in map units
24 */
25double G_planimetric_polygon_area(const double *x, const double *y, int n)
26{
27 double x1, y1, x2, y2, xshift, yshift;
28 double area;
29
30 /* Get a reference point close to the polygon as new origin.
31 * The first point would be good enough, particularly for small
32 * polygons. The average of the first and the mid-point is a fast
33 * approximation of a reference point with reduced distance to the
34 * ring's vertices, also for larger rings.
35 *
36 * Shift coordinates towards this reference point to make
37 * calculation of the signed area more robust by increasing the
38 * accuracy for given fp precision limits.
39 *
40 * Considering the basic formula
41 * area += (x2 - x1) * (y2 + y1)
42 * the shift is in theory only needed for addition, not subtraction,
43 * but does no harm and is kept for symmetry treating x and y coords.
44 *
45 * Keep in sync with dig_find_area_poly() in lib/vector/diglib/poly.c */
46
47 xshift = (x[0] + x[n / 2]) / 2.;
48 yshift = (y[0] + y[n / 2]) / 2.;
49
50 x2 = x[n - 1] - xshift;
51 y2 = y[n - 1] - yshift;
52
53 area = 0;
54 while (--n >= 0) {
55 x1 = x2;
56 y1 = y2;
57
58 x2 = *x++;
59 y2 = *y++;
60
61 x2 -= xshift;
62 y2 -= yshift;
63
64 area += (y2 + y1) * (x2 - x1);
65 }
66
67 if ((area /= 2.0) < 0.0)
68 area = -area;
69
70 return area;
71}
double G_planimetric_polygon_area(const double *x, const double *y, int n)
Calculates planimetric polygon area.
Definition area_poly2.c:25
#define x