diglib/poly.c

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

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5