// computes distance between two parallel lines double IntersectionRectangle::parallelDist(const DLine& d1, const DLine& d2) const { OGDF_ASSERT((d1.isHorizontal() && d2.isHorizontal()) || (d1.isVertical() && d2.isVertical())); double d1min, d1max, d2min, d2max, paraDist, dist; if(d1.isVertical()) { d1min = d1.start().m_y; d1max = d1.end().m_y; d2min = d2.start().m_y; d2max = d2.end().m_y; paraDist = fabs(d1.start().m_x - d2.start().m_x); } else { d1min = d1.start().m_x; d1max = d1.end().m_x; d2min = d2.start().m_x; d2max = d2.end().m_x; paraDist = fabs(d1.start().m_y - d2.start().m_y); } if(d1min > d1max) swap(d1min,d1max); if(d2min > d2max) swap(d2min,d2max); if(d1min > d2max || d2min > d1max) { // no overlap dist = pointDist(d1.start(),d2.start()); dist = min(dist,pointDist(d1.start(),d2.end())); dist = min(dist,pointDist(d1.end(),d2.start())); dist = min(dist,pointDist(d1.end(),d2.end())); } else dist = paraDist; // segments overlap return dist; }
// gives the intersection-point between two lines, returns true, if any // computes the crossing point between the (infinite) lines // defined by the endpoints of the DLines, then checks if it // lies within the two rectangles defined by the DLines endpoints bool DLine::intersection( const DLine &line, DPoint &inter, bool endpoints) const { double ix, iy; //do not return true if parallel edges are encountered if (slope() == line.slope()) return false; //two possible checks: // only check for overlap on endpoints if option parameter set, // compute crossing otherwise // or skip computation if endpoints overlap (can't have "real" crossing) // (currently implemented) //if (endpoints) { if (m_start == line.m_start || m_start == line.m_end) { inter = m_start; if (endpoints) return true; else return false; } if (m_end == line.m_start || m_end == line.m_end) { inter = m_end; if (endpoints) return true; else return false; } //}//if endpoints //if the edge is vertical, we cannot compute the slope if (isVertical()) ix = m_start.m_x; else if (line.isVertical()) ix = line.m_start.m_x; else ix = (line.yAbs() - yAbs())/(slope() - line.slope()); //set iy to the value of the infinite line at xvalue ix //use a non-vertical line (can't be both, otherwise they're parallel) if (isVertical()) iy = line.slope() * ix + line.yAbs(); else iy = slope() * ix + yAbs(); inter = DPoint(ix, iy); //the (infinite) lines cross point DRect tRect(line); DRect mRect(*this); return (tRect.contains(inter) && mRect.contains(inter)); }