PostGIS  2.5.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 1257 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().

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