PostGIS  2.5.0dev-r@@SVN_REVISION@@
 int lw_dist2d_seg_seg ( const POINT2D * A, const POINT2D * B, const POINT2D * C, const POINT2D * D, DISTPTS * dl )

Finds the shortest distance between two segments.

This function is changed so it is not doing any comparasion of distance but just sending every possible combination further to lw_dist2d_pt_seg

Definition at line 1816 of file measures.c.

Referenced by lw_dist2d_arc_arc(), lw_dist2d_ptarray_ptarray(), and lw_dist2d_seg_arc().

1817 {
1818  double s_top, s_bot,s;
1819  double r_top, r_bot,r;
1820
1821  LWDEBUGF(2, "lw_dist2d_seg_seg [%g,%g]->[%g,%g] by [%g,%g]->[%g,%g]",
1822  A->x,A->y,B->x,B->y, C->x,C->y, D->x, D->y);
1823
1824  /*A and B are the same point */
1825  if ( ( A->x == B->x) && (A->y == B->y) )
1826  {
1827  return lw_dist2d_pt_seg(A,C,D,dl);
1828  }
1829  /*U and V are the same point */
1830
1831  if ( ( C->x == D->x) && (C->y == D->y) )
1832  {
1833  dl->twisted= ((dl->twisted) * (-1));
1834  return lw_dist2d_pt_seg(D,A,B,dl);
1835  }
1836  /* AB and CD are line segments */
1837  /* from comp.graphics.algo
1838
1839  Solving the above for r and s yields
1840  (Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy)
1841  r = ----------------------------- (eqn 1)
1842  (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)
1843
1844  (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
1845  s = ----------------------------- (eqn 2)
1846  (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)
1847  Let P be the position vector of the intersection point, then
1848  P=A+r(B-A) or
1849  Px=Ax+r(Bx-Ax)
1850  Py=Ay+r(By-Ay)
1851  By examining the values of r & s, you can also determine some other limiting conditions:
1852  If 0<=r<=1 & 0<=s<=1, intersection exists
1853  r<0 or r>1 or s<0 or s>1 line segments do not intersect
1854  If the denominator in eqn 1 is zero, AB & CD are parallel
1855  If the numerator in eqn 1 is also zero, AB & CD are collinear.
1856
1857  */
1858  r_top = (A->y-C->y)*(D->x-C->x) - (A->x-C->x)*(D->y-C->y);
1859  r_bot = (B->x-A->x)*(D->y-C->y) - (B->y-A->y)*(D->x-C->x);
1860
1861  s_top = (A->y-C->y)*(B->x-A->x) - (A->x-C->x)*(B->y-A->y);
1862  s_bot = (B->x-A->x)*(D->y-C->y) - (B->y-A->y)*(D->x-C->x);
1863
1864  if ( (r_bot==0) || (s_bot == 0) )
1865  {
1866  if ((lw_dist2d_pt_seg(A,C,D,dl)) && (lw_dist2d_pt_seg(B,C,D,dl)))
1867  {
1868  dl->twisted= ((dl->twisted) * (-1)); /*here we change the order of inputted geometrys and that we notice by changing sign on dl->twisted*/
1869  return ((lw_dist2d_pt_seg(C,A,B,dl)) && (lw_dist2d_pt_seg(D,A,B,dl))); /*if all is successful we return true*/
1870  }
1871  else
1872  {
1873  return LW_FALSE; /* if any of the calls to lw_dist2d_pt_seg goes wrong we return false*/
1874  }
1875  }
1876
1877  s = s_top/s_bot;
1878  r= r_top/r_bot;
1879
1880  if (((r<0) || (r>1) || (s<0) || (s>1)) || (dl->mode == DIST_MAX))
1881  {
1882  if ((lw_dist2d_pt_seg(A,C,D,dl)) && (lw_dist2d_pt_seg(B,C,D,dl)))
1883  {
1884  dl->twisted= ((dl->twisted) * (-1)); /*here we change the order of inputted geometrys and that we notice by changing sign on dl->twisted*/
1885  return ((lw_dist2d_pt_seg(C,A,B,dl)) && (lw_dist2d_pt_seg(D,A,B,dl))); /*if all is successful we return true*/
1886  }
1887  else
1888  {
1889  return LW_FALSE; /* if any of the calls to lw_dist2d_pt_seg goes wrong we return false*/
1890  }
1891  }
1892  else
1893  {
1894  if (dl->mode == DIST_MIN) /*If there is intersection we identify the intersection point and return it but only if we are looking for mindistance*/
1895  {
1896  POINT2D theP;
1897
1898  if (((A->x==C->x)&&(A->y==C->y))||((A->x==D->x)&&(A->y==D->y)))
1899  {
1900  theP.x = A->x;
1901  theP.y = A->y;
1902  }
1903  else if (((B->x==C->x)&&(B->y==C->y))||((B->x==D->x)&&(B->y==D->y)))
1904  {
1905  theP.x = B->x;
1906  theP.y = B->y;
1907  }
1908  else
1909  {
1910  theP.x = A->x+r*(B->x-A->x);
1911  theP.y = A->y+r*(B->y-A->y);
1912  }
1913  dl->distance=0.0;
1914  dl->p1=theP;
1915  dl->p2=theP;
1916  }
1917  return LW_TRUE;
1918  }
1919 }
char * r
Definition: cu_in_wkt.c:24
int mode
Definition: measures.h:51
POINT2D p1
Definition: measures.h:49
double x
Definition: liblwgeom.h:327
#define DIST_MIN
Definition: measures.h:41
#define LW_FALSE
Definition: liblwgeom.h:76
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
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 i...
Definition: measures.c:2200
POINT2D p2
Definition: measures.h:50
double y
Definition: liblwgeom.h:327
char * s
Definition: cu_in_wkt.c:23
int twisted
Definition: measures.h:52
double distance
Definition: measures.h:48
#define DIST_MAX
Definition: measures.h:40
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88

Here is the call graph for this function:

Here is the caller graph for this function: