当我使用boost库中的intersection()函数时,我遇到以下错误。
我有一个多边形矢量,我想得到每个多边形与正方形的交集(((300,-90),(300,90),(500,90),(500,-90), (300,-90)))。
在这些多边形中有一个多边形(((300,90),(273.338,31.7382),(208.263,6.01099),(188.95,12.8509),(161.473,90),(300,90))),
当我在以下循环中计算结果时,此多边形的结果是错误的。输出是(((300,90),(300,90),(500,90),(500,-90),(300,-90),(300,90)))。但是,如果我不使用循环并仅测试此多边形,则交集为空,结果是我所期望的。
这很奇怪。有人知道是什么原因吗?
double Analysis::GetDensity(const vector<polygon_2d>& polygon, const polygon_2d& measureArea)
{
double density=0;
for(vector<polygon_2d>::const_iterator polygon_iterator = polygon.begin(); polygon_iterator!=polygon.end();polygon_iterator++)
{
typedef std::vector<polygon_2d > polygon_list;
polygon_list v;
intersection(measureArea, *polygon_iterator, v);
if(!v.empty())
{
density+=area(v[0])/area(*polygon_iterator);
if((area(v[0])/area(*polygon_iterator))>1.00001)
{
std::cout<<dsv(v[0])<<"\n";
std::cout<<dsv(measureArea)<<"\n";
std::cout<<dsv(*polygon_iterator)<<"\n";
std::cout<<"this is a wrong result "<<area(v[0])<<'\t'<<area(*polygon_iterator)<<"\n";
exit(EXIT_FAILURE);
}
}
}
}