PostGIS  2.4.9dev-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 1258 of file ptarray.c.

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

Referenced by lwline_split_by_point_to(), and ptarray_locate_point().

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