A few weeks back I received a script that, I think, calculates the distance of a point from a reference line (at right angle) and returns the distance along the reference li
My opinion on the first function depends on whether the points described by arrays x,y lie on a line (as you say) or not (as the second function suggests; in the second script they are obtained from spline interpolation.
Then the computation of sina, cosa within the loop is redundant since the angle is always the same. For each point in the arrays xp,yp the function calculates its orthogonal projection onto the line. It returns the position along the line, measured from the first point x(1),y(1), and signed normal distance. All of this happens in a rather inefficient way for this task.
Then the computation of sina, cosa is incorrect. These are meant to be components of a unit vector in the direction from (x(i),y(i)) to (x(i+2),y(i+2)). But the denominator is s(i+2)-s(i), which [looking at the second function] will generally be greater than the straight-line distance from (x(i),y(i)) to (x(i+2),y(i+2)).
The following would make more sense:
cosa = (x(i+2)-x(i))/sqrt((x(i+2)-x(i))^2 + (y(i+2)-y(i))^2);
sina = (y(i+2)-y(i))/sqrt((x(i+2)-x(i))^2 + (y(i+2)-y(i))^2);
The confusion of straight-line distances and along-the-curve distances persists through the rest of the function: the comparisons of sproj with s(i+2) and especially with s(ns) are not geometrically motivated.
Anyway, the function attempts to locate a point near the orthogonal projection of xp,yp onto the curve (which is not unique to begin with); depending on how wiggly the curve is, it may have moderate success or fail badly.