PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ distance2d_sqr_pt_seg()

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

Definition at line 2455 of file measures.c.

2456 {
2457  /*if start==end, then use pt distance */
2458  if ((A->x == B->x) && (A->y == B->y))
2459  return distance2d_sqr_pt_pt(C, A);
2460 
2461  /*
2462  * otherwise, we use comp.graphics.algorithms
2463  * Frequently Asked Questions method
2464  *
2465  * (1) AC dot AB
2466  * r = ---------
2467  * ||AB||^2
2468  * r has the following meaning:
2469  * r=0 P = A
2470  * r=1 P = B
2471  * r<0 P is on the backward extension of AB
2472  * r>1 P is on the forward extension of AB
2473  * 0<r<1 P is interior to AB
2474  */
2475 
2476  double ba_x = (B->x - A->x);
2477  double ba_y = (B->y - A->y);
2478  double ab_length_sqr = (ba_x * ba_x + ba_y * ba_y);
2479  double ca_x = (C->x - A->x);
2480  double ca_y = (C->y - A->y);
2481  double dot_ac_ab = (ca_x * ba_x + ca_y * ba_y);
2482 
2483  if (dot_ac_ab <= 0)
2484  return distance2d_sqr_pt_pt(C, A);
2485  if (dot_ac_ab >= ab_length_sqr)
2486  return distance2d_sqr_pt_pt(C, B);
2487 
2488  /*
2489  * (2)
2490  * (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
2491  * s = -----------------------------
2492  * L^2
2493  *
2494  * Then the distance from C to P = |s|*L.
2495  *
2496  */
2497 
2498  double s_numerator = ca_x * ba_y - ca_y * ba_x;
2499 
2500  /* Distance = (s_num / ab) * (s_num / ab) * ab == s_num * s_num / ab) */
2501  return s_numerator * s_numerator / ab_length_sqr;
2502 }
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:35
double y
Definition: liblwgeom.h:405
double x
Definition: liblwgeom.h:405

References distance2d_sqr_pt_pt(), POINT2D::x, and POINT2D::y.

Referenced by lwline_split_by_point_to(), ptarray_closest_segment_2d(), and ptarray_locate_point().

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