function [pfnl test] = project_a2line(a, line_pts, ifplot)

if nargin<3
    ifplot = false;
end

%form vectors
origin = line_pts(2,:);
avec = a - origin;
bvecs = [line_pts(1, :) - origin; line_pts(3, :) - origin];
bmags = (bvecs(:, 1).^2 + bvecs(:, 2).^2);
cmags = (bvecs * avec') ./ bmags;
cvecs = (bvecs' * diag(cmags))';
cpts = [cvecs(:, 1) + origin(1), cvecs(:, 2) + origin(2)];

%test to see if cpts are on the lines
test = (cpts(:, 1) < max(line_pts([1,3], 1), [origin(1); origin(1)])) ...
    & (cpts(:, 1) > min(line_pts([1,3], 1), [origin(1); origin(1)]))...
    & (cpts(:, 2) > min(line_pts([1,3], 2), [origin(2); origin(2)]))...
    & (cpts(:, 2) > min(line_pts([1,3], 2), [origin(2); origin(2)]));
if ~any(test)
    pfnl = origin;
elseif all(test)
    dist = sqrt((cpts(:, 1) - a(1)).^2 + (cpts(:, 2) - a(2)).^2);
    if abs(dist(1) - dist(2)) < sqrt(eps)
        pfnl = origin;
    else
        pfnl = cpts(dist == min(dist), :);
    end
else
    pfnl = cpts(test, :);
end
   
if ifplot
    plot(a(1), a(2), 'x'), hold on, axis equal, plot(line_pts(:,1), line_pts(:,2))
    plot(cpts(:, 1), cpts(:,2), '.g')
    for i = 1:2
    plot([cpts(i,1), a(1)], [cpts(i, 2), a(2)], 'g')
    end
    plot(pfnl(1), pfnl(2), 'ro')
end