Heisenberg
Heisenberg

Reputation: 147

Polyxpoly returns empty matrix

I'm writing a code in which I have to use polyxpoly and I'm having some issues with the output.

My code:

x=[-286.1018 -363.2334];
y=[4617.4 4725.1];
xv=[-316.7 -128-9 -268.3 -1864.6 -840.4];
yv=[4694.4 4944.7 5641.7 6002.0 4519.9];
[xi,yi] = polyxpoly(x,y,xv,yv);

And it returns:

Empty matrix: 0-by-2

What am I doing wrong I can't understand why it is not working (it should return the intersection points)? Can someone help me? Is this a bug of the function polyxpoly?

Upvotes: 3

Views: 1063

Answers (1)

rayryeng
rayryeng

Reputation: 104503

You're getting an empty matrix because the polylines defined for (x,y) and (xv,yv) don't intersect. This can clearly be shown by the plotting the polylines:

x=[-286.1018 -363.2334];
y=[4617.4 4725.1];
xv=[-316.7 -128-9 -268.3 -1864.6 -840.4];
yv=[4694.4 4944.7 5641.7 6002.0 4519.9];
mapshow(xv, yv);
mapshow(x,y,'color','red')

We get:

enter image description here

As you can see, the bigger shape defined by xv and yv is not closed, so the smaller line defined by x and y never intersect the shape. If you want to find the intersection points, you'll need to close the larger polygon. This can simply be done by duplicating the first xv and yv point in the array and making sure these appear at the end of the xv and yv arrays so that you close the polygon:

x=[-286.1018 -363.2334];
y=[4617.4 4725.1];
xv=[-316.7 -128-9 -268.3 -1864.6 -840.4];
yv=[4694.4 4944.7 5641.7 6002.0 4519.9];
%// Change
xv = [xv xv(1)];
yv = [yv yv(1)];
mapshow(xv, yv);
mapshow(x,y,'color','red')

We get:

enter image description here

That's better! Now try polyxpoly on the new xv and yv values:

>> [xi,yi] = polyxpoly(x, y, xv, yv)

xi =

 -336.5178


yi =

   4.6878e+03

We can show this point of intersection by adding one more mapshow call to the already spawned figure:

mapshow(xi,yi,'DisplayType','point','Marker','o')

We get:

enter image description here

You can see that the point of intersection has been found, and that it is delineated by a red circle in the map.

Upvotes: 2

Related Questions