PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ptarray_closest_segment_2d()

int ptarray_closest_segment_2d ( const POINTARRAY pa,
const POINT2D qp,
double *  dist 
)
Parameters
pathe subject pointarray
qpthe query point
distoptional output for actual distance from segment
Returns
0-based segment index for the closest segment (earliest segment in case of same distance)

Definition at line 1320 of file ptarray.c.

1321 {
1322  const POINT2D *start = getPoint2d_cp(pa, 0), *end = NULL;
1323  uint32_t t, seg=0;
1324  double mindist=DBL_MAX;
1325 
1326  /* Loop through pointarray looking for nearest segment */
1327  for (t=1; t<pa->npoints; t++)
1328  {
1329  double dist_sqr;
1330  end = getPoint2d_cp(pa, t);
1331  dist_sqr = distance2d_sqr_pt_seg(qp, start, end);
1332 
1333  if (dist_sqr < mindist)
1334  {
1335  mindist = dist_sqr;
1336  seg=t-1;
1337  if ( mindist == 0 )
1338  {
1339  LWDEBUG(3, "Breaking on mindist=0");
1340  break;
1341  }
1342  }
1343 
1344  start = end;
1345  }
1346 
1347  if ( dist ) *dist = sqrt(mindist);
1348  return seg;
1349 }
double distance2d_sqr_pt_seg(const POINT2D *p, const POINT2D *A, const POINT2D *B)
Definition: measures.c:2408
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
uint32_t npoints
Definition: liblwgeom.h:427

References distance2d_sqr_pt_seg(), getPoint2d_cp(), LWDEBUG, and POINTARRAY::npoints.

Referenced by lwt_GetFaceContainingPoint(), and test_ptarray_closest_segment_2d().

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