PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ closest_point_on_segment()

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

Definition at line 1255 of file ptarray.c.

1256 {
1257  double r;
1258 
1259  if ( FP_EQUALS(A->x, B->x) && FP_EQUALS(A->y, B->y) )
1260  {
1261  *ret = *A;
1262  return;
1263  }
1264 
1265  /*
1266  * We use comp.graphics.algorithms Frequently Asked Questions method
1267  *
1268  * (1) AC dot AB
1269  * r = ----------
1270  * ||AB||^2
1271  * r has the following meaning:
1272  * r=0 P = A
1273  * r=1 P = B
1274  * r<0 P is on the backward extension of AB
1275  * r>1 P is on the forward extension of AB
1276  * 0<r<1 P is interior to AB
1277  *
1278  */
1279  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) );
1280 
1281  if (r<0)
1282  {
1283  *ret = *A;
1284  return;
1285  }
1286  if (r>1)
1287  {
1288  *ret = *B;
1289  return;
1290  }
1291 
1292  ret->x = A->x + ( (B->x - A->x) * r );
1293  ret->y = A->y + ( (B->y - A->y) * r );
1294  ret->z = A->z + ( (B->z - A->z) * r );
1295  ret->m = A->m + ( (B->m - A->m) * r );
1296 }
char * r
Definition: cu_in_wkt.c:24
#define FP_EQUALS(A, B)
double m
Definition: liblwgeom.h:400
double x
Definition: liblwgeom.h:400
double z
Definition: liblwgeom.h:400
double y
Definition: liblwgeom.h:400

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

Referenced by lwline_split_by_point_to(), and ptarray_locate_point().

Here is the caller graph for this function: