我有两个多边形,它们的顶点存储为双坐标。我想找到这些多边形的相交区域,所以我正在查看快船库(C++ 版本)。问题是,Clipper 只适用于整数数学(它使用 Long 类型)。
有没有一种方法可以安全地使用相同的比例因子变换我的两个多边形,将它们的坐标转换为长整型,使用 Clipper 执行相交算法,并使用相同的因子将生成的相交多边形缩小,然后将其转换回双精度没有太多的精度损失?
我不太清楚该怎么做。
您可以使用简单的乘数在两者之间进行转换:
/* Using power-of-two because it is exactly representable and makes
the scaling operation (not the rounding!) lossless. The value 1024
preserves roughly three decimal digits. */
double const scale = 1024.0;
// representable range
double const min_value = std::numeric_limits<long>::min() / scale;
double const max_value = std::numeric_limits<long>::max() / scale;
long
to_long(double v)
{
if(v < 0)
{
if(v < min_value)
throw out_of_range();
return static_cast<long>(v * scale - 0.5);
}
else
{
if(v > max_value)
throw out_of_range();
return static_cast<long>(v * scale + 0.5);
}
}
请注意,刻度越大,精度越高,但也会降低范围。实际上,这会将浮点数转换为定点数。
最后,您应该能够轻松找到代码来使用浮点数学计算线段之间的交点,所以我想知道为什么您要使用 Clipper。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)