PostGIS  3.6.1dev-r@@SVN_REVISION@@

◆ closest_point_on_segment()

void closest_point_on_segment ( const POINT4D R,
const POINT4D A,
const POINT4D B,
POINT4D ret 
)

Definition at line 1403 of file ptarray.c.

1404 {
1405  double r;
1406 
1407  if ( FP_EQUALS(A->x, B->x) && FP_EQUALS(A->y, B->y) )
1408  {
1409  *ret = *A;
1410  return;
1411  }
1412 
1413  /*
1414  * We use comp.graphics.algorithms Frequently Asked Questions method
1415  *
1416  * (1) AC dot AB
1417  * r = ----------
1418  * ||AB||^2
1419  * r has the following meaning:
1420  * r=0 P = A
1421  * r=1 P = B
1422  * r<0 P is on the backward extension of AB
1423  * r>1 P is on the forward extension of AB
1424  * 0<r<1 P is interior to AB
1425  *
1426  */
1427  r = ( (p->x-A->x) * (B->x-A->x) + (p->y-A->y) * (B->y-A->y) )/( (B->x-A->x)*(B->x-A->x) +(B->y-A->y)*(B->y-A->y) );
1428 
1429  if (r<=0)
1430  {
1431  *ret = *A;
1432  return;
1433  }
1434  if (r>=1)
1435  {
1436  *ret = *B;
1437  return;
1438  }
1439 
1440  ret->x = A->x + ( (B->x - A->x) * r );
1441  ret->y = A->y + ( (B->y - A->y) * r );
1442  ret->z = A->z + ( (B->z - A->z) * r );
1443  ret->m = A->m + ( (B->m - A->m) * r );
1444 }
char * r
Definition: cu_in_wkt.c:24
#define FP_EQUALS(A, B)
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414

References FP_EQUALS, POINT4D::m, r, POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by lwline_split_by_point_to(), ptarray_locate_point(), and test_ptarray_closest_point_on_segment().

Here is the caller graph for this function: