PostGIS  2.5.0beta2dev-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 1250 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().

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