PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lw_dist2d_seg_seg()

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 comparison of distance but just sending every possible combination further to lw_dist2d_pt_seg

Definition at line 1917 of file measures.c.

1918 {
1919  double s_top, s_bot, s;
1920  double r_top, r_bot, r;
1921 
1922  /*A and B are the same point */
1923  if ((A->x == B->x) && (A->y == B->y))
1924  {
1925  return lw_dist2d_pt_seg(A, C, D, dl);
1926  }
1927 
1928  /*U and V are the same point */
1929  if ((C->x == D->x) && (C->y == D->y))
1930  {
1931  dl->twisted = ((dl->twisted) * (-1));
1932  return lw_dist2d_pt_seg(D, A, B, dl);
1933  }
1934 
1935  /* AB and CD are line segments */
1936  /* from comp.graphics.algo
1937 
1938  Solving the above for r and s yields
1939  (Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy)
1940  r = ----------------------------- (eqn 1)
1941  (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)
1942 
1943  (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
1944  s = ----------------------------- (eqn 2)
1945  (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)
1946  Let P be the position vector of the intersection point, then
1947  P=A+r(B-A) or
1948  Px=Ax+r(Bx-Ax)
1949  Py=Ay+r(By-Ay)
1950  By examining the values of r & s, you can also determine some other limiting conditions:
1951  If 0<=r<=1 & 0<=s<=1, intersection exists
1952  r<0 or r>1 or s<0 or s>1 line segments do not intersect
1953  If the denominator in eqn 1 is zero, AB & CD are parallel
1954  If the numerator in eqn 1 is also zero, AB & CD are collinear.
1955 
1956  */
1957  r_top = (A->y - C->y) * (D->x - C->x) - (A->x - C->x) * (D->y - C->y);
1958  r_bot = (B->x - A->x) * (D->y - C->y) - (B->y - A->y) * (D->x - C->x);
1959 
1960  s_top = (A->y - C->y) * (B->x - A->x) - (A->x - C->x) * (B->y - A->y);
1961  s_bot = (B->x - A->x) * (D->y - C->y) - (B->y - A->y) * (D->x - C->x);
1962 
1963  if ((r_bot == 0) || (s_bot == 0))
1964  {
1965  if ((lw_dist2d_pt_seg(A, C, D, dl)) && (lw_dist2d_pt_seg(B, C, D, dl)))
1966  {
1967  /* change the order of inputted geometries and that we notice by changing sign on dl->twisted*/
1968  dl->twisted *= -1;
1969  return ((lw_dist2d_pt_seg(C, A, B, dl)) && (lw_dist2d_pt_seg(D, A, B, dl)));
1970  }
1971  else
1972  return LW_FALSE; /* if any of the calls to lw_dist2d_pt_seg goes wrong we return false*/
1973  }
1974 
1975  s = s_top / s_bot;
1976  r = r_top / r_bot;
1977 
1978  if (((r < 0) || (r > 1) || (s < 0) || (s > 1)) || (dl->mode == DIST_MAX))
1979  {
1980  if ((lw_dist2d_pt_seg(A, C, D, dl)) && (lw_dist2d_pt_seg(B, C, D, dl)))
1981  {
1982  /* change the order of inputted geometries and that we notice by changing sign on dl->twisted*/
1983  dl->twisted *= -1;
1984  return ((lw_dist2d_pt_seg(C, A, B, dl)) && (lw_dist2d_pt_seg(D, A, B, dl)));
1985  }
1986  else
1987  return LW_FALSE; /* if any of the calls to lw_dist2d_pt_seg goes wrong we return false*/
1988  }
1989  else
1990  {
1991  /* If there is intersection we identify the intersection point and return it but only if we are looking
1992  * for mindistance */
1993  if (dl->mode == DIST_MIN)
1994  {
1995  POINT2D theP;
1996 
1997  if (((A->x == C->x) && (A->y == C->y)) || ((A->x == D->x) && (A->y == D->y)))
1998  {
1999  theP.x = A->x;
2000  theP.y = A->y;
2001  }
2002  else if (((B->x == C->x) && (B->y == C->y)) || ((B->x == D->x) && (B->y == D->y)))
2003  {
2004  theP.x = B->x;
2005  theP.y = B->y;
2006  }
2007  else
2008  {
2009  theP.x = A->x + r * (B->x - A->x);
2010  theP.y = A->y + r * (B->y - A->y);
2011  }
2012  dl->distance = 0.0;
2013  dl->p1 = theP;
2014  dl->p2 = theP;
2015  }
2016  return LW_TRUE;
2017  }
2018 }
char * s
Definition: cu_in_wkt.c:23
char * r
Definition: cu_in_wkt.c:24
#define LW_FALSE
Definition: liblwgeom.h:94
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
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 occasion to lw_dist2d_pt_pt Before i...
Definition: measures.c:2306
#define DIST_MIN
Definition: measures.h:44
#define DIST_MAX
Definition: measures.h:43
POINT2D p1
Definition: measures.h:52
POINT2D p2
Definition: measures.h:53
int twisted
Definition: measures.h:55
int mode
Definition: measures.h:54
double distance
Definition: measures.h:51
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390

References DIST_MAX, DIST_MIN, DISTPTS::distance, lw_dist2d_pt_seg(), LW_FALSE, LW_TRUE, DISTPTS::mode, DISTPTS::p1, DISTPTS::p2, r, s, DISTPTS::twisted, POINT2D::x, and POINT2D::y.

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

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