PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ distance2d_sqr_pt_seg()

double distance2d_sqr_pt_seg ( const POINT2D C,
const POINT2D A,
const POINT2D B 
)

Definition at line 2350 of file measures.c.

2351 {
2352  /*if start==end, then use pt distance */
2353  if ((A->x == B->x) && (A->y == B->y))
2354  return distance2d_sqr_pt_pt(C, A);
2355 
2356  /*
2357  * otherwise, we use comp.graphics.algorithms
2358  * Frequently Asked Questions method
2359  *
2360  * (1) AC dot AB
2361  * r = ---------
2362  * ||AB||^2
2363  * r has the following meaning:
2364  * r=0 P = A
2365  * r=1 P = B
2366  * r<0 P is on the backward extension of AB
2367  * r>1 P is on the forward extension of AB
2368  * 0<r<1 P is interior to AB
2369  */
2370 
2371  double ba_x = (B->x - A->x);
2372  double ba_y = (B->y - A->y);
2373  double ab_length_sqr = (ba_x * ba_x + ba_y * ba_y);
2374  double ca_x = (C->x - A->x);
2375  double ca_y = (C->y - A->y);
2376  double dot_ac_ab = (ca_x * ba_x + ca_y * ba_y);
2377 
2378  if (dot_ac_ab <= 0)
2379  return distance2d_sqr_pt_pt(C, A);
2380  if (dot_ac_ab >= ab_length_sqr)
2381  return distance2d_sqr_pt_pt(C, B);
2382 
2383  /*
2384  * (2)
2385  * (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
2386  * s = -----------------------------
2387  * L^2
2388  *
2389  * Then the distance from C to P = |s|*L.
2390  *
2391  */
2392 
2393  double s_numerator = ca_x * ba_y - ca_y * ba_x;
2394 
2395  /* Distance = (s_num / ab) * (s_num / ab) * ab == s_num * s_num / ab) */
2396  return s_numerator * s_numerator / ab_length_sqr;
2397 }
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:35
double y
Definition: liblwgeom.h:404
double x
Definition: liblwgeom.h:404

References distance2d_sqr_pt_pt(), 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: