Is there a robust C++ implementation of the Bentley-Ottmann algorithm?

前端 未结 3 515
深忆病人
深忆病人 2020-12-14 19:17

The Bentley-Ottoman algorithm finds all crossings in a set of line segments. For a well known and important algorithm, it seems quite weird that a C++ implementation of Bent

3条回答
  •  隐瞒了意图╮
    2020-12-14 19:47

    CGAL has something in there with the same complexity as Bentley-Ottmann, O((n + k)*log(n)) where n is the number of segments and k is the number of intersections (not sure which algorithm they used):

    //! \file examples/Arrangement_on_surface_2/sweep_line.cpp
    // Computing intersection points among curves using the sweep line.
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    typedef CGAL::Quotient                  NT;
    typedef CGAL::Cartesian                             Kernel;
    typedef Kernel::Point_2                                 Point_2;
    typedef CGAL::Arr_segment_traits_2              Traits_2;
    typedef Traits_2::Curve_2                               Segment_2;
    
    int main()
    {
      // Construct the input segments.
      Segment_2 segments[] = {Segment_2 (Point_2 (1, 5), Point_2 (8, 5)),
                              Segment_2 (Point_2 (1, 1), Point_2 (8, 8)),
                              Segment_2 (Point_2 (3, 1), Point_2 (3, 8)),
                              Segment_2 (Point_2 (8, 5), Point_2 (8, 8))};
    
      // Compute all intersection points.
      std::list     pts;
    
      CGAL::compute_intersection_points (segments, segments + 4,
                                         std::back_inserter (pts));
    
      // Print the result.
      std::cout << "Found " << pts.size() << " intersection points: " << std::endl; 
      std::copy (pts.begin(), pts.end(),
                 std::ostream_iterator(std::cout, "\n"));
    
      // Compute the non-intersecting sub-segments induced by the input segments.
      std::list   sub_segs;
    
      CGAL::compute_subcurves(segments, segments + 4, std::back_inserter(sub_segs));
    
      std::cout << "Found " << sub_segs.size()
                << " interior-disjoint sub-segments." << std::endl;
    
      CGAL_assertion (CGAL::do_curves_intersect (segments, segments + 4));
    
      return 0;
    }
    

    http://doc.cgal.org/latest/Sweep_line_2/index.html

提交回复
热议问题