polyInt: Function to find intersection points of two polygons.

View source: R/segSegInt.r

polyIntR Documentation

Function to find intersection points of two polygons.

Description

This is a Q&D tool to find the locations where two polygons, in a plane only (not 3D space), intersect.

Usage

polyInt(poly1,poly2, stopAtFirst = FALSE, plotit = FALSE, roundPrecision = 10, ...)

Arguments

poly1

An Nx2 or 2xN matrix with X-values in the first row/column and Y-values in the second.

poly2

An Nx2 or 2xN matrix with X-values in the first row/column and Y-values in the second.

stopAtFirst

Boolean: if TRUE, then just return the first intersection point found. Useful time-saver when the user only needs to know if the polygons intersect.

plotit

Boolean: if TRUE, (and stopAtFirst is FALSE), the two polygons are plotted and the intersection points marked on the graph.

roundPrecision

Number of digits that data should be rounded to. This is necessary to avoid the usual floating-point precision problems when checking for possible duplicated intersection points.

...

Arguments to be passed along to the internal line call when plotit is TRUE.

Details

The function loops over all pairs of segments (one from poly1 and one from poly2), calling segSegInt to see if they intersect. After all pair-combinations are tested, the collected intersection points, if any, are reduced to the unique collection. This avoids repetition when an intersection point is a vertex of one (or both) of the polygons. It is not necessary to "close" the supplied set of vertices, i.e. repeat the initial vertex at the end of the array as is needed to generate a complete line-plot of a polygon. The function will add that repeated vertex if it's not present in the input polygon(s).

Note: The supporting function segSegInt returns NA when two segments are parallel. However, when two polygons in fact have an overlapping (and thus parallel) couple of edges, the adjoining edges of one or both polygons will not be parallel to these parallel edges and will intersect one or both, so the vertex which lies on the other polygon's edge will be reported.

Value

A matrix of the x and y coordinates of all intersection points, or, if stopAtFirst is TRUE, the first intersection point found. If no intersections exist, NULL is returned.

Author(s)

Carl Witthoft, carl@witthoft.com

See Also

There are many tools which are far faster and more flexible. I wrote this one because it uses only base functions and doesn't require converting polygon vertices into a special class variable. Here are two common packages. intersect , st_intersection

Examples

sqone <- cbind(c(0,1,1,0),c(0,0,1,1))
sqtwo <- sqone + 0.5 
foo <- polyInt(sqone, sqtwo, plotit = TRUE)


cgwtools documentation built on Oct. 21, 2023, 1:06 a.m.