Skip to content

Commit ca061ca

Browse files
committed
improve intersection calculation
1 parent 45d930c commit ca061ca

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎lib/vector/Vlib/poly.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ int Vect__intersect_y_line_with_poly(const struct line_pnts *Points, double y,
162162
if (a == b)
163163
continue;
164164

165-
p = (d - c) / (b - a);
166-
x = c + p * (y - a);
165+
p = (y - a) / (b - a); /* always within [0, 1] */
166+
x = c + p * (d - c);
167167

168168
if (0 > Vect_append_point(Inter, x, y, 0))
169169
return -1;
@@ -217,8 +217,8 @@ int Vect__intersect_x_line_with_poly(const struct line_pnts *Points, double x,
217217
if (a == b)
218218
continue;
219219

220-
p = (d - c) / (b - a);
221-
y = c + p * (x - a);
220+
p = (x - a) / (b - a); /* always within [0, 1] */
221+
y = c + p * (d - c);
222222

223223
if (0 > Vect_append_point(Inter, x, y, 0))
224224
return -1;
@@ -584,6 +584,9 @@ int Vect_get_point_in_poly_isl(const struct line_pnts *Points,
584584
}
585585

586586
/* get centroid, center of gravity of the polygon line, not the area */
587+
/* original version, does not consider isles
588+
* the centroid can be inside an isle or completely outside of the area
589+
* for very thin polygons, the centroid might be on the boundary */
587590
Vect_find_poly_centroid(Points, &cent_x, &cent_y);
588591
/* is it w/in poly? */
589592
point_in_sles = 0;
@@ -603,6 +606,9 @@ int Vect_get_point_in_poly_isl(const struct line_pnts *Points,
603606
}
604607

605608
/* get centroid, center of gravity of the area */
609+
/* new version, considers isles
610+
* the centroid can still be inside an isle or completely outside of the
611+
* area for very thin polygons, the centroid might be on the boundary */
606612
Vect_find_poly_centroid_cog(Points, IPoints, n_isles, &cent_x, &cent_y);
607613
/* is it w/in poly? */
608614
point_in_sles = 0;

‎lib/vector/diglib/inside.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ double dig_x_intersect(double beg_x, double end_x, double beg_y, double end_y,
4343
*
4444
* simplify a + b * Y:
4545
* a + b * Y = beg_x - b * beg_y + b * Y
46-
* a + b * Y = beg_x + b * (Y - beg_y) */
46+
* a + b * Y = beg_x + b * (Y - beg_y)
47+
* a + b * Y = beg_x + (end_x - beg_x) * (Y - beg_y) / (end_y - beg_y) */
4748

48-
b = (end_x - beg_x) / (end_y - beg_y);
49+
b = (Y - beg_y) / (end_y - beg_y); /* always within [0, 1] */
4950

50-
return beg_x + b * (Y - beg_y);
51+
return beg_x + b * (end_x - beg_x);
5152
}

0 commit comments

Comments
 (0)