public void BentleyOttmann_Stress_Test() { var lines = new List <Line>(); lines.AddRange(getRandomLines(1000)); var stopWatch = new Stopwatch(); stopWatch.Start(); var expectedIntersections = getExpectedIntersections(lines); stopWatch.Stop(); var naiveElapsedTime = stopWatch.ElapsedMilliseconds; var bentleyOttmannAlgorithm = new BentleyOttmann(); stopWatch.Reset(); stopWatch.Start(); var actualIntersections = bentleyOttmannAlgorithm.FindIntersections(lines); stopWatch.Stop(); var actualElapsedTime = stopWatch.ElapsedMilliseconds; Assert.IsTrue(actualElapsedTime <= naiveElapsedTime); Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count); }
public void BentleyOttmann_Vertical_Horizontal_Lines_Test() { var lines = new List <Line>(); //vertical lines.Add(new Line(new Point(100, 100), new Point(100, 200))); lines.Add(new Line(new Point(125, 100), new Point(125, 200))); lines.Add(new Line(new Point(150, 100), new Point(150, 200))); lines.Add(new Line(new Point(175, 100), new Point(175, 200))); lines.Add(new Line(new Point(200, 100), new Point(200, 200))); //horizontal lines.Add(new Line(new Point(100, 100), new Point(200, 100))); lines.Add(new Line(new Point(100, 125), new Point(200, 125))); lines.Add(new Line(new Point(100, 150), new Point(200, 150))); lines.Add(new Line(new Point(100, 175), new Point(200, 175))); lines.Add(new Line(new Point(100, 200), new Point(200, 200))); var expectedIntersections = getExpectedIntersections(lines); var bentleyOttmannAlgorithm = new BentleyOttmann(); var actualIntersections = bentleyOttmannAlgorithm.FindIntersections(lines); Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count); }
public void BentleyOttmann_Horizontal_Lines_Test() { var lines = new List <Line>(); lines.AddRange(horizontalLines()); var expectedIntersections = getExpectedIntersections(lines); var bentleyOttmannAlgorithm = new BentleyOttmann(); var actualIntersections = bentleyOttmannAlgorithm.FindIntersections(lines); Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count); }
public void BentleyOttmann_Smoke_Test_2() { var lines = new List <Line>(); var s1 = new Line(new Point(100, 0), new Point(150, 130)); var s2 = new Line(new Point(20, 80), new Point(80, 70)); var s3 = new Line(new Point(80, 70), new Point(50, 100)); lines.AddRange(new[] { s1, s2, s3 }); var expectedIntersections = getExpectedIntersections(lines); var bentleyOttmannAlgorithm = new BentleyOttmann(); var actualIntersections = bentleyOttmannAlgorithm.FindIntersections(lines); Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count); }
private void generate(bool redo) { var precision = 5; tolerance = Math.Round(Math.Pow(0.1, precision), precision); var sweepLine = new BentleyOttmann(precision); if (!redo) { lines = new List <Advanced.Algorithms.Geometry.Line>(); expectedIntersections.Clear(); actualIntersections.Clear(); while (expectedIntersections.Count == actualIntersections.Count) { lines = getRandomLines(nodeCount); if (lines.Any(x => x.IsHorizontal || x.IsVertical)) { continue; } var watch = new Stopwatch(); watch.Start(); expectedIntersections = getExpectedIntersections(lines); watch.Stop(); if (expectedIntersections.Any(x => x.Value.Count > 2)) { expectedIntersections.Clear(); continue; } var orgElapsed = watch.ElapsedMilliseconds; watch.Reset(); long actualElapsed; try { watch.Start(); var actual = sweepLine.FindIntersections(lines); watch.Start(); actualElapsed = watch.ElapsedMilliseconds; actualIntersections = actual.Select(x => x.Key).ToList(); } catch { break; } } } else { try { expectedIntersections = getExpectedIntersections(lines); var ss = new HashSet <Advanced.Algorithms.Geometry.Line>(lines); actualIntersections = sweepLine.FindIntersections(ss) .Select(x => x.Key).ToList(); } catch { } } display(lines, expectedIntersections.Select(x => x.Key).ToList(), actualIntersections); }
public bool CheckIntersections(int line1, List <int> second) { int t_c = 0; var bentleyOttmannAlgorithm = new BentleyOttmann(5); var actualIntersections = bentleyOttmannAlgorithm.FindIntersections(lines); //foreach(Line t in lines) //{ // Debug.Log(t.stroke_number); //} int q = 0; foreach (var temp in actualIntersections) { //Debug.Log(q + " "+temp.Value[0].stroke_number+" "+ temp.Value[1].stroke_number); //q++; int u = Math.Min(temp.Value[0].stroke_number, temp.Value[1].stroke_number); int v = Math.Max(temp.Value[0].stroke_number, temp.Value[1].stroke_number); Debug.Log(u + " " + v); for (int p = 0; p < second.Count; p++) { int x = Math.Min(line1, second[p]); int y = Math.Max(line1, second[p]); if (x == u && v == y) { t_c++; } } if (t_c >= second.Count) { return(true); } } //PriorityQueue<Event> Q = new PriorityQueue<Event>(); //foreach (Line line in lines) //{ // Event a = new Event(line.Left); // Event b = new Event(line.Right); // if (a.CompareTo(b) < 0) // { // a.isUpper = true; // b.isUpper = false; // } // else // { // a.isUpper = false; // b.isUpper = true; // } // Q.Enqueue(a); // Q.Enqueue(b); //} //AVLTree<Status> T = new AVLTree<Status>(); //while(Q.Peek() != null) //{ //Event ptr = Q.Dequeue(); //Status temp1 = new Status(calc_x(ptr.line, ptr.y), ptr.line); //if (ptr.isUpper && !ptr.isIntersect) //{ // T.Insert(temp1); // if(T.NextLower(temp1)!= null) // { // Point temp = LineIntersection.FindIntersection(T.NextLower(temp1).line, ptr.line, 5); // if (temp != null) // { // Event temp2 = new Event(temp); // temp2.line = T.NextLower(temp1).line; // temp2.line1 = ptr.line; // temp2.isIntersect = true; // Q.Enqueue(temp2); // } // } // if(T.NextHigher(temp1) != null) // { // Point temp = LineIntersection.FindIntersection(T.NextHigher(temp1).line, ptr.line, 5); // if (temp != null) // { // Event temp2 = new Event(temp); // temp2.line = T.NextLower(temp1).line; // temp2.line1 = ptr.line; // temp2.isIntersect = true; // Q.Enqueue(temp2); // } // } //} else if(!ptr.isUpper && !ptr.isIntersect) { // if(T.NextHigher(temp1) != null && T.NextLower(temp1) != null) // { // Point temp = LineIntersection.FindIntersection(T.NextHigher(temp1).line, T.NextLower(temp1).line, 5); // if (temp != null) // { // Event temp2 = new Event(temp); // temp2.line = T.NextLower(temp1).line; // temp2.line1 = ptr.line; // temp2.isIntersect = true; // Q.Enqueue(temp2); // } // } // T.Delete(temp1); //} //else //{ // T.Delete(ptr) // Line l1, l2; // if(ptr.line < ptr.intersectLine) // { // l1 = ptr.line; // } // if (status.NextLower(ptr.intersectLine) != null) // { // Point temp = LineIntersection.FindIntersection(ptr.intersectLine, status.NextLower(ptr.intersectLine), 5); // if (temp != null) // { // temp.line = ptr.intersectLine; // temp.intersectLine = status.NextLower(ptr.intersectLine); // temp.isIntersect = true; // Q.Enqueue(temp); // } // } //} //} //foreach (Line l1 in lines1) //{ // foreach (Line l2 in lines1) // { // if (l1 != l2) // { // if (LineIntersection.FindIntersection(l1, l2, 2) != null) // { // int u = Math.Min(l1.stroke_number, l2.stroke_number); // int v = Math.Max(l1.stroke_number, l2.stroke_number); // Debug.Log(u + " " + v); // //for (int p = 0; p < second.Count; p++) // //{ // // int x = Math.Min(line1, second[p]); // // int y = Math.Max(line1, second[p]); // // if (x == u && v == y) // // { // // t_c++; // // } // //} // //if (t_c >= second.Count) // //return true; // } // } // } //} ////return true; //foreach (Line l1 in lines) //{ // foreach (Line l2 in lines) // { // if (l1 != l2) // { // if (LineIntersection.FindIntersection(l1, l2, 2) != null) // { // int u = Math.Min(l1.stroke_number, l2.stroke_number); // int v = Math.Max(l1.stroke_number, l2.stroke_number); // Debug.Log(u + " " + v); // for (int p = 0; p < second.Count; p++) // { // int x = Math.Min(line1, second[p]); // int y = Math.Max(line1, second[p]); // if (x == u && v == y) // { // t_c++; // } // } // if (t_c >= second.Count) // return true; // } // } // } //} //} // if (ptr.isLeft) // { // T.Insert(ptr.line); // if(LineIntersection.FindIntersection(T.NextHigher(ptr.line), ptr.line,5) != null) { // int u = Math.Min(ptr.line.stroke_number, T.NextHigher(ptr.line).stroke_number); // int v = Math.Max(ptr.line.stroke_number, T.NextHigher(ptr.line).stroke_number); // Debug.Log(u + " " + v); // for (int p = 0; p < second.Count; p++) // { // int x = Math.Min(line1, second[p]); // int y = Math.Max(line1, second[p]); // if (x == u && v == y) // { // t_c++; // } // } // if (t_c >= second.Count) // return true; // } // if (LineIntersection.FindIntersection(T.NextLower(ptr.line), ptr.line, 5) != null) // { // int u = Math.Min(ptr.line.stroke_number, T.NextLower(ptr.line).stroke_number); // int v = Math.Max(ptr.line.stroke_number, T.NextLower(ptr.line).stroke_number); // Debug.Log(u + " " + v); // for (int p = 0; p < second.Count; p++) // { // int x = Math.Min(line1, second[p]); // int y = Math.Max(line1, second[p]); // if (x == u && v == y) // { // t_c++; // } // } // if (t_c >= second.Count) // return true; // } // } // else // { // if (LineIntersection.FindIntersection(T.NextLower(ptr.line), T.NextHigher(ptr.line), 5) != null) // { // int u = Math.Min(T.NextLower(ptr.line).stroke_number, T.NextHigher(ptr.line).stroke_number); // int v = Math.Max(T.NextLower(ptr.line).stroke_number, T.NextHigher(ptr.line).stroke_number); // for (int p = 0; p < second.Count; p++) // { // int x = Math.Min(line1, second[p]); // int y = Math.Max(line1, second[p]); // if (x == u && v == y) // { // t_c++; // } // } // if (t_c >= second.Count) // return true; // } // T.Delete(ptr.line); // } //} return(false); }