var x0 = 0.2; var y0 = 0.4; var x1 = 0.7; var y1 = 0.6; var px = 0.5; var py = 0.5; var dx = x1 - x0; var dy = y1 - y0; var length2 = (dx * dx) + (dy * dy); var t = ((px - x0) * dx + (py - y0) * dy) / length2; var ix = x0 + t * dx; var iy = y0 + t * dy; var d = sqrt((px - ix) * (px - ix) + (py - iy) * (py - iy));