The technical term for snapping a point to a line is projecting a point to a line(-segment)
The only question that remains: Should the points projected to the line or to the line segment? (A line segment is only between two points, the line has infinity length and goes through both points)
The code below solves both: To allow also projecting points to th epart of the line that is outside of points A->B, the code would be much simpler, but this is covered in the link below, too.
See http://forums.codeguru.com/showthread.php?194400-Distance-between-point-and-line-segment
The projected point is in the variables (xx,yy): (xx,yy) is the point on the lineSegment closest to (cx,cy)