我有一個關於CGAL :: do_intersec()函數的非常愚蠢的問題。關於CGAL do_intersect
這裏是我的代碼:
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Boolean_set_operations_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_2 Point_2;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
int main()
{
Polygon_2 P;
P.push_back (Point_2 (-1,1));
P.push_back (Point_2 (0,-1));
P.push_back (Point_2 (1,1));
/* DOESN'T WORK!!!! WHY???????
P.push_back (Point_2 (-1,1));
P.push_back (Point_2 (1,1));
P.push_back (Point_2 (1,-1));
P.push_back (Point_2 (-1,-1));
*/
Polygon_2 Q;
Q.push_back(Point_2 (-1,-1));
Q.push_back(Point_2 (1,-1));
Q.push_back(Point_2 (0,1));
if ((CGAL::do_intersect (P, Q)))
std::cout << "The two polygons intersect in their interior." << std::endl;
else
std::cout << "The two polygons do not intersect." << std::endl;
return 0;
}
所以,P是一個三角形,現在,該程序工作正常。但是,如果它被改爲矩形,它不起作用。
我的問題是我們如何測試兩個矩形並知道它們是否相交?矩形 - 三角形,矩形 - 圓形怎麼樣?
非常感謝!
謝謝sloriot,問題解決了。然而,新的問題來了......
這些代碼再次失敗。你能幫我看看嗎?所有的點值都是正確的。我已經檢查過。
Point points1[] = {Point(mPointsOfSkt1[0].x, mPointsOfSkt1[0].y), Point(mPointsOfSkt1[1].x, mPointsOfSkt1[1].y),Point(mPointsOfSkt1[2].x, mPointsOfSkt1[2].y), Point(mPointsOfSkt1[3].x, mPointsOfSkt1[3].y)};
Point points2[] = {Point(mPointsOfSkt2[0].x, mPointsOfSkt2[0].y), Point(mPointsOfSkt2[1].x, mPointsOfSkt2[1].y),Point(mPointsOfSkt2[2].x, mPointsOfSkt2[2].y), Point(mPointsOfSkt2[3].x, mPointsOfSkt2[3].y)};
Polygon2d a = Polygon2d(points1, points1+4);
Polygon2d b = Polygon2d(points2, points2+4);
return CGAL::do_intersect(a, b);
的錯誤信息是:
Line : 313
Explanation: The polygon has a wrong orientation.
Refer to the bug-reporting instructions at http://www.cgal.org/bug_report.html
terminate called after throwing an instance of 'CGAL::Precondition_exception'
what(): CGAL ERROR: precondition violation!
Expr: is_valid_unknown_polygon(p, t)
File: /usr/include/CGAL/General_polygon_set_on_surface_2.h
Line: 44
啊哈!我自己想出來了! polygon_2的頂點順序必須逆時針。非常感謝你,男人!
你需要使用一個內核像'CGAL確切結構:: Exact_predicates_exact_constructions_kernel'使用這個函數(這是誤導性的,但它不是來自內核的函數) – sloriot
@sloriot你應該寫一個答案而不是註釋,以便問題可以被關閉。 – lrineau