GRASS Programmer's Manual  6.5.svn(2014)-r66266
diglib/poly.c
Go to the documentation of this file.
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
21
22 #ifndef HUGE_VAL
23 #define HUGE_VAL 9999999999999.0
24 #endif
25
26 /*
27  ** fills BPoints (must be inited previously) by points from imput
28  ** array LPoints. Each imput points must have at least 2 points.
29  **
30  ** returns number of points or -1 on error
31  */
32
33
34 int dig_get_poly_points(int n_lines, struct line_pnts **LPoints, int *direction, /* line direction: > 0 or < 0 */
35  struct line_pnts *BPoints)
36 {
37  register int i, j, point, start, end, inc;
38  struct line_pnts *Points;
39  int n_points;
40
41  BPoints->n_points = 0;
42
43  if (n_lines < 1) {
44  return 0;
45  }
46
47  /* Calc required space */
48  n_points = 0;
49  for (i = 0; i < n_lines; i++) {
50  Points = LPoints[i];
51  n_points += Points->n_points - 1; /* each line from first to last - 1 */
52  }
53  n_points++; /* last point */
54
55  if (0 > dig_alloc_points(BPoints, n_points))
56  return (-1);
57
58  point = 0;
59  j = 0;
60  for (i = 0; i < n_lines; i++) {
61  Points = LPoints[i];
62  if (direction[i] > 0) {
63  start = 0;
64  end = Points->n_points - 1;
65  inc = 1;
66  }
67  else {
68  start = Points->n_points - 1;
69  end = 0;
70  inc = -1;
71  }
72
73  for (j = start; j != end; j += inc) {
74  BPoints->x[point] = Points->x[j];
75  BPoints->y[point] = Points->y[j];
76  }
77  point++;
78  }
79  /* last point */
80  BPoints->x[point] = Points->x[j];
81  BPoints->y[point] = Points->y[j];
82
83  BPoints->n_points = n_points;
84
85  return (BPoints->n_points);
86 }
87
88 /*
89  ** Calculate area size for polygon.
90  **
91  ** Total area is positive for clockwise and negative for counter clockwise ?
92  */
93 int dig_find_area_poly(struct line_pnts *Points, double *totalarea)
94 {
95  int i;
96  double *x, *y;
97  double tot_area, sum_area;
98
99
100  *totalarea = 0.;
101
102  tot_area = 0.0;
103
104  x = Points->x;
105  y = Points->y;
106
107  sum_area = 0.0;
108  for (i = 1; i < Points->n_points; i++) {
109  sum_area += (x[i] - x[i - 1]) * (y[i] + y[i - 1]);
110  }
111  tot_area += sum_area;
112
113  *totalarea = 0.5 * tot_area;
114
115  return (0);
116 }
int dig_get_poly_points(int n_lines, struct line_pnts **LPoints, int *direction, struct line_pnts *BPoints)
Definition: diglib/poly.c:34
struct triple * point