Search code examples
c++computational-geometrycgal

Obtaining the intersection points of two circles in CGAL


I am trying to find out the intersection points of two unit-radius circles in CGAL. Inspired by the CGAL sample codes and tutorials, I have succeeded in creating the following code. But unfortunately, I am unable to print out the points. I have noticed that the size of the vector is the number of intersection points. Any help will be appreciated since I am new to CGAL.

    #include <CGAL/Circular_kernel_intersections.h>
    #include <CGAL/Exact_circular_kernel_2.h>

    typedef CGAL::Exact_circular_kernel_2             Circular_k;
    typedef CGAL::Point_2<Circular_k>                 Point_2;
    typedef CGAL::Circle_2<Circular_k>                Circle_2;
    typedef CGAL::Circular_arc_2<Circular_k>          Circular_arc_2;
    typedef CGAL::CK2_Intersection_traits<Circular_k, Circle_2, Circle_2>::type Intersection_result;

    using namespace std;

    int main() {

        Point_2 p(2,2), r(5.5,2);
        Circle_2 c1(p,1), c2(r,1);

        vector<Intersection_result> res;
        intersection(c1,c2,back_inserter(res));

        cout << res.size() << endl;
    }

Solution

  • I tested your code and it seems to work. Note that the intersection points are stored as Circular_arc_point_2. Using your code I added only the following lines:

    using boostRetVal = std::pair<CGAL::Circular_arc_point_2<CGAL::Filtered_bbox_circular_kernel_2<CGAL::Circular_kernel_2<CGAL::Cartesian<CGAL::Gmpq>, CGAL::Algebraic_kernel_for_circles_2_2<CGAL::Gmpq> > > > , unsigned>;
    
    for(const auto& element : res) {
      auto algPoint = std::get<0>( boost::get< boostRetVal >(element) );
      auto point = Point_2(to_double(algPoint.x()), to_double(algPoint.y()));
      std::cout << point << std::endl;
    }
    

    As points I used p(0,0), r(2,0), as circles c1(p,4), c2(r,1), then the received output was:

    2
    7/4 -4360591588697965/4503599627370496
    7/4 4360591588697965/4503599627370496