24 (((A) > 0 && (B) < 0) || ((A) < 0 && (B) > 0) ? (((A) + (B)) / 2.) \
25 : ((A) + ((B) - (A)) / 2.))
33static int comp_double(
const void *,
const void *);
34static int V__within(
double,
double,
double);
39static void destroy_links(
struct link_head *,
struct Slink *);
40static int Vect__divide_and_conquer(
struct Slink *,
const struct line_pnts *,
63 static int first_time = 1;
67 G_debug(3,
"Vect_get_point_in_area()");
86 for (i = 0; i < n_isles; i++) {
99static int comp_double(
const void *i,
const void *
j)
101 if (*(
const double *)i < *(
const double *)
j)
104 return (*(
const double *)i > *(
const double *)
j);
107static int V__within(
double a,
double x,
double b)
110 return (
x >= a &&
x <
b);
112 return (
x >
b &&
x <= a);
134 double a,
b, c, d,
x;
137 for (i = 1; i < Points->
n_points; i++) {
138 a = Points->
y[i - 1];
141 c = Points->
x[i - 1];
146 if (
b < a || (
b == a && d < c)) {
156 if (V__within(a,
y,
b)) {
160 p = (
y - a) / (
b - a);
189 double a,
b, c, d,
y;
192 for (i = 1; i < Points->
n_points; i++) {
193 a = Points->
x[i - 1];
196 c = Points->
y[i - 1];
201 if (
b < a || (
b == a && d < c)) {
211 if (V__within(a,
x,
b)) {
215 p = (
x - a) / (
b - a);
242 static int first_time = 1;
257 G_debug(3,
"Vect_get_point_in_poly(): divide and conquer");
261 for (i = 0; i < Points->
n_points; i++) {
291 G_warning(
"Vect_get_point_in_poly(): %s",
292 _(
"Unable to find point in polygon"));
296 G_debug(3,
"Found point in %d iterations", 10 -
ret);
323static int Vect__divide_and_conquer(
struct Slink *
Head,
328 struct Slink *A, *B, *C;
339 C->x = (A->x + B->x) / 2.;
363 struct Slink *p, *tmp;
402 for (i = 1; i < points->
n_points; i++) {
451 x = (box.
W + box.
E) / 2.;
452 y = (box.
S + box.
N) / 2.;
461 for (i = 1; i < Points->
n_points; i++) {
462 w = (
x -
xp[i - 1]) * (
yp[i] - y) - (
x -
xp[i]) * (
yp[i - 1] - y);
473 w = (
x -
xp[i - 1]) * (
yp[i] - y) - (
x -
xp[i]) * (
yp[i - 1] - y);
550 static int first_time = 1;
562 G_debug(3,
"Vect_get_point_in_poly_isl(): n_isles = %d", n_isles);
574 "Vect_get_point_in_poly_isl(): outer ring with less than 4 points");
592 for (i = 0; i < n_isles; i++) {
614 for (i = 0; i < n_isles; i++) {
627 G_debug(3,
"Vect_get_point_in_poly_isl(): the hard way");
641 for (i = 0; i < Points->
n_points; i++) {
656 for (i = 0; i < Points->
n_points; i++) {
660 if ((Points->
y[i] >=
cent_y) &&
667 if ((Points->
x[i] >=
cent_x) &&
671 for (i = 0; i < n_isles; i++) {
691 G_debug(3,
"Vect_get_point_in_poly_isl(): lo_y == hi_y");
703 for (i = 0; i < n_isles; i++) {
710 G_debug(3,
"Vect_get_point_in_poly_isl(): no x intersections");
723 for (i = 0; i <
Intersects->n_points; i += 2) {
751 G_debug(3,
"Vect_get_point_in_poly_isl(): trying x intersect");
754 G_debug(3,
"Vect_get_point_in_poly_isl(): lo_x == hi_x");
766 for (i = 0; i < n_isles; i++) {
773 G_debug(3,
"Vect_get_point_in_poly_isl(): no y intersections");
785 for (i = 0; i <
Intersects->n_points; i += 2) {
807 G_warning(
"Vect_get_point_in_poly_isl(): collapsed area");
822 G_warning(
"Vect_get_point_in_poly_isl(), the hard way: centroid is on "
823 "outer ring, max dist is %g",
830 for (i = 0; i < n_isles; i++) {
833 G_warning(
"Vect_get_point_in_poly_isl(), the hard way: "
834 "centroid is in isle, max dist is %g",
851static int segments_x_ray(
double X,
double Y,
const struct line_pnts *Points)
853 double x1, x2, y1, y2;
858 G_debug(3,
"segments_x_ray(): x = %f y = %f n_points = %d",
X,
Y,
866 for (n = 1; n < Points->
n_points; n++) {
867 x1 = Points->
x[n - 1];
868 y1 = Points->
y[n - 1];
885 if (y1 >
Y && y2 >
Y)
889 if (y1 <
Y && y2 <
Y)
893 if (x1 <
X && x2 <
X)
897 if ((x1 ==
X && y1 ==
Y) || (x2 ==
X && y2 ==
Y))
901 if (x1 == x2 && x1 ==
X) {
907 if (y1 == y2 && y1 ==
Y) {
919 if ((y1 ==
Y && y2 >
Y) || (y2 ==
Y && y1 >
Y))
925 if (y1 ==
Y && y2 <
Y) {
930 if (y2 ==
Y && y1 <
Y) {
938 if (x1 >=
X && x2 >=
X) {
959 G_warning(
"segments_x_ray() %s: X = %f Y = %f x1 = %f y1 = %f x2 = %f "
961 _(
"conditions failed"),
X,
Y, x1, y1, x2, y2);
981 G_debug(3,
"Vect_point_in_poly(): x = %f y = %f n_points = %d",
X,
Y,
1010 static int first = 1;
1015 G_debug(3,
"Vect_point_in_area_outer_ring(): x = %f y = %f area = %d",
X,
Y,
1048 static int first = 1;
1053 G_debug(3,
"Vect_point_in_island(): x = %f y = %f isle = %d",
X,
Y,
isle);
int Vect_find_poly_centroid_cog(const struct line_pnts *Points, const struct line_pnts **IPoints, int n_isles, double *cent_x, double *cent_y)
Get centroid of polygon.
int Vect_get_point_in_poly_isl(const struct line_pnts *Points, const struct line_pnts **IPoints, int n_isles, double *att_x, double *att_y)
Get point inside polygon but outside the islands specifiled in IPoints.
int Vect_point_in_poly(double X, double Y, const struct line_pnts *Points)
Determines if a point (X,Y) is inside a polygon.
int Vect_get_point_in_poly(const struct line_pnts *Points, double *X, double *Y)
Get point inside polygon.
int Vect_find_poly_centroid(const struct line_pnts *points, double *cent_x, double *cent_y)
Get centroid of polygon.
int Vect__intersect_x_line_with_poly(const struct line_pnts *, double, struct line_pnts *)
int Vect__intersect_y_line_with_poly(const struct line_pnts *, double, struct line_pnts *)
int Vect_point_in_island(double X, double Y, struct Map_info *Map, int isle, struct bound_box *box)
Determines if a point (X,Y) is inside an island.
int Vect_point_in_area_outer_ring(double X, double Y, struct Map_info *Map, int area, struct bound_box *box)
Determines if a point (X,Y) is inside an area outer ring. Islands are not considered.
int Vect_get_point_in_area(struct Map_info *Map, int area, double *X, double *Y)
Get point inside area and outside all islands.
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
VOID_T * link_new(struct link_head *)
void link_dispose(struct link_head *, VOID_T *)
void link_exit_on_error(int)
struct link_head * link_init(int)
void Vect_line_box(const struct line_pnts *, struct bound_box *)
Get bounding box of line.
int Vect_get_isle_points(struct Map_info *, int, struct line_pnts *)
Returns polygon array of points for given isle.
int Vect_get_area_points(struct Map_info *, int, struct line_pnts *)
Returns polygon array of points (outer ring) of given area.
int Vect_get_area_isle(struct Map_info *, int, int)
Returns isle id for area.
int Vect_get_area_num_isles(struct Map_info *, int)
Returns number of isles for given area.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
double dig_x_intersect(double, double, double, double, double)
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
double * x
Array of X coordinates.
int n_points
Number of points.