This is the 'neatest' I can come up with:Ref wrote:Saw the typo and after correction seems to work for me.
Easy to find closest-point-on-line but pretty mess to get closest-point-on-line-segment.
Do you have a 'neat' way of doing this?
Code: Select all
function ClosestPointOnLineSegment(px,py,x1,y1,x2,y2) local dx,dy = x2-x1,y2-y1 local length = math.sqrt(dx*dx+dy*dy) dx,dy = dx/length,dy/length local posOnLine = math.min(length, math.max(0,dx*(px-x1) + dy*(py-y1))) return x1+posOnLine*dx,x2+posOnLine*dy end