# Calculates the closest point to a segment source code

This snippet submitted by Adri�n Deccico on 2005-03-06. It has been viewed 30146 times.
Rating of 5.3 with 206 votes

``` //--------------------------------------------------------------------------------------------------------
/*
Description: calculate the more near point belonging to a given segment.
Parameters:
nSx1, nSy1 =  First point of the segment
nSx2, int nSy2 = Second point of the segment
nPx, int nPy = The point to analize
*nCercaX,  *nCercaY = The closest point to the given point (nPx,nPy) that belongs to the segment
Returns: The distance between the point and the founded point of the segment
*/
double ElPuntoMasCercanoAUnSegmento(double nSx1, double nSy1, double nSx2,
double nSy2, double nPx, double nPy,
double *nCercaX,  double *nCercaY)
{

//caracterizo la recta del segmento dado Y=m*X + b
double m, b;

bool bEsHorizontalOVertical = false;

//el segmento es vertical
if (nSx2 == nSx1){
*nCercaX = nSx1;
*nCercaY = nPy;
bEsHorizontalOVertical = true;
}

//el segmento es horizontal
if (nSy2 == nSy1){
*nCercaY = nSy1;
*nCercaX = nPx;
bEsHorizontalOVertical = true;
}

//el segmento est� caracterizado por una recta \"AX + BY + C = 0\" con
// A y B distintos de cero
if (!bEsHorizontalOVertical)
{
m= (nSy2 - nSy1) / (nSx2 - nSx1);
b = nSy1 - (nSx1 * m);

//caracterizo la recta que une al punto dado con el punto m�s
// cercano dentro del segmento Y=m1*X + b1
double m1, b1;

m1 = -1 / m;
b1 = -m1 * nPx + nPy;

//encuentro el punto de intersecci�n
*nCercaX =  (b1 - b) / (m - m1);
*nCercaY =  m * *nCercaX + b;
}

//verificar que el punto encontrado est� dentro del segmento
if (*nCercaX < min(nSx1,nSx2)
||  *nCercaX > max(nSx1,nSx2)
||  *nCercaY < min(nSy1,nSy2)
||  *nCercaY > max(nSy1,nSy2) )
{
//el punto m�s cercano ser� uno de los dos extremos (el m�s cercano al punto)
if (dDistanciaEntrePuntos(nSx1, nSy1, nPx, nPy)
<= dDistanciaEntrePuntos(nSx2, nSy2, nPx, nPy))
{
*nCercaX = nSx1;
*nCercaY = nSy1;
}
else
{
*nCercaX = nSx2;
*nCercaY = nSy2;
}
}

return dDistanciaEntrePuntos(nPx, nPy, *nCercaX , *nCercaY);

}
//--------------------------------------------------------------------------------------------

//------------------------------------------------------------------------
//return the distance between thwo points in a x,y plane.
double dDistanciaEntrePuntos(double lOldX, double lOldY,
double lNewX, double lNewY)
{
double x = lNewX - lOldX;
double y = lNewY - lOldY;

return sqrt(pow(x,2) + pow(y,2));
}
//------------------------------------------------------------------------
```

More C and C++ source code snippets