diglib/poly.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00028
00029
00030
00031
00032
00033
00034 int dig_get_poly_points(int n_lines, struct line_pnts **LPoints, int *direction,
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
00048 n_points = 0;
00049 for (i = 0; i < n_lines; i++) {
00050 Points = LPoints[i];
00051 n_points += Points->n_points - 1;
00052 }
00053 n_points++;
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
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
00090
00091
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 }