Commit a7a55e5f authored by Caoimhe Rooney's avatar Caoimhe Rooney
Browse files

Code to check if line segments intersect

parent 89eeef7d
% check if line segment AB intersects line segment CD
%
function isInSegment = checkCrossedLines(A,B,C,D)
% A = [1,1]; C = [2,2]; B = [2,1]; D = [1,2];
x = [A(1) C(1); B(1) D(1)]; % Starting points in first row, ending points in second row
y = [A(2) C(2); B(2) D(2)];
dx = diff(x); % Take the differences down each column
dy = diff(y);
den = dx(1)*dy(2)-dy(1)*dx(2); % Compute the denominator
ua = (dx(2)*(y(1)-y(3))-dy(2)*(x(1)-x(3)))/den;
ub = (dx(1)*(y(1)-y(3))-dy(1)*(x(1)-x(3)))/den;
% Intersection points
xi = x(1)+ua*dx(1);
yi = y(1)+ua*dy(1);
isInSegment = all(([ua ub] >= 0) & ([ua ub] <= 1));
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment