/// <summary> /// True if part of this line is above the other. Returns the point /// on this and on the other. /// </summary> /// <param name="Other"></param> /// <param name="dVerticalDistance"></param> /// <param name="ptOnThis"></param> /// <param name="ptOnOther"></param> /// <returns></returns> public bool OverlapsAbove(C2DLine Other, ref double dVerticalDistance, C2DPoint ptOnThis, C2DPoint ptOnOther) { // Get the 2 points for both lines C2DPoint OtherTo = new C2DPoint(Other.point.x + Other.vector.i, Other.point.y + Other.vector.j); C2DPoint ThisTo = new C2DPoint(point.x + vector.i, point.y + vector.j); // Make an interval for both in the x plane CInterval iThis = new CInterval( point.x, point.x); iThis.ExpandToInclude( ThisTo.x ); CInterval iOther = new CInterval( Other.point.x, Other.point.x); iOther.ExpandToInclude( OtherTo.x ); // This is an interval for the overlap between the 2 CInterval iOverlap = new CInterval(); // If there is an overlap... if (iThis.Overlaps(iOther, iOverlap)) { double dThisYMin; double dThisYMax; double dOtherYMin; double dOtherYMax; // If the line is vertical then y at the x min / max can be set to the ends of the line. if (vector.i == 0) { dThisYMin = point.y; dThisYMax = ThisTo.y; } else // otherwise, caluclate the y values at the interval ends { dThisYMin = GetY(iOverlap.dMin); dThisYMax = GetY(iOverlap.dMax); } // Now do the same for the other line if (Other.vector.i == 0) { dOtherYMin = Other.point.y; dOtherYMax = OtherTo.y; } else { dOtherYMin = Other.GetY(iOverlap.dMin); dOtherYMax = Other.GetY(iOverlap.dMax); } // Now find the distance between the 2 at the ends double dDistMin = dThisYMin - dOtherYMin; double dDistMax = dThisYMax - dOtherYMax; // If they are both > 0 then no intersection if ( (dDistMin > 0) && (dDistMax > 0)) { dDistMin = Math.Abs( dDistMin); dDistMax = Math.Abs(dDistMax); // find which one is smallest if ( dDistMin > dDistMax) { dVerticalDistance = dDistMax; // distance at the max is smallest ptOnThis.x = iOverlap.dMax; ptOnThis.y = dThisYMax; ptOnOther.x = iOverlap.dMax; ptOnOther.y = dOtherYMax; } else { dVerticalDistance = dDistMin; // distance at the min is smallest ptOnThis.x = iOverlap.dMin; ptOnThis.y = dThisYMin; ptOnOther.x = iOverlap.dMin; ptOnOther.y = dOtherYMin; } return true; } else if ( (dDistMin < 0) && (dDistMax < 0)) // This is below. { return false; } else { // find the intersection. dVerticalDistance = 0; C2DPointSet pts = new C2DPointSet(); if(this.Crosses(Other, pts)) { ptOnThis = pts[0]; ptOnOther = ptOnThis; } else { Debug.Assert(false); } } return true; } else { return false; } }
/// <summary> /// True if part of this line is above the other. Returns the point /// on this and on the other. /// </summary> /// <param name="Other"></param> /// <param name="dVerticalDistance"></param> /// <param name="ptOnThis"></param> /// <param name="ptOnOther"></param> /// <returns></returns> public bool OverlapsAbove(C2DLine Other, ref double dVerticalDistance, C2DPoint ptOnThis, C2DPoint ptOnOther) { // Get the 2 points for both lines var OtherTo = new C2DPoint(Other.point.x + Other.vector.i, Other.point.y + Other.vector.j); var ThisTo = new C2DPoint(point.x + vector.i, point.y + vector.j); // Make an interval for both in the x plane var iThis = new CInterval(point.x, point.x); iThis.ExpandToInclude(ThisTo.x); var iOther = new CInterval(Other.point.x, Other.point.x); iOther.ExpandToInclude(OtherTo.x); // This is an interval for the overlap between the 2 var iOverlap = new CInterval(); // If there is an overlap... if (iThis.Overlaps(iOther, iOverlap)) { double dThisYMin; double dThisYMax; double dOtherYMin; double dOtherYMax; // If the line is vertical then y at the x min / max can be set to the ends of the line. if (vector.i == 0) { dThisYMin = point.y; dThisYMax = ThisTo.y; } else // otherwise, caluclate the y values at the interval ends { dThisYMin = GetY(iOverlap.dMin); dThisYMax = GetY(iOverlap.dMax); } // Now do the same for the other line if (Other.vector.i == 0) { dOtherYMin = Other.point.y; dOtherYMax = OtherTo.y; } else { dOtherYMin = Other.GetY(iOverlap.dMin); dOtherYMax = Other.GetY(iOverlap.dMax); } // Now find the distance between the 2 at the ends var dDistMin = dThisYMin - dOtherYMin; var dDistMax = dThisYMax - dOtherYMax; // If they are both > 0 then no intersection if ((dDistMin > 0) && (dDistMax > 0)) { dDistMin = Math.Abs(dDistMin); dDistMax = Math.Abs(dDistMax); // find which one is smallest if (dDistMin > dDistMax) { dVerticalDistance = dDistMax; // distance at the max is smallest ptOnThis.x = iOverlap.dMax; ptOnThis.y = dThisYMax; ptOnOther.x = iOverlap.dMax; ptOnOther.y = dOtherYMax; } else { dVerticalDistance = dDistMin; // distance at the min is smallest ptOnThis.x = iOverlap.dMin; ptOnThis.y = dThisYMin; ptOnOther.x = iOverlap.dMin; ptOnOther.y = dOtherYMin; } return(true); } else if ((dDistMin < 0) && (dDistMax < 0)) // This is below. { return(false); } else { // find the intersection. dVerticalDistance = 0; var pts = new C2DPointSet(); if (this.Crosses(Other, pts)) { ptOnThis = pts[0]; ptOnOther = ptOnThis; } else { Debug.Assert(false); } } return(true); } else { return(false); } }