Skip to content
Snippets Groups Projects
Commit 9cdf724c authored by Ivar Stefansson's avatar Ivar Stefansson
Browse files

Replace intersection_between_inclusive.m

parent c23056ed
No related branches found
No related tags found
No related merge requests found
function [ intersection ] = intersection_between_inclusive( k, vk, l, vl, between) function [ intersection ] = intersection_between_inclusive( k, kIsVertical, l, lIsVertical, between)
%UNTITLED2 Calculates intersections of two lines specified by two sets of %intersection_between_inclusive Calculates intersections of two lines.
%points. If between is true, an empty value is returned if the intersection % The lines are specified by two sets of points. If between is true, an empty value is returned if the intersection
%is outside the endpoints of either one of the lines %is outside the endpoints of either one of the lines
% lines in format [x1, y1; x2, y2] % lines in format [x1, y1; x2, y2]
% [11, 12; 21, 22] % [11, 12; 21, 22]
% line k = ax + b, l = cx + d % line k = ax + b, l = cx + d
% vk = true iff k vertical % vk = true iff k vertical
if vk if kIsVertical
c = (l(2,2) - l(1,2)) / (l(2,1)-l(1,1)); c = (l(2,2) - l(1,2)) / (l(2,1)-l(1,1));
d = l(1,2) - c*l(1,1); d = l(1,2) - c*l(1,1);
y = c * k(1) + d; y = c * k(1) + d;
if isnan(y) if isnan(y)
if vl if lIsVertical
intersection = []; intersection = [];
else else
error('non-parallel lines did not intersect'); error('non-parallel lines did not intersect');
end end
elseif between && (y > max(k(3:4)) || y < min(k(3:4)) || ... elseif between && (y > max(k(3:4)) || y < min(k(3:4)) || ...
y > max(l(3:4)) || y < min(l(3:4))) y > max(l(3:4)) || y < min(l(3:4)))
intersection = []; intersection = [];
else else
intersection = [k(1), y]; intersection = [k(1), y];
end end
elseif vl elseif lIsVertical
a = (k(2,2) - k(1,2)) / (k(2,1)-k(1,1)); a = (k(2,2) - k(1,2)) / (k(2,1)-k(1,1));
b = k(1,2) - a*k(1,1); b = k(1,2) - a*k(1,1);
x = l(1); x = l(1);
y = a * x + b; y = a * x + b;
if isnan(y)
error('non-parallel lines did not intersect'); if between && (y > max(l(3:4)) || y < min(l(3:4)) || ...
elseif between && (y > max(l(3:4)) || y < (min(l(3:4))) || ... y > max(k(3:4)) || y < min(k(3:4)))
y > max(k(3:4)) || y < (min(k(3:4)))) intersection = [];
intersection = []; else
else intersection = [x, y];
intersection = [x, y]; end
end else
else a = (k(2,2) - k(1,2)) / (k(2,1)-k(1,1));
a = (k(2,2) - k(1,2)) / (k(2,1)-k(1,1)); c = (l(2,2) - l(1,2)) / (l(2,1)-l(1,1));
c = (l(2,2) - l(1,2)) / (l(2,1)-l(1,1)); b = k(1,2) - a*k(1,1);
b = k(1,2) - a*k(1,1); d = l(1,2) - c*l(1,1);
d = l(1,2) - c*l(1,1);
x = (d-b)/(a-c);
x = (d-b)/(a-c);
%y = a * x + b; if between && (x > max(l(1:2)) || x < min(l(1:2)) || ...
if isnan(x) x > max(k(1:2)) || x < min(k(1:2)))
intersection = []; intersection = [];
elseif between && (x > max(l(1:2)) || x < (min(l(1:2))) || ... else
x > max(k(1:2)) || x < (min(k(1:2)))) intersection = [x, a * x + b];
intersection = []; end
else end
intersection = [x, a * x + b]; end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment