PostGIS  2.5.7dev-r@@SVN_REVISION@@

## ◆ distance2d_pt_seg()

 double distance2d_pt_seg ( const POINT2D * p, const POINT2D * A, const POINT2D * B )

Definition at line 2332 of file measures.c.

2333 {
2334  double r,s;
2335
2336  /*if start==end, then use pt distance */
2337  if ( ( A->x == B->x) && (A->y == B->y) )
2338  return distance2d_pt_pt(p,A);
2339
2340  /*
2341  * otherwise, we use comp.graphics.algorithms
2342  * Frequently Asked Questions method
2343  *
2344  * (1) AC dot AB
2345  * r = ---------
2346  * ||AB||^2
2347  * r has the following meaning:
2348  * r=0 P = A
2349  * r=1 P = B
2350  * r<0 P is on the backward extension of AB
2351  * r>1 P is on the forward extension of AB
2352  * 0<r<1 P is interior to AB
2353  */
2354
2355  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) );
2356
2357  if (r<0) return distance2d_pt_pt(p,A);
2358  if (r>1) return distance2d_pt_pt(p,B);
2359
2360
2361  /*
2362  * (2)
2363  * (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
2364  * s = -----------------------------
2365  * L^2
2366  *
2367  * Then the distance from C to P = |s|*L.
2368  *
2369  */
2370
2371  s = ( (A->y-p->y)*(B->x-A->x)- (A->x-p->x)*(B->y-A->y) ) /
2372  ( (B->x-A->x)*(B->x-A->x) +(B->y-A->y)*(B->y-A->y) );
2373
2374  return FP_ABS(s) * sqrt(
2375  (B->x-A->x)*(B->x-A->x) + (B->y-A->y)*(B->y-A->y)
2376  );
2377 }
char * s
Definition: cu_in_wkt.c:23
char * r
Definition: cu_in_wkt.c:24
#define FP_ABS(a)
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2314
double y
Definition: liblwgeom.h:331
double x
Definition: liblwgeom.h:331

References distance2d_pt_pt(), FP_ABS, r, s, POINT2D::x, and POINT2D::y.

Referenced by lwline_split_by_point_to(), and ptarray_locate_point().

Here is the call graph for this function:
Here is the caller graph for this function: