将多边形坐标从Double转换为Long以用于Clipper库
Converting polygon coordinates from Double to Long for use with Clipper library
我有两个多边形,它们的顶点存储为双坐标。我想找到这些多边形的相交区域,所以我在看Clipper库(C++版本)。问题是,Clipper只适用于整数数学(它使用Long类型)。
有没有一种方法可以安全地用相同的比例因子变换我的两个多边形,将它们的坐标转换为Longs,用Clipper执行交集算法,并用相同的因子将生成的交集多边形缩小,并将其转换回Double,而不会损失太多精度?
我不知道该怎么做。
您可以使用一个简单的乘法器在两者之间进行转换:
/* 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。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 用于访问容器<T>数据成员的正确 API
- 重载操作程序时出错>>用于类中的字符串 memebr
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- C++中的cin.ignore()函数不适用于整个流
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 没有用于初始化C++中的变量模板的匹配构造函数
- 用于C++中带有数组和指针的循环
- 为什么它不适用于Visual 2019的原因
- 使用在用于SFINAE的void_t中具有参数的方法
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 重载==不适用于二进制树
- Insert函数不适用于2 if语句C++
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 用于矢量处理的多个线程
- 使外部项目可用于find_package CMake
- map(long long int,long long int)不适用于key=1000 000 000(在long l
- 将多边形坐标从Double转换为Long以用于Clipper库