File: poly.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (122 lines) | stat: -rw-r--r-- 2,735 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* $Id: poly.c,v 1.6 2003/09/11 16:12:48 markus Exp $
*
****************************************************************************
*
* MODULE:       Vector library 
*   	    	
* AUTHOR(S):    Original author CERL, probably Dave Gerdes.
*               Update to GRASS 5.7 Radim Blazek.
*
* PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
*
* COPYRIGHT:    (C) 2001 by the GRASS Development Team
*
*               This program is free software under the GNU General Public
*   	    	License (>=v2). Read the file COPYING that comes with GRASS
*   	    	for details.
*
*****************************************************************************/
#include <math.h>
#include "Vect.h"


#ifndef HUGE_VAL
#define HUGE_VAL 9999999999999.0
#endif

/*
**  fills BPoints (must be inited previously) by points from imput
**  array LPoints. Each imput points must have at least 2 points.
**   
**  returns number of points or -1 on error
*/


int 
dig_get_poly_points ( int n_lines,
		      struct line_pnts **LPoints,
		      int *direction,             /* line direction: > 0 or < 0 */
		      struct line_pnts *BPoints
		      )
{
    register int i, j, point, start, end, inc;
    struct line_pnts *Points;
    int n_points;

    BPoints->n_points = 0;

    if ( n_lines < 1 ) { return 0; } 
  
    /* Calc required space */
    n_points = 0;
    for (i = 0; i < n_lines; i++) {
        Points = LPoints[i];
        n_points += Points->n_points - 1; /* each line from first to last - 1 */
    }
    n_points++; /* last point */ 
      
    if (0 > dig_alloc_points (BPoints, n_points))
	return (-1);

    point = 0; j = 0;
    for (i = 0; i < n_lines; i++) {
        Points = LPoints[i];
        if (direction[i] > 0) {
	  start = 0;
	  end   = Points->n_points - 1;
	  inc   = 1;
	} else {
	  start = Points->n_points - 1;
	  end   = 0;
	  inc   = -1;
	}

        for ( j = start; j != end; j += inc) {
	  BPoints->x[point] = Points->x[j];
	  BPoints->y[point] = Points->y[j];
	}
	point++;
    }
    /* last point */
    BPoints->x[point] = Points->x[j];
    BPoints->y[point] = Points->y[j];
    
    BPoints->n_points = n_points;

    return (BPoints->n_points);
}

/*
**  Calculate area size for polygon. 
**
**  Total area is positive for clockwise and negative for counter clockwise ?
*/
int 
dig_find_area_poly (
    struct line_pnts *Points,
    double *totalarea)
{
  int i;
  double *x, *y;
  double tot_area, sum_area;


  *totalarea = 0.;

  tot_area = 0.0;

  x = Points->x;
  y = Points->y;

  sum_area = 0.0;
  for (i = 1; i < Points->n_points; i++)
    {
      sum_area += (x[i] - x[i - 1]) * (y[i] + y[i - 1]);
    }
  tot_area += sum_area;

  *totalarea = 0.5 * tot_area;

  return (0);
}