PostGIS  3.2.2dev-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 1543 of file ptarray.c.

1544 {
1545  double r;
1546 
1547  if ( FP_EQUALS(A->x, B->x) && FP_EQUALS(A->y, B->y) )
1548  {
1549  *ret = *A;
1550  return;
1551  }
1552 
1553  /*
1554  * We use comp.graphics.algorithms Frequently Asked Questions method
1555  *
1556  * (1) AC dot AB
1557  * r = ----------
1558  * ||AB||^2
1559  * r has the following meaning:
1560  * r=0 P = A
1561  * r=1 P = B
1562  * r<0 P is on the backward extension of AB
1563  * r>1 P is on the forward extension of AB
1564  * 0<r<1 P is interior to AB
1565  *
1566  */
1567  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) );
1568 
1569  if (r<=0)
1570  {
1571  *ret = *A;
1572  return;
1573  }
1574  if (r>=1)
1575  {
1576  *ret = *B;
1577  return;
1578  }
1579 
1580  ret->x = A->x + ( (B->x - A->x) * r );
1581  ret->y = A->y + ( (B->y - A->y) * r );
1582  ret->z = A->z + ( (B->z - A->z) * r );
1583  ret->m = A->m + ( (B->m - A->m) * r );
1584 }
char * r
Definition: cu_in_wkt.c:24
#define FP_EQUALS(A, B)
double m
Definition: liblwgeom.h:428
double x
Definition: liblwgeom.h:428
double z
Definition: liblwgeom.h:428
double y
Definition: liblwgeom.h:428

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: