PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ distance2d_sqr_pt_seg()

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

Definition at line 2407 of file measures.c.

2408 {
2409  /*if start==end, then use pt distance */
2410  if ((A->x == B->x) && (A->y == B->y))
2411  return distance2d_sqr_pt_pt(C, A);
2412 
2413  /*
2414  * otherwise, we use comp.graphics.algorithms
2415  * Frequently Asked Questions method
2416  *
2417  * (1) AC dot AB
2418  * r = ---------
2419  * ||AB||^2
2420  * r has the following meaning:
2421  * r=0 P = A
2422  * r=1 P = B
2423  * r<0 P is on the backward extension of AB
2424  * r>1 P is on the forward extension of AB
2425  * 0<r<1 P is interior to AB
2426  */
2427 
2428  double ba_x = (B->x - A->x);
2429  double ba_y = (B->y - A->y);
2430  double ab_length_sqr = (ba_x * ba_x + ba_y * ba_y);
2431  double ca_x = (C->x - A->x);
2432  double ca_y = (C->y - A->y);
2433  double dot_ac_ab = (ca_x * ba_x + ca_y * ba_y);
2434 
2435  if (dot_ac_ab <= 0)
2436  return distance2d_sqr_pt_pt(C, A);
2437  if (dot_ac_ab >= ab_length_sqr)
2438  return distance2d_sqr_pt_pt(C, B);
2439 
2440  /*
2441  * (2)
2442  * (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
2443  * s = -----------------------------
2444  * L^2
2445  *
2446  * Then the distance from C to P = |s|*L.
2447  *
2448  */
2449 
2450  double s_numerator = ca_x * ba_y - ca_y * ba_x;
2451 
2452  /* Distance = (s_num / ab) * (s_num / ab) * ab == s_num * s_num / ab) */
2453  return s_numerator * s_numerator / ab_length_sqr;
2454 }
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:35
double y
Definition: liblwgeom.h:376
double x
Definition: liblwgeom.h:376

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: