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