PostGIS  2.1.10dev-r@@SVN_REVISION@@
 void closest_point_on_segment ( const POINT4D * R, const POINT4D * A, const POINT4D * B, POINT4D * ret )

Definition at line 1219 of file ptarray.c.

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

Referenced by ptarray_locate_point().

1220 {
1221  double r;
1222
1223  if ( FP_EQUALS(A->x, B->x) && FP_EQUALS(A->y, B->y) )
1224  {
1225  *ret = *A;
1226  return;
1227  }
1228
1229  /*
1230  * We use comp.graphics.algorithms Frequently Asked Questions method
1231  *
1232  * (1) AC dot AB
1233  * r = ----------
1234  * ||AB||^2
1235  * r has the following meaning:
1236  * r=0 P = A
1237  * r=1 P = B
1238  * r<0 P is on the backward extension of AB
1239  * r>1 P is on the forward extension of AB
1240  * 0<r<1 P is interior to AB
1241  *
1242  */
1243  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) );
1244
1245  if (r<0)
1246  {
1247  *ret = *A;
1248  return;
1249  }
1250  if (r>1)
1251  {
1252  *ret = *B;
1253  return;
1254  }
1255
1256  ret->x = A->x + ( (B->x - A->x) * r );
1257  ret->y = A->y + ( (B->y - A->y) * r );
1258  ret->z = A->z + ( (B->z - A->z) * r );
1259  ret->m = A->m + ( (B->m - A->m) * r );
1260 }
double x
Definition: liblwgeom.h:308
double m
Definition: liblwgeom.h:308
char * r
Definition: cu_in_wkt.c:25
double z
Definition: liblwgeom.h:308
#define FP_EQUALS(A, B)
double y
Definition: liblwgeom.h:308

Here is the caller graph for this function: