%% 1. polyxpoly 예제
clc; clear; close all;
% 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])';
% 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]';
% Intersect the polyline with the rectangle
[xi, yi] = polyxpoly(x, y, xbox, ybox);
figure(1); hold on;
plot(xbox,ybox,'b-')
plot(x,y,'g-')
plot(xi,yi,'ro');
hold off;
clc; clear; close all;
% 1. Boundary
xlimit = 20*rand(1, 2);
ylimit = 20*rand(1, 2);
xbox = xlimit([1 1 2 2 1]);
ybox = ylimit([1 2 2 1 1]);
poly_bd = [xbox' ybox'];
% 2. Line
line_bd = 20*rand(2, 2);
% 3. Intersection
intersection = get_inters_btw_line_poly(poly_bd, line_bd);
figure(1); hold on;
plot(poly_bd(:, 1), poly_bd(:, 2), 'b-', 'LineWidth', 2);
plot([line_bd(1, 1) line_bd(2, 1)], [line_bd(1, 2) line_bd(2, 2)], 'g-', 'LineWidth', 2);
plot(intersection(:, 1), intersection(:, 2), 'ro', 'MarkerSize',15, 'LineWidth', 2);
axis([0 20 0 20]);
axis equal; grid on;
title(sprintf('%d Intersections', size(intersection, 1)), 'FontSize', 15);
function [intersection, nr_intersection] = get_inters_btw_line_poly(poly_bd, line_bd)
%
% polygon과 line 사이의 intersection을 반환한다.
%
% poly_bd =
% 3 6
% 3 16
% 13 16
% 13 6
% 3 6
%
% line_bd =
%
% 18.3826 18.4282
% 2.7728 4.9451
%
[xi, yi] = polyxpoly(line_bd(:, 1), line_bd(:, 2), poly_bd(:, 1), poly_bd(:, 2));
intersection = [xi yi];
nr_intersection = length(xi);