PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ distance2d_pt_seg()

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

The old function nessecary for ptarray_segmentize2d in ptarray.c.

Definition at line 2342 of file measures.c.

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

Referenced by lwline_split_by_point_to(), and ptarray_locate_point().

2343 {
2344  double r,s;
2345 
2346  /*if start==end, then use pt distance */
2347  if ( ( A->x == B->x) && (A->y == B->y) )
2348  return distance2d_pt_pt(p,A);
2349 
2350  /*
2351  * otherwise, we use comp.graphics.algorithms
2352  * Frequently Asked Questions method
2353  *
2354  * (1) AC dot AB
2355  * r = ---------
2356  * ||AB||^2
2357  * r has the following meaning:
2358  * r=0 P = A
2359  * r=1 P = B
2360  * r<0 P is on the backward extension of AB
2361  * r>1 P is on the forward extension of AB
2362  * 0<r<1 P is interior to AB
2363  */
2364 
2365  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) );
2366 
2367  if (r<0) return distance2d_pt_pt(p,A);
2368  if (r>1) return distance2d_pt_pt(p,B);
2369 
2370 
2371  /*
2372  * (2)
2373  * (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
2374  * s = -----------------------------
2375  * L^2
2376  *
2377  * Then the distance from C to P = |s|*L.
2378  *
2379  */
2380 
2381  s = ( (A->y-p->y)*(B->x-A->x)- (A->x-p->x)*(B->y-A->y) ) /
2382  ( (B->x-A->x)*(B->x-A->x) +(B->y-A->y)*(B->y-A->y) );
2383 
2384  return FP_ABS(s) * sqrt(
2385  (B->x-A->x)*(B->x-A->x) + (B->y-A->y)*(B->y-A->y)
2386  );
2387 }
char * r
Definition: cu_in_wkt.c:24
double x
Definition: liblwgeom.h:328
double y
Definition: liblwgeom.h:328
char * s
Definition: cu_in_wkt.c:23
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
The old function nessecary for ptarray_segmentize2d in ptarray.c.
Definition: measures.c:2317
#define FP_ABS(a)
Here is the call graph for this function:
Here is the caller graph for this function: