/// <summary> /// Returns the intersections within the set. Each intersection has and /// associated point an 2 indexes corresponding to the lines that /// created the intersection. /// </summary> /// <param name="pPoints">Output. The point set.</param> /// <param name="pIndexes1">Output. The indexes.</param> /// <param name="pIndexes2">Output. The indexes.</param> public void GetIntersections(List <C2DPoint> pPoints, List <int> pIndexes1, List <int> pIndexes2) { var Lines = new List <CLineBaseRect>(); for (var i = 0; i < Count; i++) { var LineRect = new CLineBaseRect(); LineRect.Line = this[i]; LineRect.Line.GetBoundingRect(LineRect.Rect); LineRect.usIndex = i; Lines.Add(LineRect); } var Comparitor = new CLineBaseRectLeftToRight(); Lines.Sort(Comparitor); var j = 0; var IntPt = new List <C2DPoint>(); // For each line... while (j < Lines.Count) { var r = j + 1; var dXLimit = Lines[j].Rect.GetRight(); // ...search forward untill the end or a line whose rect starts after this ends while (r < Lines.Count && Lines[r].Rect.GetLeft() < dXLimit) { if (Lines[j].Rect.Overlaps(Lines[r].Rect) && Lines[j].Line.Crosses(Lines[r].Line, IntPt)) { while (IntPt.Count > 0) { pPoints.Add(IntPt[IntPt.Count - 1]); IntPt.RemoveAt(IntPt.Count - 1); pIndexes1.Add(Lines[j].usIndex); pIndexes2.Add(Lines[r].usIndex); } } r++; } j++; } }
/// <summary> /// True if there are crossing lines within the set. /// </summary> public bool HasCrossingLines() { // Set up an array of these structures and the left most points of the line rects var Lines = new List <CLineBaseRect>(); for (var i = 0; i < Count; i++) { var LineRect = new CLineBaseRect(); LineRect.Line = this[i]; LineRect.Line.GetBoundingRect(LineRect.Rect); Lines.Add(LineRect); } var Comparitor = new CLineBaseRectLeftToRight(); Lines.Sort(Comparitor); var j = 0; var IntPt = new List <C2DPoint>(); var bIntersect = false; // For each line... while (j < Lines.Count && !bIntersect) { var r = j + 1; var dXLimit = Lines[j].Rect.GetRight(); // ...search forward untill the end or a line whose rect starts after this ends while (!bIntersect && r < Lines.Count && Lines[r].Rect.GetLeft() < dXLimit) { if (Lines[j].Rect.Overlaps(Lines[r].Rect) && Lines[j].Line.Crosses(Lines[r].Line, IntPt)) { bIntersect = true; } r++; } j++; } return(bIntersect); }
/// <summary> /// Returns the intersections with this set and the other. /// Each intersection has an associated point and 2 indexes /// corresponding to the lines that created the intersection. /// </summary> /// <param name="Other">Input. The other line set.</param> /// <param name="pPoints">Output. The intersection points.</param> /// <param name="pIndexesThis">Output. The indexes for this.</param> /// <param name="pIndexesOther">Output. The indexes for the other set.</param> /// <param name="pBoundingRectThis">Input. The bounding rect for this.</param> /// <param name="pBoundingRectOther">Input. The bounding rect for the other.</param> public void GetIntersections(List <C2DLineBase> Other, List <C2DPoint> pPoints, List <int> pIndexesThis, List <int> pIndexesOther, C2DRect pBoundingRectThis, C2DRect pBoundingRectOther) { var Lines = new List <CLineBaseRect>(); for (var i = 0; i < Count; i++) { var LineRect = new CLineBaseRect(); LineRect.Line = this[i]; LineRect.Line.GetBoundingRect(LineRect.Rect); LineRect.usIndex = i; LineRect.bSetFlag = true; if (pBoundingRectOther.Overlaps(LineRect.Rect)) { Lines.Add(LineRect); } } for (var d = 0; d < Other.Count; d++) { var LineRect = new CLineBaseRect(); LineRect.Line = Other[d]; LineRect.Line.GetBoundingRect(LineRect.Rect); LineRect.usIndex = d; LineRect.bSetFlag = false; if (pBoundingRectThis.Overlaps(LineRect.Rect)) { Lines.Add(LineRect); } } var Comparitor = new CLineBaseRectLeftToRight(); Lines.Sort(Comparitor); var j = 0; var IntPt = new List <C2DPoint>(); while (j < Lines.Count) { var r = j + 1; var dXLimit = Lines[j].Rect.GetRight(); while (r < Lines.Count && Lines[r].Rect.GetLeft() < dXLimit) { if ((Lines[j].bSetFlag ^ Lines[r].bSetFlag) && Lines[j].Rect.Overlaps(Lines[r].Rect) && Lines[j].Line.Crosses(Lines[r].Line, IntPt)) { while (IntPt.Count > 0) { pPoints.Add(IntPt[IntPt.Count - 1]); IntPt.RemoveAt(IntPt.Count - 1); if (Lines[j].bSetFlag) { pIndexesThis.Add(Lines[j].usIndex); } else { pIndexesThis.Add(Lines[r].usIndex); } if (Lines[j].bSetFlag) { pIndexesOther.Add(Lines[r].usIndex); } else { pIndexesOther.Add(Lines[j].usIndex); } } } r++; } j++; } }