Skip to content

Commit 9514748

Browse files
committed
Merge branch 'master' into beta
2 parents 5b85975 + 87c9839 commit 9514748

File tree

2 files changed

+103
-10
lines changed

2 files changed

+103
-10
lines changed

src/Advanced.Algorithms/Geometry/BentleyOttmann.cs

+19-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class BentleyOttmann
1313
private readonly PointComparer pointComparer;
1414

1515
private HashSet<Event> verticalHorizontalLines;
16-
private HashSet<Event> normalLines;
16+
private HashSet<Event> otherLines;
1717

1818
private BHeap<Event> eventQueue;
1919
private HashSet<Event> eventQueueLookUp;
@@ -42,7 +42,7 @@ private void initialize(IEnumerable<Line> lineSegments)
4242
intersectionEvents = new Dictionary<Point, HashSet<Tuple<Event, Event>>>(pointComparer);
4343

4444
verticalHorizontalLines = new HashSet<Event>();
45-
normalLines = new HashSet<Event>();
45+
otherLines = new HashSet<Event>();
4646

4747
rightLeftEventLookUp = lineSegments
4848
.Select(x =>
@@ -81,6 +81,7 @@ public Dictionary<Point, List<Line>> FindIntersections(IEnumerable<Line> lineSeg
8181
{
8282
case EventType.Start:
8383

84+
//special case
8485
if (verticalHorizontalLines.Count > 0)
8586
{
8687
foreach (var line in verticalHorizontalLines)
@@ -90,11 +91,12 @@ public Dictionary<Point, List<Line>> FindIntersections(IEnumerable<Line> lineSeg
9091
}
9192
}
9293

94+
//special case
9395
if (currentEvent.Segment.IsVertical || currentEvent.Segment.IsHorizontal)
9496
{
9597
verticalHorizontalLines.Add(currentEvent);
9698

97-
foreach (var line in normalLines)
99+
foreach (var line in otherLines)
98100
{
99101
var intersection = findIntersection(currentEvent, line);
100102
recordIntersection(currentEvent, line, intersection);
@@ -103,7 +105,7 @@ public Dictionary<Point, List<Line>> FindIntersections(IEnumerable<Line> lineSeg
103105
break;
104106
}
105107

106-
normalLines.Add(currentEvent);
108+
otherLines.Add(currentEvent);
107109

108110
currentlyTrackedLines.Insert(currentEvent);
109111

@@ -124,13 +126,14 @@ public Dictionary<Point, List<Line>> FindIntersections(IEnumerable<Line> lineSeg
124126

125127
currentEvent = rightLeftEventLookUp[currentEvent];
126128

129+
//special case
127130
if (currentEvent.Segment.IsVertical || currentEvent.Segment.IsHorizontal)
128131
{
129132
verticalHorizontalLines.Remove(currentEvent);
130133
break;
131134
}
132135

133-
normalLines.Remove(currentEvent);
136+
otherLines.Remove(currentEvent);
134137

135138
lower = currentlyTrackedLines.NextLower(currentEvent);
136139
upper = currentlyTrackedLines.NextHigher(currentEvent);
@@ -147,18 +150,25 @@ public Dictionary<Point, List<Line>> FindIntersections(IEnumerable<Line> lineSeg
147150

148151
var intersectionLines = intersectionEvents[currentEvent as Point];
149152

150-
foreach (var item in intersectionLines)
153+
foreach (var lines in intersectionLines)
151154
{
152-
swapBstNodes(currentlyTrackedLines, item.Item1, item.Item2);
155+
//special case
156+
if (lines.Item1.Segment.IsHorizontal || lines.Item1.Segment.IsVertical
157+
|| lines.Item2.Segment.IsHorizontal || lines.Item2.Segment.IsVertical)
158+
{
159+
continue;
160+
}
161+
162+
swapBstNodes(currentlyTrackedLines, lines.Item1, lines.Item2);
153163

154-
var upperLine = item.Item1;
164+
var upperLine = lines.Item1;
155165
var upperUpper = currentlyTrackedLines.NextHigher(upperLine);
156166

157167
var newUpperIntersection = findIntersection(upperLine, upperUpper);
158168
recordIntersection(upperLine, upperUpper, newUpperIntersection);
159169
enqueueIntersectionEvent(currentEvent, newUpperIntersection);
160170

161-
var lowerLine = item.Item2;
171+
var lowerLine = lines.Item2;
162172
var lowerLower = currentlyTrackedLines.NextLower(lowerLine);
163173

164174
var newLowerIntersection = findIntersection(lowerLine, lowerLower);

tests/Advanced.Algorithms.Tests/Geometry/BentleyOttmann_Tests.cs

+84-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,89 @@ public void BentleyOttmann_Horizontal_Lines_Test()
8383
Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count);
8484
}
8585

86+
[TestMethod]
87+
public void BentleyOttmann_Vertical_Horizontal_Lines_Test()
88+
{
89+
var lines = new List<Line>();
90+
91+
//vertical
92+
lines.Add(new Line(new Point(100, 100), new Point(100, 200)));
93+
lines.Add(new Line(new Point(125, 100), new Point(125, 200)));
94+
lines.Add(new Line(new Point(150, 100), new Point(150, 200)));
95+
lines.Add(new Line(new Point(175, 100), new Point(175, 200)));
96+
lines.Add(new Line(new Point(200, 100), new Point(200, 200)));
97+
98+
//horizontal
99+
lines.Add(new Line(new Point(100, 100), new Point(200, 100)));
100+
lines.Add(new Line(new Point(100, 125), new Point(200, 125)));
101+
lines.Add(new Line(new Point(100, 150), new Point(200, 150)));
102+
lines.Add(new Line(new Point(100, 175), new Point(200, 175)));
103+
lines.Add(new Line(new Point(100, 200), new Point(200, 200)));
104+
105+
var expectedIntersections = getExpectedIntersections(lines);
106+
107+
var bentleyOttmannAlgorithm = new BentleyOttmann();
108+
109+
var actualIntersections = bentleyOttmannAlgorithm.FindIntersections(lines);
110+
111+
Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count);
112+
}
113+
114+
[TestMethod]
115+
public void BentleyOttmann_Vertical_Horizontal_Other_Lines_Test_1()
116+
{
117+
var lines = new List<Line>();
118+
119+
//vertical
120+
lines.Add(new Line(new Point(100, 100), new Point(100, 200)));
121+
lines.Add(new Line(new Point(200, 100), new Point(200, 200)));
122+
123+
//horizontal
124+
lines.Add(new Line(new Point(100, 100), new Point(200, 100)));
125+
lines.Add(new Line(new Point(100, 150), new Point(200, 150)));
126+
lines.Add(new Line(new Point(100, 200), new Point(200, 200)));
127+
128+
//other lines
129+
lines.Add(new Line(new Point(100, 100), new Point(200, 200)));
130+
lines.Add(new Line(new Point(100, 200), new Point(200, 100)));
131+
132+
var expectedIntersections = getExpectedIntersections(lines);
133+
134+
var bentleyOttmannAlgorithm = new BentleyOttmann();
135+
136+
var actualIntersections = bentleyOttmannAlgorithm.FindIntersections(lines);
137+
138+
Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count);
139+
}
140+
141+
142+
[TestMethod]
143+
public void BentleyOttmann_Vertical_Horizontal_Other_Lines_Test_2()
144+
{
145+
var lines = new List<Line>();
146+
147+
//vertical
148+
lines.Add(new Line(new Point(100, 100), new Point(100, 200)));
149+
lines.Add(new Line(new Point(200, 100), new Point(200, 200)));
150+
151+
//horizontal
152+
lines.Add(new Line(new Point(100, 100), new Point(200, 100)));
153+
lines.Add(new Line(new Point(100, 150), new Point(200, 150)));
154+
lines.Add(new Line(new Point(100, 200), new Point(200, 200)));
155+
156+
//other lines
157+
lines.Add(new Line(new Point(110, 100), new Point(210, 200)));
158+
lines.Add(new Line(new Point(90, 200), new Point(250, 100)));
159+
160+
var expectedIntersections = getExpectedIntersections(lines);
161+
162+
var bentleyOttmannAlgorithm = new BentleyOttmann();
163+
164+
var actualIntersections = bentleyOttmannAlgorithm.FindIntersections(lines);
165+
166+
Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count);
167+
}
168+
86169
[TestMethod]
87170
public void BentleyOttmann_Stress_Test()
88171
{
@@ -178,7 +261,7 @@ private static List<Line> horizontalLines()
178261
{
179262
var lines = new List<Line>();
180263

181-
var s1 = new Line(new Point(200, 100), new Point(600, 100));
264+
var s1 = new Line(new Point(100, 100), new Point(600, 100));
182265
var s2 = new Line(new Point(225, 100), new Point(625, 100));
183266
var s3 = new Line(new Point(250, 100), new Point(475, 100));
184267
var s4 = new Line(new Point(290, 100), new Point(675, 100));

0 commit comments

Comments
 (0)