2017-01-03 134 views
2

我正在使用Kernel :: Circle_2和CGAL :: Arr_circle_segment_traits_2。檢查給定點是否位於圓的(未)有界邊/邊界上(CGAL)

給定一個點(這組特徵的嵌套類型Point_2),我想檢查它是在有界邊,無邊還是在給定圓的邊界上。

有類Circle_2的函數叫做bounded_side,但它僅支持Kernel :: Point_2的點。當我使用CGAL :: to_double()將點轉換爲此類時,我失去了準確性。

是否有另一種方法來執行此檢查? 我將信息存儲在2D_Arrangement中,如果有幫助的話。

回答

1

您可以使用下面的代碼。請注意,2D點的座標類型爲Sqrt_extension

#include <CGAL/Exact_predicates_exact_constructions_kernel.h> 
    #include <CGAL/Arr_circle_segment_traits_2.h> 

    typedef CGAL::Exact_predicates_exact_constructions_kernel K; 
    typedef CGAL::Arr_circle_segment_traits_2<K> Traits; 
    typedef Traits::Point_2::CoordNT Sqrt_extension; 

    CGAL::Bounded_side 
    incircle(const typename K::Circle_2& circle, 
      const typename Traits::Point_2& p) 
    { 
     const K::Point_2& center = circle.center(); 
     K::FT sq_rad = circle.squared_radius(); 

     switch(CGAL::compare( CGAL::square(p.x()-center.x())-sq_rad, 
          -CGAL::square(p.y()-center.y()))) 
     { 
     case CGAL::LARGER: 
      return CGAL::ON_UNBOUNDED_SIDE; 
     case CGAL::SMALLER: 
      return CGAL::ON_BOUNDED_SIDE; 
     case CGAL::EQUAL: 
      break; 
     } 
     return CGAL::ON_BOUNDARY; 
    } 

    int main() 
    { 

     K::Circle_2 circle(K::Point_2(0,0), 2); 

     Traits::Point_2 out(Sqrt_extension(1,2,3) , Sqrt_extension(4,5,6)); 
     CGAL_assertion(incircle(circle, out) == CGAL::ON_UNBOUNDED_SIDE); 

     Traits::Point_2 in(Sqrt_extension(1) , Sqrt_extension(0)); 
     CGAL_assertion(incircle(circle, in) == CGAL::ON_BOUNDED_SIDE); 

     Traits::Point_2 bnd(Sqrt_extension(0,1,2) , Sqrt_extension(0)); 
     CGAL_assertion(incircle(circle, bnd) == CGAL::ON_BOUNDARY); 

     return 0; 
    } 
+0

謝謝@sloriot! – Mega