Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Intersection points for lines or polygon edges

`[xi,yi] = polyxpoly(x1,y1,x2,y2)`

[xi,yi,ii] = polyxpoly(...)

[xi,yi] = polyxpoly(...,'unique')

`[xi,yi] = polyxpoly(x1,y1,x2,y2)`

returns
the intersection points of two polylines in a planar, Cartesian system. `x1`

and `y1`

are
vectors containing the *x*- and *y*-coordinates
of the vertices in the first polyline, and `x2`

and `y2`

contain
the vertices in the second polyline. The output variables, `xi`

and `yi`

,
are column vectors containing the *x*- and *y*-coordinates
of each point at which a segment of the first polyline intersects
a segment of the second. In the case of overlapping, collinear segments,
the intersection is actually a line segment rather than a point, and
both endpoints are included in `xi`

, `yi`

.

`[xi,yi,ii] = polyxpoly(...)`

returns a two-column
array of line segment indices corresponding to the intersection points.
The *k*-th row of `ii`

indicates
which polyline segments give rise to the intersection point `xi(k)`

, `yi(k)`

.
To remember how these indices work, just think of segments and vertices
as fence sections and posts. The *i*-th fence section
connects the *i*-th post to the (*i*+1)-th
post. In general, letting *i* and *j* denote
the scalar values comprised by the *k*-th row of `ii`

,
the intersection indicated by that row occurs where the *i*-th
segment of the first polyline intersects the *j*-th
segment of the second polyline. But when an intersection falls precisely
on a vertex of the first polyline, then *i* is the
index of that vertex. Likewise with the second polyline and the index *j*.
In the case of an intersection at the *i*-th vertex
of the first line, for example, `xi(k)`

equals `x1(i)`

and `yi(k)`

equals `y1(i)`

.
In the case of intersections between vertices, *i* and *j* can
be interpreted as follows: the segment connecting `x1(i)`

, `y1(i)`

to `x1(i+1)`

, `y1(i+1)`

intersects
the segment connecting `x2(j)`

, `y2(j)`

to `x2(j+1)`

, `y2(j+1)`

at
the point `xi(k)`

, `yi(k)`

.

`[xi,yi] = polyxpoly(...,'unique')`

filters
out duplicate intersections, which may result if the input polylines
are self-intersecting.

Use the `polyxpoly`

function to find the intersection
points between a rectangle and a two-part polyline.

% Define and fill a rectangular area in the plane xlimit = [3 13]; ylimit = [2 8]; xbox = xlimit([1 1 2 2 1]); ybox = ylimit([1 2 2 1 1]); mapshow(xbox,ybox,'DisplayType','polygon','LineStyle','none') % Define and display a two-part polyline x = [0 6 4 8 8 10 14 10 14 NaN 4 4 6 9 15]; y = [4 6 10 11 7 6 10 10 6 NaN 0 3 4 3 6]; mapshow(x,y,'Marker','+') % Intersect the polyline with the rectangle [xi, yi] = polyxpoly(x, y, xbox, ybox); mapshow(xi,yi,'DisplayType','point','Marker','o')

% Display the intersection points; note that the point (12, 8) % appears twice because of a self-intersection near the end of % the first part of the polyline. [xi yi]

ans = 3 5 5 8 8 8 12 8 12 8 13 7 4 2 13 5

% You could suppress this duplicate point by using the 'unique' % option. [xi, yi] = polyxpoly(x, y, xbox, ybox, 'unique'); [xi yi]

ans = 3 5 5 8 8 8 12 8 13 7 4 2 13 5

Use the `polyxpoly`

function to find the intersection
points between the state of California and a small circle.

california = shaperead('usastatehi',... 'UseGeoCoords', true,... 'Selector',{@(name) strcmpi(name,'California'), 'Name'}); usamap('california') geoshow(california, 'FaceColor', 'none') lat0 = 37; lon0 = -122; rad = 500; [latc, lonc] = scircle1(lat0, lon0, km2deg(rad)); plotm(lat0, lon0, 'r*') plotm(latc, lonc, 'r') [loni, lati] = polyxpoly(lonc, latc, ... california.Lon', california.Lat'); plotm(lati, loni, 'bo')

Was this topic helpful?