MATLAB function for finding intersection points between line and multiple polygon

Hello everyone,
I am looking for any MATLAB function or logic to find intersection between single scan-line and multiple polygon (one inside another).
Here as you can see into attached figure, I need to find intersection points 1,2,3 and 4. scan-line Y=2 is intersecting both the polygon-1 and 2 at same time. Polygon 2is inside the polygon 1 and the orange lines indicates the way I want to fill this area between two polygon.
MATLAB library "Geom2d" has function named 'intersectLinePolygon(line, poly, varargin)'. But this function takes only single line and one polygon as a input paramter.
So please help me with any other function which can find intersection between single line and multiple polygon.
thank you in advance.

 Accepted Answer

One option is to use the inpolygon function:
rctnglx = [1 1 3 3 1]; % Create Data
rctngly = [1 2 2 1 1]; % Create Data
linx = linspace(0, 4); % Create Data
liny = ones(1,numel(linx))*1.5; % Create Data
[in, on] = inpolygon(linx, liny, rctnglx, rctngly); % Points Of Line In Or On Rectangle
leftidx = find(in,1,'first'); % Left Intersection
rightidx = find(in,1,'last'); % Right Intersection
figure
plot(rctnglx, rctngly)
hold on
plot(linx, liny)
plot(linx(leftidx), liny(leftidx), 'or', linx(rightidx), liny(rightidx), 'or')
hold off
ylim([0 3])
Experiment to get this to work with your data.

4 Comments

First of all thank you so much for youe quick response. :)
Here you only considered intersection of line with single polygon. I want intersection of line and multiple polygon (2 polygon: one inside another) at a same time. As you can see in my figure, I want like this [point-1;point-2;point-3;point-4] as a answer. here point-1,2,3,4 are intersection of line and both the polygons.
My pleasure.
I gave an illustration in my code on how to define the points. You can do the same thing with the second polygon:
rctnglx = [2 2 5 5 2]; % Create Data
rctngly = [1 4 4 1 1]; % Create Data
rctnglx2 = [3 3 4 4 3]; % Create Data
rctngly2 = [2 3 3 2 2]; % Create Data
linx = linspace(0, 7, 500); % Create Data
liny = ones(1,numel(linx))*2.5; % Create Data
[in1, on1] = inpolygon(linx, liny, rctnglx, rctngly); % Points Of Line In Or On Rectangle 1
leftidx = find(in1,1,'first'); % Left Intersection 1
rightidx = find(in1,1,'last'); % Right Intersection 1
[in2, on2] = inpolygon(linx, liny, rctnglx2, rctngly2); % Points Of Line In Or On Rectangle 2
leftidx2 = find(in2,1,'first'); % Left Intersection 2
rightidx2 = find(in2,1,'last'); % Right Intersection 2
figure
plot(rctnglx, rctngly, rctnglx2, rctngly2)
hold on
plot(linx, liny)
plot(linx(leftidx), liny(leftidx), 'or', linx(rightidx), liny(rightidx), 'or')
plot(linx(leftidx2), liny(leftidx2), 'sr', linx(rightidx2), liny(rightidx2), 'sr')
hold off
ylim([0 5])
The addiitional code is the same as the initial code, simply using the coordinates of the second polygon as well, and adding those to the plot. Repeat for as many polygons as you have.
Thank you so much. Now I can easily set my logic from this code. :) Will get back to you if I will stuck with something else. But for now I got my answer.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!