PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ distance2d_sqr_pt_seg()

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

Definition at line 2316 of file measures.c.

2317 {
2318  /*if start==end, then use pt distance */
2319  if ((A->x == B->x) && (A->y == B->y))
2320  return distance2d_sqr_pt_pt(C, A);
2321 
2322  /*
2323  * otherwise, we use comp.graphics.algorithms
2324  * Frequently Asked Questions method
2325  *
2326  * (1) AC dot AB
2327  * r = ---------
2328  * ||AB||^2
2329  * r has the following meaning:
2330  * r=0 P = A
2331  * r=1 P = B
2332  * r<0 P is on the backward extension of AB
2333  * r>1 P is on the forward extension of AB
2334  * 0<r<1 P is interior to AB
2335  */
2336 
2337  double ba_x = (B->x - A->x);
2338  double ba_y = (B->y - A->y);
2339  double ab_length_sqr = (ba_x * ba_x + ba_y * ba_y);
2340  double ca_x = (C->x - A->x);
2341  double ca_y = (C->y - A->y);
2342  double dot_ac_ab = (ca_x * ba_x + ca_y * ba_y);
2343 
2344  if (dot_ac_ab <= 0)
2345  return distance2d_sqr_pt_pt(C, A);
2346  if (dot_ac_ab >= ab_length_sqr)
2347  return distance2d_sqr_pt_pt(C, B);
2348 
2349  /*
2350  * (2)
2351  * (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
2352  * s = -----------------------------
2353  * L^2
2354  *
2355  * Then the distance from C to P = |s|*L.
2356  *
2357  */
2358 
2359  double s_numerator = ca_x * ba_y - ca_y * ba_x;
2360 
2361  /* Distance = (s_num / ab) * (s_num / ab) * ab == s_num * s_num / ab) */
2362  return s_numerator * s_numerator / ab_length_sqr;
2363 }
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(), ptarray_closest_segment_2d(), and ptarray_locate_point().

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