PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ lw_dist2d_pt_seg()

int lw_dist2d_pt_seg ( const POINT2D p,
const POINT2D A,
const POINT2D B,
DISTPTS dl 
)

lw_dist2d_comp from p to line A->B This one is now sending every occation to lw_dist2d_pt_pt Before it was handling occations where r was between 0 and 1 internally and just returning the distance without identifying the points.

To get this points it was nessecary to change and it also showed to be about 10faster.

Definition at line 2189 of file measures.c.

References DIST_MAX, DIST_MIN, DISTPTS::distance, lw_dist2d_pt_pt(), DISTPTS::mode, DISTPTS::p1, DISTPTS::p2, r, POINT2D::x, and POINT2D::y.

Referenced by lw_dist2d_pt_arc(), lw_dist2d_pt_ptarray(), lw_dist2d_seg_arc(), lw_dist2d_seg_seg(), and lw_dist2d_selected_seg_seg().

2190 {
2191  POINT2D c;
2192  double r;
2193  /*if start==end, then use pt distance */
2194  if ( ( A->x == B->x) && (A->y == B->y) )
2195  {
2196  return lw_dist2d_pt_pt(p,A,dl);
2197  }
2198  /*
2199  * otherwise, we use comp.graphics.algorithms
2200  * Frequently Asked Questions method
2201  *
2202  * (1) AC dot AB
2203  * r = ---------
2204  * ||AB||^2
2205  * r has the following meaning:
2206  * r=0 P = A
2207  * r=1 P = B
2208  * r<0 P is on the backward extension of AB
2209  * r>1 P is on the forward extension of AB
2210  * 0<r<1 P is interior to AB
2211  */
2212 
2213  r = ( (p->x-A->x) * (B->x-A->x) + (p->y-A->y) * (B->y-A->y) )/( (B->x-A->x)*(B->x-A->x) +(B->y-A->y)*(B->y-A->y) );
2214 
2215  /*This is for finding the maxdistance.
2216  the maxdistance have to be between two vertexes,
2217  compared to mindistance which can be between
2218  tvo vertexes vertex.*/
2219  if (dl->mode == DIST_MAX)
2220  {
2221  if (r>=0.5)
2222  {
2223  return lw_dist2d_pt_pt(p,A,dl);
2224  }
2225  if (r<0.5)
2226  {
2227  return lw_dist2d_pt_pt(p,B,dl);
2228  }
2229  }
2230 
2231  if (r<0) /*If p projected on the line is outside point A*/
2232  {
2233  return lw_dist2d_pt_pt(p,A,dl);
2234  }
2235  if (r>=1) /*If p projected on the line is outside point B or on point B*/
2236  {
2237  return lw_dist2d_pt_pt(p,B,dl);
2238  }
2239 
2240  /*If the point p is on the segment this is a more robust way to find out that*/
2241  if (( ((A->y-p->y)*(B->x-A->x)==(A->x-p->x)*(B->y-A->y) ) ) && (dl->mode == DIST_MIN))
2242  {
2243  dl->distance = 0.0;
2244  dl->p1 = *p;
2245  dl->p2 = *p;
2246  }
2247 
2248  /*If the projection of point p on the segment is between A and B
2249  then we find that "point on segment" and send it to lw_dist2d_pt_pt*/
2250  c.x=A->x + r * (B->x-A->x);
2251  c.y=A->y + r * (B->y-A->y);
2252 
2253  return lw_dist2d_pt_pt(p,&c,dl);
2254 }
char * r
Definition: cu_in_wkt.c:24
int mode
Definition: measures.h:27
POINT2D p1
Definition: measures.h:25
double x
Definition: liblwgeom.h:312
#define DIST_MIN
Definition: measures.h:17
POINT2D p2
Definition: measures.h:26
double y
Definition: liblwgeom.h:312
double distance
Definition: measures.h:24
#define DIST_MAX
Definition: measures.h:16
int lw_dist2d_pt_pt(const POINT2D *thep1, const POINT2D *thep2, DISTPTS *dl)
Compares incomming points and stores the points closest to each other or most far away from each othe...
Definition: measures.c:2265
Here is the call graph for this function:
Here is the caller graph for this function: