I was looking for a KML Library in C/C++ and came across your post. Looks like it is 6yrs+ old, but if someone else gets here looking for the 2nd part of your question...
I can't recall where I dug this up on the web, so I can't give/take credit.
// *************************************************************************
//
// Function: distToLine
//
// Usage: Calculate the shortest distance to a line
//
// Params: x1 - Line start x
// y1 - Line start y
// x2 - Line end x
// y2 - Line end y
// ptX - Observation Point x
// ptY - Observation Point y
//
// Returns: Distance
//
// Notes:
//
// *************************************************************************
double distToLine(double x1, double y1, double x2, double y2, double ptX, double ptY)
{
double dx;
float dy;
float t;
dx = x2 - x1;
dy = y2 - y1;
if ((dx == 0) && (dy == 0))
{
dx = ptX - x1;
dy = ptY - y1;
return sqrt(dx * dx + dy * dy);
}
t = ((ptX - x1) * dx + (ptY - y1) * dy) / (dx * dx + dy * dy);
if (t < 0) // point is nearest to the first point i.e x1,y1
{
dx = ptX - x1;
dy = ptY - y1;
}
else if (t > 1) // point is nearest to the end point i.e x2,y2
{
dx = ptX - x2;
dy = ptY - y2;
}
else // if perpendicular line intersect the line segment.
{
dx = ptX - (x1 + t * dx);
dy = ptY - (y1 + t * dy);
}
return sqrt(dx * dx + dy * dy); // returning shortest distance
}
// distToLine()