You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.5 KiB
60 lines
1.5 KiB
function [checkPt, onEnd] = checkPointOnSegment(segment, C, delta1, delta2)
|
|
|
|
% Check if a point C lies on a line segment
|
|
% Vipul Lugade
|
|
% Matlabgeeks.com
|
|
% 9/9/2018
|
|
|
|
% e.g. segment = [P1_X, P1_Y; P2_X, P2_Y]; C = [CX, CY];
|
|
|
|
%% initialize outputs
|
|
checkPt = false;
|
|
onEnd = false;
|
|
|
|
%% Error check
|
|
if any(size(segment) ~=2) || numel(C) ~= 2
|
|
error('Point/segment arguments not properly defined.');
|
|
end
|
|
|
|
%% Perform cross product to see if the vectors formed by both endpoints
|
|
% and the point in question are collinear
|
|
A = segment(1,:);
|
|
B = segment(2,:);
|
|
|
|
% form vectors for the line segment (AB) and the point to one endpoint of
|
|
% segment
|
|
AB = B - A;
|
|
AC = C - A;
|
|
|
|
% if not collinear then return false as point cannot be on segment
|
|
if abs(cross(AB, AC)) < delta1
|
|
% calculate the dotproduct of (AB, AC) and (AB, AB) to see point is now
|
|
% on the segment
|
|
dotAB = dot(AB, AB);
|
|
dotAC = dot(AB, AC);
|
|
% on end points of segment
|
|
if abs(dotAC) < delta2 || abs(dotAC - dotAB) < delta2
|
|
onEnd = true;
|
|
checkPt = true;
|
|
% on segment
|
|
elseif dotAC > delta2 && dotAC < dotAB
|
|
checkPt = true;
|
|
end
|
|
end
|
|
|
|
%% plot the results
|
|
% if plotResults
|
|
% figure; hold on;
|
|
% plot(segment(:,1), segment(:,2), 'ro-');
|
|
% plot(C(1), C(2), 'bs');
|
|
% legend('segment', 'point');
|
|
% if checkPt
|
|
% text(C(1)+0.1, C(2)+0.1, 'Point on Segment');
|
|
% else
|
|
% text(C(1)+0.1, C(2)+0.1, 'Point Not on Segment');
|
|
% end
|
|
% end
|
|
|
|
%% Cross product returning z value
|
|
function z = cross(a, b)
|
|
z = a(1)*b(2) - a(2)*b(1);
|