diglib/poly.c

Go to the documentation of this file.
00001 /*
00002  ****************************************************************************
00003  *
00004  * MODULE:       Vector library 
00005  *              
00006  * AUTHOR(S):    Original author CERL, probably Dave Gerdes.
00007  *               Update to GRASS 5.7 Radim Blazek.
00008  *
00009  * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
00010  *
00011  * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00012  *
00013  *               This program is free software under the GNU General Public
00014  *              License (>=v2). Read the file COPYING that comes with GRASS
00015  *              for details.
00016  *
00017  *****************************************************************************/
00018 #include <math.h>
00019 #include <grass/Vect.h>
00020 
00021 
00022 #ifndef HUGE_VAL
00023 #define HUGE_VAL 9999999999999.0
00024 #endif
00025 
00026 /*
00027  **  fills BPoints (must be inited previously) by points from imput
00028  **  array LPoints. Each imput points must have at least 2 points.
00029  **   
00030  **  returns number of points or -1 on error
00031  */
00032 
00033 
00034 int dig_get_poly_points(int n_lines, struct line_pnts **LPoints, int *direction,        /* line direction: > 0 or < 0 */
00035                         struct line_pnts *BPoints)
00036 {
00037     register int i, j, point, start, end, inc;
00038     struct line_pnts *Points;
00039     int n_points;
00040 
00041     BPoints->n_points = 0;
00042 
00043     if (n_lines < 1) {
00044         return 0;
00045     }
00046 
00047     /* Calc required space */
00048     n_points = 0;
00049     for (i = 0; i < n_lines; i++) {
00050         Points = LPoints[i];
00051         n_points += Points->n_points - 1;       /* each line from first to last - 1 */
00052     }
00053     n_points++;                 /* last point */
00054 
00055     if (0 > dig_alloc_points(BPoints, n_points))
00056         return (-1);
00057 
00058     point = 0;
00059     j = 0;
00060     for (i = 0; i < n_lines; i++) {
00061         Points = LPoints[i];
00062         if (direction[i] > 0) {
00063             start = 0;
00064             end = Points->n_points - 1;
00065             inc = 1;
00066         }
00067         else {
00068             start = Points->n_points - 1;
00069             end = 0;
00070             inc = -1;
00071         }
00072 
00073         for (j = start; j != end; j += inc) {
00074             BPoints->x[point] = Points->x[j];
00075             BPoints->y[point] = Points->y[j];
00076         }
00077         point++;
00078     }
00079     /* last point */
00080     BPoints->x[point] = Points->x[j];
00081     BPoints->y[point] = Points->y[j];
00082 
00083     BPoints->n_points = n_points;
00084 
00085     return (BPoints->n_points);
00086 }
00087 
00088 /*
00089  **  Calculate area size for polygon. 
00090  **
00091  **  Total area is positive for clockwise and negative for counter clockwise ?
00092  */
00093 int dig_find_area_poly(struct line_pnts *Points, double *totalarea)
00094 {
00095     int i;
00096     double *x, *y;
00097     double tot_area, sum_area;
00098 
00099 
00100     *totalarea = 0.;
00101 
00102     tot_area = 0.0;
00103 
00104     x = Points->x;
00105     y = Points->y;
00106 
00107     sum_area = 0.0;
00108     for (i = 1; i < Points->n_points; i++) {
00109         sum_area += (x[i] - x[i - 1]) * (y[i] + y[i - 1]);
00110     }
00111     tot_area += sum_area;
00112 
00113     *totalarea = 0.5 * tot_area;
00114 
00115     return (0);
00116 }
Generated on Tue Apr 6 13:28:10 2010 for GRASS Programmer's Manual by  doxygen 1.6.3